AWS Tricks#
Quick CLI recipes for the bits of AWS plumbing the operator hits often, subnets, VPCs, NAT, S3, and EC2 AMIs.
Subnets#
Create a subnet.
$ aws ec2 create-subnet --vpc-id <vpc_id> --cidr-block <cidr_block> \
--availability-zone <az> --region <region>
Auto-assign public IPs to instances in a public subnet.
$ aws ec2 modify-subnet-attribute --subnet-id <subnet_id> \
--map-public-ip-on-launch --region <region>
VPC#
Create a VPC.
$ aws ec2 create-vpc --cidr-block <cidr_block> --region <region>
Allow DNS hostnames.
$ aws ec2 modify-vpc-attribute --vpc-id <vpc_id> --enable-dns-hostnames "{\"Value\":true}" --region <region>
NAT#
Set up a NAT gateway.
# allocate elastic IP
$ aws ec2 allocate-address --domain vpc --region <region>
# use the AllocationId to create the NAT gateway for the public zone
$ aws ec2 create-nat-gateway --subnet-id <subnet_id> --allocation-id <alloc_id> --region <region>
S3 API#
$ aws s3api list-buckets --query 'Buckets[].Name' # list bucket names
$ aws s3api get-bucket-location --bucket <bucket_name> # bucket region
$ aws s3 sync <local_path> s3://<bucket_name> # sync local to bucket
$ aws s3 cp <folder_name>/ s3://<bucket_name>/ --recursive # copy folder
$ aws s3 cp <folder_name>/ s3://<bucket_name>/ --recursive --exclude "<pattern>"
$ aws s3 cp example.com/ s3://example-backup/ --recursive --exclude ".git/*"
$ aws s3 rm s3://<bucket_name>/<file_name> # remove file
$ aws s3 rb s3://<bucket_name> --force # delete bucket
$ aws s3 rm s3://<bucket_name>/<key_name> --recursive # empty bucket
EC2 instance#
Create AMI without rebooting the machine.
$ aws ec2 create-image --instance-id <id> \
--name "image-$(date +'%Y-%m-%d_%H-%M-%S')" \
--description "image-$(date +'%Y-%m-%d_%H-%M-%S')" --no-reboot
Lambda#
Use Lambda with scheduled events.
$ sid=Sid$(date +%Y%m%d%H%M%S); aws lambda add-permission \
--statement-id $sid --action 'lambda:InvokeFunction' \
--principal events.amazonaws.com \
--source-arn arn:aws:events:<region>:<arn>:rule/AWSLambdaBasicExecutionRole \
--function-name function:<awsents> --region <region>
Delete unused volumes.
$ for x in $(aws ec2 describe-volumes --filters Name=status,Values=available \
--profile <profile> | grep VolumeId | awk '{print $2}' | tr ',|"' ' '); do
aws ec2 delete-volume --region <region> --volume-id $x;
done