AWS CLI Introduction

Ankit
4 min readOct 14, 2020

The AWS Command Line Interface (AWS CLI) is an open source tool that enables you to interact with AWS services using commands in your command-line shell. It’s 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.The AWS CLI provides direct access to the public APIs of AWS services.

Important Links:

  1. AWS CLI 2 Installation on Linux.
  2. AWS CLI 2 Command Reference.

Task Description

1. Create a key pair
2. Create a security group
3. Launch an instance using the above created key pair and security group.
4. Create an EBS volume of 1 GB.
5. The final step is to attach the above created EBS volume to the instance you created in the previous steps.

Create a key pair : Creates a 2048-bit RSA key pair with the specified name. Amazon EC2 stores the public key and displays the private key for you to save to a file. The private key is returned as an unencrypted PEM encoded PKCS#1
private key. The key pair returned to you is available only in the Region in which you create it.

aws ec2 create-key-pair --key-name key1 --query 'KeyMaterial' --output text > key2.pem

Output

Create a security group : A security group acts as a virtual firewall for your instance to control inbound and outbound traffic.You can’t have two security groups for use in EC2 with the same name. You have a default security group for use in EC2. If you don’t specify a security group when you launch an instance, the instance is launched into the appropriate default security group. A default security group includes a default rule that grants instances unrestricted network access to each other. You can add or remove rules from your security groups using Autho-
rizeSecurityGroupIngress, AuthorizeSecurityGroupEgress, RevokeSecurityGroupIngress, and RevokeSecurityGroupEgress.

aws ec2 create-security-group --group-name mysg1 --description mysecuritygroup1

Output

IngressRule to security group:

aws ec2 authorize-security-group-ingress --group-name mysg1 --protocol tcp --port 22 --cidr 0.0.0.0/0

Launch an instance using the above created key pair and security group.

aws ec2 run-instances --image-id ami-0947d2ba12ee1ff75 --instance-type t2.micro --count 1 --security-group-ids sg-0ceaad0cdac4e5e62 --key-name key1

Output

Create an EBS volume of 1 GB : Creates an EBS volume that can be attached to an instance in the same Availability Zone.

aws ec2 create-volume --availability-zone us-east-1a --volume-type gp2 --size 1

Output

Attach the above created EBS volume to the above created instance : Attaches an EBS volume to a running or stopped instance and exposes it to the instance with the specified device name. After you attach an EBS volume, you must make it available.

aws ec2 attach-volume --device /dev/sdb --instance-id i-0adf14aa70c11d23d --volume-id vol-033a31c6fc102d01e

Output

We have successfully attached EBS vol to our instance all through AWS CLI only without need to use AWS WebUI.

Complete shell-script

https://github.com/ankit-since1996/aws_cli

#awscloud #awscli #aws #vimaldaga #righteducation #educationredefine #rightmentor #worldrecordholder #linuxworld #makingindiafutureready #righeudcation #arthbylw #awsbylw

Thanks

--

--