{"id":7556,"date":"2018-06-07T07:59:29","date_gmt":"2018-06-06T23:59:29","guid":{"rendered":"http:\/\/rmohan.com\/?p=7556"},"modified":"2018-06-07T08:48:10","modified_gmt":"2018-06-07T00:48:10","slug":"ec2-cli-1","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7556","title":{"rendered":"ec2 cli -1"},"content":{"rendered":"<pre>#!\/bin\/bash\r\n\r\n# EC2\r\n\r\n# List your running EC2 istances\r\naws ec2 describe-instances\r\n\r\n# Stops an instance\r\naws ec2 stop-instances --instance-ids i-004f15f18e76bb7eb\r\n\r\n# Starts a stopped instance\r\naws ec2 start-instances --instance-ids i-004f15f18e76bb7eb\r\n\r\n# Reboots an instance\r\naws ec2 reboot-instances --instance-ids i-004f15f18e76bb7eb \r\n\r\n# List image information\r\naws ec2 describe-images --image-ids ami-340aae4e\r\n\r\n#Creates an image from an instance\r\naws ec2 create-image --instance-id i-004f15f18e76bb7eb --name \"WebServer AMI\" --description \"WebServer for dev team\"\r\n\r\n\r\n\r\n<\/pre>\n<pre>aws --version\r\n\r\n# Best practice, verify user configuration\r\naws configure\r\n\r\n# Lists all buckets\r\naws s3 ls\r\n\r\n# Lists contents of named bucket\r\naws s3 ls s3:\/\/demo-simple-lynn-data\r\n\r\n# Create a new bucket in a particular region\r\naws s3 mb s3:\/\/demo-simple-lynn-new-today --region us-west-1\r\n\r\n# Lists all buckets\r\naws s3 ls\r\n\r\n\r\n\r\n<strong>how to launch instances<\/strong> \r\n\r\n\r\n<\/pre>\n<pre>!\/bin\/bash\r\n\r\nIMAGENAME=BASTION\r\nINSTANCEID=i-076d3582c7db06190\r\nIMAGEDESCRIP=\"An AMI for $IMAGENAME\"\r\n\r\n\r\nfunction createImage {\r\n\taws ec2 create-image --instance-id $INSTANCEID --name $IMAGENAME --description \"$IMAGEDESCRIP\"\r\n\r\n}\r\n\r\n# aws configure\r\ncreateImage\r\n# aws ec2 create-image --instance-id i-034a9a2d28543a0de --name \"BASTION\" --description \"An AMI for BASTION\"\r\n# apt install ec2-api-tools\r\n#launchImage\r\n# aws ec2 run-instances --image-id ami-b911ded4 --count 1 --instance-type t2.micro --key-name BASTIONKEYPAIR --security-group-ids sg-a3eaadd8 --subnet-id subnet-013c3c77 --tags Key=Name,Value=BASTION\r\n\r\n\r\n\r\n\r\n<\/pre>\n<pre>#!\/bin\/bash\r\naws ec2 create-image --region=us-west-2 --instance-id=i-acc8cea1 --name=smtp`date +%m%d%y` --no-reboot\r\naws ec2 create-image --region=us-west-2 --instance-id=i-549d8b5c --name=vms`date +%m%d%y` --no-reboot<\/pre>\n<pre>#!\/bin\/bash\r\nec2-describe-images | grep `date --date=\"4 days ago\" +%Y-%m-%d` | awk '{print \"Deregistering-&gt; \" $2; system(\"ec2-deregister \" $2)}'\r\nec2-describe-instances | grep instance | awk '{print \"Creating -&gt; \" $3; system(\"ec2-create-image --name \" $5 \"-$(date +%F) --no-reboot \" $3)}'\r\nec2-describe-snapshots | sort -k 5 | awk '{print \"Deleting-&gt; \" $2; system(\"ec2-delete-snapshot \" $2)}'\r\n\r\n\r\n\r\n\r\n<\/pre>\n<pre>#!\/bin\/bash\r\n\r\n\r\n_instanceIDS=\"instanceIDS \" # Put Instance ID separated by space\r\n\r\n_date=`date +\"%m-%B-%Y\"`\r\n\r\n\r\nfor instanceID in ${_instanceIDS[@]};do\r\n\r\n        # Get the Tag Associated with the EC2 Instances\r\n       _tag=$(aws ec2 describe-instances  --filters \"Name=instance-id,Values=$instanceID \" --query Reservations[*].Instances[*].Tags[*].Value --output text)\r\n     \r\n        \r\n      echo \"Creating AMI for $_tag Instance having instance ID : $instanceID\"\r\n      \r\n      aws ec2 create-image --instance-id $instanceID --description $_tag-AMI-$_date --no-reboot \r\ndone\r\n\r\n\r\n\r\n<\/pre>\n<pre>#!\/bin\/sh\r\n \r\ninstances=$(aws ec2 describe-instances --filters Name=tag-key,Values=backup --query 'Reservations[*].Instances[*].[InstanceId,to_string(Tags[?Key==`backup`].Value),to_string(Tags[?Key==`Name`].Value)]' --output text | tr -d \"[\" | tr -d \"]\" | tr -d \"\\\"\" | awk '{print $1\",\"$2\",\"$3}')\r\n \r\nfor instance in $instances\r\ndo\r\n  parts=$(echo $instance | sed -e \"s\/,\/ \/g\")\r\n  columns=($parts)\r\n  instance_id=${columns[0]}\r\n  name=${columns[2]}\r\n  aws ec2 create-image --instance-id $instance_id --no-reboot --name ${name}_`date +\"%Y%m%d%H%M%S\"`\r\ndone<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#!\/bin\/bash # EC2 # List your running EC2 istances aws ec2 describe-instances # Stops an instance aws ec2 stop-instances &#8211;instance-ids i-004f15f18e76bb7eb # Starts a stopped instance aws ec2 start-instances &#8211;instance-ids i-004f15f18e76bb7eb # Reboots an instance aws ec2 reboot-instances &#8211;instance-ids i-004f15f18e76bb7eb # List image information aws ec2 describe-images &#8211;image-ids ami-340aae4e #Creates an image from an instance [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7556"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7556"}],"version-history":[{"count":3,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7556\/revisions"}],"predecessor-version":[{"id":7559,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7556\/revisions\/7559"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}