AWS CLI

From Bonus Bits
Jump to: navigation, search

Description

This article gives references to use Amazon Web Services Command Line Interface commands.


Proxy

Here's a way to force a proxy when calling an AWS CLI command on Linux since there isn't a built in argument currently. Which can be useful if calling AWS CLI before the proxy environment is setup say in a Chef attribute file.

https_proxy=http://proxy.mydomain.com:8080 aws ec2 describe-images --image-ids ami-12345678 --region us-west-2 --query 'Images[*].[Name]' --output text


EC2

Return a JSON of All Instances

aws ec2 describe-instances --profile ${profilename} --region us-west-2

Return a JSON of a specific Instance

aws ec2 describe-instances --profile ${profilename} --region us-west-2 --instance-ids i-12345657890123

Return IAM Profile Role of a specific Instance

aws ec2 describe-instances --profile ${profilename} --region us-west-2 --instance-ids i-12345657890123 --query 'Instances[*].[IamInstanceProfile[?Key==`Arn`].Value[]]' --output text

Start an instance

aws ec2 start-instances --profile ${profilename} --instance-ids i-1348636c


AMI

List a Specific ImageID Name

aws ec2 describe-images --image-ids ami-12345678 --region us-west-2 --query 'Images[*].[Name]' --output text

List a Specific ImageID Tag (Release)

Say you have a Tag named release and you want to output the value of that tag...

aws ec2 describe-images --image-ids ami-12345678 --region us-west-2 --query 'Images[*].[Tags[?Key==`release`].Value[]]' --output text

List All Amazon Linux AMI Oldest to Newest (Name)

aws ec2 describe-images --profile bonusbits --region "us-west-2" --owners amazon --filters "Name=name,Values=amzn-ami*" --query 'sort_by(Images, &CreationDate)[].Name'

List Latest Amazon Linux HVM GP2 (AMI ID)

aws ec2 describe-images --profile bonusbits --region "us-west-2" --owners amazon --filters "Name=name,Values=amzn-ami-hvm*x86_64-gp2" --query 'sort_by(Images, &CreationDate)[-1].ImageId'

List Latest Amazon Linux HVM GP2 (Name)

aws ec2 describe-images --profile bonusbits --region "us-west-2" --owners amazon --filters "Name=name,Values=amzn-ami-hvm*x86_64-gp2" --query 'sort_by(Images, &CreationDate)[].Name'

List Latest Windows Server 2016 Full (AMI ID)

aws ec2 describe-images --profile bonusbits --region "us-west-2" --owners amazon --filters "Name=name,Values=Windows_Server-2016-English-Full-Base*" --query 'sort_by(Images, &CreationDate)[-1].ImageId'

List Latest Private AMI by Name & Tags (AMI ID)

aws ec2 describe-images --owners "123456789012" --profile bonusbits --region "us-west-2" --filters "Name=name,Values=BB-AMZN-*" "Name=tag:release,Values=prod" --query 'sort_by(Images, &CreationDate)[-1].ImageId'

List Latest Private AMI by Tags Only (AMI ID)

aws ec2 describe-images --owners "123456789012" --profile bonusbits --region "us-west-2" --filters "Name=tag:release,Values=prod" "Name=tag:os,Values=linux" "Name=tag:platform,Values=amazon" "Name=tag:platform_version,Values=2018.03" --query 'sort_by(Images, &CreationDate)[-1].ImageId'


S3

List Contents

aws s3 ls --profile ${profilename} s3://mybucket --recursive

Copy From Bucket to Local

aws s3 cp --profile ${profilename} s3://${s3_plugin_path} ${local_plugin_path}

Copy From Local to Bucket

aws s3 cp --profile ${profilename} /tmp/foo/ s3://bucket/ --recursive --exclude "*" --include "*.jpg"

Sync Content From Bucket or Bucket Folder to Local

aws s3 sync s3://${s3_plugin_path} ${local_plugin_path}

Delete Folder Recursively

aws s3 rm --profile ${profilename} --recursive s3://bucketname/foldertodelete

Move

Moves a local file or S3 object to another location locally or in S3.

aws s3 mv --profile ${profilename} test.txt s3://mybucket/test2.txt
aws s3 mv --profile ${profilename} s3://mybucket/test.txt s3://mybucket/test2.txt


S3 (Versioning Enabled)

List Object Versions

aws s3api list-object-versions --profile ${profilename} --bucket <bucket name>


CloudFormation

Validate Template

aws cloudformation validate-template --profile ${profilename} --template-body file:///path/to/template/cfn_template.yml

Create or Update Stack

# Custom Variables
iamaccess=true
stackname=awsaccount-env-stack
profilename=awsaccount
templateurl=https://s3.amazonaws.com/cfn-bucket/stack-template.yml
parametersfilepath=/path/to/template/parameters/awsaccount-region-env-stack-parameters.json
update=false
# Set Task Type
if [ "$update" == "true" ]; then
    task_type=update-stack
else
    task_type=create-stack
fi
# Determine if IAM Needed
if [ "$iamaccess" == "true" ]
    then
        capability_iam=" --capabilities CAPABILITY_IAM"
    else
        capability_iam=" "
fi
aws cloudformation ${task_type} --profile ${profilename} --stack-name ${stackname}${capability_iam} --template-url "${templateurl}" --parameters file://${parametersfilepath}

Get Status of Stack

aws cloudformation describe-stacks  --profile ${profilename} --stack-name "$stackname" --output text --query 'Stacks[*].StackStatus'

Get All Create Complete Events of Stack

aws cloudformation describe-stack-events  --profile ${profilename} --stack-name ${stackname} --query 'StackEvents[?ResourceStatus==`CREATE_COMPLETE`]

Get All Create Failed Events of Stack

aws cloudformation describe-stack-events  --profile ${profilename} --stack-name ${stackname} --query 'StackEvents[?ResourceStatus==`CREATE_FAILED`]

Delete Stack

aws cloudformation delete-stack --profile ${profilename} --stack-name ${stackname}


SSL Certificates

List Certificates

aws acm list-certificates --profile ${profilename}


Related Articles


Sources