AWS CLI commands for VPC, VPC peering, S3 multipart, etc.

AWS CLI commands for VPC, VPC peering, S3 multipart, etc.

Getting Started with AWS CLI: A Command Reference Guide

Amazon Web Services (AWS) can be managed in three primary ways:

  • AWS Management Console - A graphical web interface.

  • AWS CLI (Command Line Interface) - A text-based interface for automation and scripting.

  • AWS API - For programmatically interacting with AWS.

This blog focuses on the AWS CLI, providing a detailed list of useful commands for various AWS resources. If you're starting with the CLI, this guide will be your cheat sheet.

General Configuration and Navigation

1. Configure AWS CLI Credentials

aws configure

2. Explore Regions and Availability Zones

aws ec2 describe-availability-zones
aws ec2 describe-regions

Managing EC2 Instances

3. Describe Instances

aws ec2 describe-instances
aws ec2 describe-instances --region ap-south-1
aws ec2 describe-instances --instance-ids "ID"

4. Control EC2 Instances

aws ec2 stop-instances --instance-id "ID"
aws ec2 start-instances --instance-id "ID"
aws ec2 reboot-instances --instance-id "ID"
aws ec2 describe-instance-status --instance-ids "ID"

5. Launch an EC2 Instance

aws ec2 run-instances --image-id ami-ID --instance-type t2.micro --key-name newkeypair

Managing Key Pairs

6. Create a New Key Pair

aws ec2 create-key-pair --key-name newkeypair

Working with AMIs (Amazon Machine Images)

7. Create and Copy Images

aws ec2 create-image --instance-id "ID" --name "server"
aws ec2 copy-image --region us-east-1 --name server --source-region ap-south-1 --source-image-id "ID"
aws ec2 copy-image --region us-east-1 --name server --source-region ap-south-1 --source-image-id "ID" --encrypted

8. Deregister an AMI

aws ec2 deregister-image --image-id "ID" --region us-east-1

Managing Volumes and Snapshots

9. Volume Commands

aws ec2 describe-volumes --volume-ids "ID"
aws ec2 modify-volume --size 12 --volume-id "ID"
aws ec2 create-volume --volume-type gp2 --size 15 --availability-zone ap-south-1a
aws ec2 describe-volume-modifications --volume-id "ID"

10. Snapshot Commands

aws ec2 create-snapshot --volume-id "ID"

VPC Management

11. VPC Commands

aws ec2 describe-vpcs
aws ec2 delete-vpc --vpc-id "ID"
aws ec2 create-default-vpc
aws ec2 create-vpc --cidr-block 10.0.0.0/16 --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=MyVPC}]'

12. Subnet Commands

aws ec2 create-subnet --vpc-id "ID" --cidr-block 10.0.1.0/24 --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=MySubnet}]'

Internet Gateways and Routing

13. Internet Gateway Commands

aws ec2 create-internet-gateway --tag-specifications 'ResourceType=internet-gateway,Tags=[{Key=Name,Value=MyIGW}]'
aws ec2 attach-internet-gateway --internet-gateway-id "ID" --vpc-id "ID"

14. Route Table and Routes

aws ec2 create-route-table --vpc-id "ID" --tag-specifications 'ResourceType=route-table,Tags=[{Key=Name,Value=MyRoute}]'
aws ec2 create-route --route-table-id "ID" --destination-cidr-block 0.0.0.0/0 --gateway-id igw-ID
aws ec2 associate-route-table --route-table-id "ID" --subnet-id "ID"
aws ec2 create-route --route-table-id "ID" --destination-cidr-block 192.168.0.0/16 --vpc-peering-connection-id "ID"

VPC Peering

15. Peering Commands

aws ec2 create-vpc-peering-connection --vpc-id "ID" --peer-vpc-id "ID" --peer-region ap-southeast-1
aws ec2 describe-vpc-peering-connections --filters "Name=status-code,Values=pending-acceptance"
aws ec2 accept-vpc-peering-connection --vpc-peering-connection-id "ID" --region ap-southeast-1

16. Route with Peering

aws ec2 create-route --route-table-id "ID" --region ap-southeast-1 --destination-cidr-block 10.0.0.0/16 --vpc-peering-connection-id "ID"

Filtering Resources

17. Filter Resources Using Attributes

aws ec2 describe-instances --filters "Name=instance-type,Values=t2.micro"

AWS CLI Commands for S3 Operations

18. Uploading Files to S3

aws s3 cp <local-file-path> s3://<bucket-name>/<file-key>

19. Uploading Directories to S3

aws s3 cp <local-directory-path> s3://<bucket-name>/ --recursive

20. High-Speed Upload Using S3 Transfer Acceleration

aws s3 cp <local-file-path> s3://<bucket-name>/<file-key> --endpoint-url https://<bucket-name>.s3-accelerate.amazonaws.com

S3 Versioning Commands

21. Enable Versioning on a Bucket

aws s3api put-bucket-versioning --bucket <bucket-name> --versioning-configuration Status=Enabled

22. Check Versioning Status

aws s3api get-bucket-versioning --bucket <bucket-name>

23. Upload a File with Versioning Enabled

aws s3 cp myfile.txt s3://my-bucket/myfile.txt

24. List Object Versions

aws s3api list-object-versions --bucket <bucket-name>

25. Retrieve a Specific Object Version

aws s3api get-object --bucket <bucket-name> --key <object-key> --version-id <version-id> <local-file-name>

26. Delete a Specific Object Version

aws s3api delete-object --bucket <bucket-name> --key <object-key> --version-id <version-id>

27. Suspend Versioning

aws s3api put-bucket-versioning --bucket <bucket-name> --versioning-configuration Status=Suspended

28. Re-enable Versioning

aws s3api put-bucket-versioning --bucket <bucket-name> --versioning-configuration Status=Enabled

Multipart Upload Commands

29. Split a Large File into Smaller Chunks

split -b 50M largefile.zip part_

30. Create a Multipart Upload

aws s3api create-multipart-upload --bucket your-bucket-name --key your-file-key

note: This will return an Upload id that you need for subsequent steps.

31. Upload a Part

aws s3api upload-part --bucket your-bucket-name --key your-file-key --part-number 1 --body part_aa --upload-id your-upload-id

32. JSON for Multipart Completion

{  
"Parts": [

    { "ETag": "etag-for-part-1", "PartNumber": 1 },

    { "ETag": "etag-for-part-2", "PartNumber": 2 } ]
}

33. Complete the Multipart Upload

aws s3api complete-multipart-upload --bucket your-bucket-name --key your-file-key --upload-id your-upload-id --multipart-upload file://parts.json

Notes:

Multipart Uploads The ETag values for each part are returned by the upload-part command.

33. Abort a Multipart Upload

aws s3api abort-multipart-upload --bucket your-bucket-name --key your-file-key --upload-id your-upload-id