AWS CLI

From Bonus Bits
Jump to: navigation, search

Description

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


EC2

Return a JSON of All Instances

aws ec2 describe-instances --profile ${profilename}

Start an instance

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


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