{"id":1442,"date":"2019-10-30T18:50:02","date_gmt":"2019-10-30T15:50:02","guid":{"rendered":"https:\/\/artem.services\/?p=1442"},"modified":"2019-11-05T17:06:58","modified_gmt":"2019-11-05T14:06:58","slug":"aws-ec2-instance-startstop-by-tag","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=1442","title":{"rendered":"AWS &#8212; EC2 Instance start|stop by tag"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-214\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo.png\" alt=\"\" width=\"975\" height=\"450\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo.png 975w, https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo-300x138.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo-768x354.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo-954x440.png 954w\" sizes=\"(max-width: 975px) 100vw, 975px\" \/><\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 <strong>BASH<\/strong> \u0441\u043a\u0440\u0438\u043f\u0442 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0438\u043b\u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0432\u0441\u0435 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u044b \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c \u0442\u0435\u0433\u043e\u043c.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n\r\nAWS=&quot;$(which aws)&quot;\r\nPROFILE=&quot;default&quot;\r\nREGION=&quot;eu-west-2&quot;\r\nTAG_KEY=&quot;Owner&quot;\r\nTAG_VALUE=&quot;Artem&quot;\r\n\r\nif [ &quot;$#&quot; == 0 ]\r\nthen\r\n  echo &quot;This script for start\/stop AWS EC Instances by tag&quot;\r\n  echo &quot;Usage  : $0 [start|stop]&quot;\r\n  echo &quot;Example: $0 start&quot;\r\n  exit 1\r\nfi\r\n\r\nif [ &quot;$1&quot; == &quot;start&quot; ]\r\nthen\r\n\t$AWS --profile $PROFILE --region $REGION ec2 start-instances --instance-ids `$AWS --profile $PROFILE --region $REGION ec2 describe-instances --filters &quot;Name=tag:$TAG_KEY,Values=$TAG_VALUE&quot; --query 'Reservations[].Instances[].InstanceId' --output text`\r\nelif [ &quot;$1&quot; == &quot;stop&quot; ]\r\nthen\r\n\t$AWS --profile $PROFILE --region $REGION ec2 stop-instances --instance-ids `$AWS --profile $PROFILE --region $REGION ec2 describe-instances --filters &quot;Name=tag:$TAG_KEY,Values=$TAG_VALUE&quot; --query 'Reservations[].Instances[].InstanceId' --output text`\r\nelse\r\n\texec $0\r\nfi\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Python3<\/strong> \u0441\u043a\u0440\u0438\u043f\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0430\u043a \u0436\u0435 \u0441\u043b\u0443\u0436\u0438\u0442 \u0434\u043b\u044f \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438\/\u0441\u0442\u0430\u0440\u0442\u0430 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u043e\u0432 \u043f\u043e \u0442\u0435\u0433\u0443, \u0442\u0430\u043a \u0436\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0443\u0435\u0442 \u0441\u0442\u0430\u0442\u0443\u0441 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u0442\u044c\/\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u044b \u0441\u043e \u0441\u0442\u0430\u0442\u0443\u0441\u043e\u043c &quot;<strong>terminated<\/strong>&quot;<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nimport boto3\r\nimport sys\r\nfrom botocore.exceptions import ClientError\r\n\r\nprofile = &quot;default&quot;\r\nregion = &quot;eu-west-2&quot;\r\ntagName = &quot;Owner&quot;\r\ntagValue = &quot;Artem&quot;\r\n\r\nif sys.argv[1:]:\r\n\taction = sys.argv[1].upper()\r\nelse:\r\n\taction = &quot;help&quot;\r\n\r\nsession = boto3.Session(profile_name=profile)\r\nec2 = session.client('ec2',region_name=region)\r\n\r\ncustom_filter_start = [{\r\n    'Name':'tag:' + tagName, \r\n    'Values': [tagValue]},\r\n    {\r\n    'Name': 'instance-state-name',\r\n    'Values': ['stopped']}]\r\n\r\ncustom_filter_stop = [{\r\n    'Name':'tag:' + tagName, \r\n    'Values': [tagValue]},\r\n    {\r\n    'Name': 'instance-state-name',\r\n    'Values': ['running', 'pending']}]\r\n\r\ndef instance_start(instanceId):\r\n\t# Do a dryrun first to verify permissions\r\n\ttry:\r\n\t\tec2.start_instances(InstanceIds=[instanceId], DryRun=True)\r\n\texcept ClientError as e:\r\n\t\tif 'DryRunOperation' not in str(e):\r\n\t\t\traise\r\n\t# Dry run succeeded, run start_instances without dryrun\r\n\ttry:\r\n\t\tresponse = ec2.start_instances(InstanceIds=[instanceId], DryRun=False)\r\n\t\tprint(response)\r\n\texcept ClientError as e:\r\n\t\tprint(e)\r\n\r\ndef instance_stop(instanceId):\r\n\t# Do a dryrun first to verify permissions\r\n\ttry:\r\n\t\tec2.start_instances(InstanceIds=[instanceId], DryRun=True)\r\n\texcept ClientError as e:\r\n\t\tif 'DryRunOperation' not in str(e):\r\n\t\t\traise\r\n\t# Dry run succeeded, run start_instances without dryrun\r\n\ttry:\r\n\t\tresponse = ec2.stop_instances(InstanceIds=[instanceId], DryRun=False)\r\n\t\tprint(response)\r\n\texcept ClientError as e:\r\n\t\tprint(e)\r\n\r\n\r\nif action == 'START':\r\n\tresponse = ec2.describe_instances(Filters=custom_filter_start)\r\n\tcount = len(response['Reservations'])\r\n\tfor x in range(count):\r\n\t\tinstanceId = response['Reservations'][x]['Instances'][0]['InstanceId']\r\n\t\tinstance_start(instanceId)\r\nelif action == 'STOP':\r\n\tresponse = ec2.describe_instances(Filters=custom_filter_stop)\r\n\tcount = len(response['Reservations'])\r\n\tfor x in range(count):\r\n\t\tinstanceId = response['Reservations'][x]['Instances'][0]['InstanceId']\r\n\t\tinstance_stop(instanceId)\r\nelse:\r\n\tprint(&quot;This script for start\/stop AWS EC Instances by tag&quot;)\r\n\tprint(&quot;Usage  : python3 &quot; + sys.argv[0] + &quot; [start|stop]&quot;)\r\n\tprint(&quot;Example: python3 &quot; + sys.argv[0] + &quot; start&quot;)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<blockquote><p>\u041c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043f\u0438\u0442\u043e\u043d \u043f\u0430\u043a\u0435\u0442\u0430 <strong>boto3<\/strong><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip3 install boto3 \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u0414\u0430\u043d\u043d\u044b\u0439 BASH \u0441\u043a\u0440\u0438\u043f\u0442 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0438\u043b\u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0432\u0441\u0435 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u044b \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c \u0442\u0435\u0433\u043e\u043c. &nbsp; &nbsp; Python3 \u0441\u043a\u0440\u0438\u043f\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0430\u043a \u0436\u0435 \u0441\u043b\u0443\u0436\u0438\u0442 \u0434\u043b\u044f \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438\/\u0441\u0442\u0430\u0440\u0442\u0430 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u043e\u0432 \u043f\u043e \u0442\u0435\u0433\u0443, \u0442\u0430\u043a \u0436\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0443\u0435\u0442 \u0441\u0442\u0430\u0442\u0443\u0441 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u0442\u044c\/\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u044b \u0441\u043e \u0441\u0442\u0430\u0442\u0443\u0441\u043e\u043c &quot;terminated&quot; &nbsp; \u041c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043f\u0438\u0442\u043e\u043d \u043f\u0430\u043a\u0435\u0442\u0430 boto3 &nbsp;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[24],"tags":[25,27,89,33,1145,1147,1159,1161,1149,1151],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1442"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1442"}],"version-history":[{"count":3,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1442\/revisions"}],"predecessor-version":[{"id":1445,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1442\/revisions\/1445"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}