Getting started with AWS CLI
Pic source: whizlabs

Getting started with AWS CLI

The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

  • Download and install https://awscli.amazonaws.com/AWSCLIV2.msi

Check if it has installed properly:

#aws --version
aws-cli/2.0.47 Python/3.7.4 Windows/10 botocore/2.0.0
  • Create IAM User with Programmatic Access note down access keys.
  • Configure the AWS Command Line Interface (AWS CLI) and specify the settings for interacting with AWS.
#aws configure

Enter your keys and options as prompted

No alt text provided for this image

Syntax: aws [options] <command> <subcommand> [parameters]

Let's see how to provision resources through AWS following this path:

1. Create KeyPair
2. Create Security Group
3. Edit ingress rules
4. Launch instance
5. Describe instance
6. Create Volume
7. Attach volume

  1. Create Key Pair
#aws ec2 create-key-pair --key-name MyKeyPair
No alt text provided for this image

Copy KeyMaterial contents to a new file MyKeyPair.pem

2. Create Security Group

#aws ec2 create-security-group --description "Allow All" --group-name "cli-sec-grp" --vpc-id "vpc-ba1fffd1"
No alt text provided for this image

Note down the GroupId

3. Add Ingress Rule to the Security Group

You can add or remove rules from your security groups using AuthorizeSecurityGroupIngress , AuthorizeSecurityGroupEgress , RevokeSecurityGroupIngress , and RevokeSecurityGroupEgress

Here I allow All Traffic to all ports from all IPs.

#aws ec2 authorize-security-group-ingress --group-id "sg-03f35cc325429b620" --protocol "all" --cidr "0.0.0.0/0"

Use help option to view options: #aws ec2 authorize-security-group-ingress help

4. Launch EC2 Instance

Use the help command

#aws ec2 run-instances help

#aws ec2 run-instances --image-id "ami-0e306788ff2473ccb" --instance-type "t2.micro" --key-name MyKeyPair --security-group-ids "sg-03f35cc325429b620" --subnet-id "subnet-1e3a3376" --count 1

Here, I've used Amazon Linux AMI
No alt text provided for this image

5. Describe an instance

#aws ec2 describe-instances --instance-ids i-0e3a5dfc347bfdfce
No alt text provided for this image

With this, you can obtain all details such as private IP, public IP, volumes attached, instance image, type, etc

6. Create EBS Volume

aws ec2 create-volume --volume-type "gp2" --size 1 --availability-zone "ap-south-1a" --tag-specifications ResourceType=volume,Tags=[{Key=name,Value=cli-vol}]

Volume type "gp2" of size 1 GiB in the same AZ as the instance with Tag Name:cli-vol

No alt text provided for this image

Note down VolumeId

7. Attach volume to the instance

aws ec2 attach-volume --instance-id "i-0e3a5dfc347bfdfce" --volume-id "vol-013f313659ce4b78d" --device "/dev/xvdf"
No alt text provided for this image

Now you may SSH into the instance and partition the volume, format it and mount it to a folder.

Always make use of "help" command while working with AWS CLI. Not only does it list the options and parameters but also displays various examples.

There are cases that cannot be done via console / WebUI and need the use of CLI. Such as:

  • Launching multiple instances together in different subnets
  • Launching multiple instances together with different AMIs
  • Launching multiple instances together with different instance types
  • Many more cases like S3 permissions, ACLs are easier to manage via CLI
  • We can combine all these commands into a single script and run it as and when required.

To view or add a comment, sign in

More articles by Ishan Singhal

Others also viewed

Explore content categories