AWS CLI Tasks

First we have to configure CLI:

Enter your Access Key ID and Secret Access Key.

Screenshot 2021-03-20 163628.jpeg

These credentials should not be shared with anyone

Creating a key pair:-

A key pair is a combination of a public key that is used to encrypt data and a private key that is used to decrypt data.If a key with the specified name already exists, Amazon EC2 returns an error and the key pair returned to you is available only in the Region in which you create it.

Synopsis:- create-key-pair — key-name [ — dry-run | — no-dry-run] [ — tag-specifications ] [ — cli-input-json | — cli-input-yaml] [ — generate-cli-skeleton ] [ — cli-auto-prompt ]

Command:

aws ec2 create-key-pair --key-name task_3

Screenshot 2021-03-20 175031.jpeg

Screenshot 2021-03-20 175421.jpeg

Creating a security group:-

A security group controls inbound and outbound traffic for your instance by acting as a virtual firewall. For more detail, see the Amazon Elastic Compute Cloud User Guide for Amazon EC2 Security Groups and the Amazon Virtual Private Cloud User Guide for Security Groups for Your VPC.

Two security groups for use in EC2-Classic with the same name or two security groups for use in a VPC with the same name cannot be used. 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.

Synopsis:-

create-security-group — description — group-name [ — vpc-id ] [ — tag-specifications ] [ — dry-run | — no-dry-run] [ — cli-input-json | — cli-input-yaml] [ — generate-cli-skeleton ] [ — cli-auto-prompt ]

Command:

aws ec2 create-security-group --group-name task_3 --description cli-task

Screenshot 2021-03-20 180559.jpeg

Screenshot 2021-03-20 180908.jpeg

Launching an instance:

we can Launch any specified number of instances using an AMI for which we want to use and have permission for using.

You can specify a number of options, or leave the default options. The following rules apply:

** [EC2-VPC] If you don’t specify a subnet ID, we choose a default subnet from your default VPC for you. If you don’t have a default VPC, you must specify a subnet ID in the request.

** [EC2-Classic] If don’t specify an Availability Zone, we choose one for you.

** Some instance types must be launched into a VPC. If you do not have a default VPC, or if you do not specify a subnet ID, the request fails. For more information, see Instance types available only in a VPC.

** [EC2-VPC] All instances have a network interface with a primary private IPv4 address. If you don’t specify this address, we choose one from the IPv4 range of your subnet. For more, AWS documentation should be referred.

Synopsis:- run-instances [ — block-device-mappings ] [ — image-id ] [ — instance-type ] [ — ipv6-address-count ] [ — ipv6-addresses ] [ — kernel-id ] [ — key-name ] [ — monitoring ] [ — placement ] [ — ramdisk-id ] [ — security-group-ids ] [ — security-groups ] [ — subnet-id ] [ — user-data ] [ — additional-info ] [ — client-token ] [ — disable-api-termination | — enable-api-termination] [ — dry-run | — no-dry-run] [ — ebs-optimized | — no-ebs-optimized] [ — iam-instance-profile ] [ — instance-initiated-shutdown-behavior ] [ — network-interfaces ] [ — private-ip-address ] [ — elastic-gpu-specification ] [ — elastic-inference-accelerators ] [ — tag-specifications ] [ — launch-template ] [ — instance-market-options ] [ — credit-specification ] [ — cpu-options ] [ — capacity-reservation-specification ] [ — hibernation-options ] [ — license-specifications ] [ — metadata-options ] [ — count ] [ — secondary-private-ip-addresses ] [ — secondary-private-ip-address-count ] [ — associate-public-ip-address | — no-associate-public-ip-address] [ — cli-input-json | — cli-input-yaml] [ — generate-cli-skeleton ] [ — cli-auto-prompt ]

Command:

aws ec2 run-instances --image-id ami-0e306788ff2473ccb --instance-type t2.micro --count 1 --subnet-id subnet-4fbec503  --key-name task_3 --security-group-ids sg-0abeb8b3e2dfac89e

Screenshot 2021-03-20 182448.jpeg

Screenshot 2021-03-20 182654.jpeg

Creating Elastic Block Storage volume:-

Amazon Elastic Block Store (EBS) is an easy to use, high-performance, block-storage service designed for use with Amazon Elastic Compute Cloud (EC2) for both throughput and transaction intensive workloads at any scale. A broad range of workloads, such as relational and non-relational databases, enterprise applications, containerized applications, big data analytics engines, file systems, and media workflows are widely deployed on Amazon EBS.

Synopsis:-

create-volume — availability-zone [ — encrypted | — no-encrypted] [ — iops ] [ — kms-key-id ] [ — outpost-arn ] [ — size ] [ — snapshot-id ] [ — volume-type ] [ — dry-run | — no-dry-run] [ — tag-specifications ] [ — multi-attach-enabled | — no-multi-attach-enabled] [ — cli-input-json | — cli-input-yaml] [ — generate-cli-skeleton ] [ — cli-auto-prompt ]

Command:

aws ec2 create-volume --availability-zone ap-south-1b --no-encrypted --size 1

Screenshot 2021-03-20 185451.jpeg

Screenshot 2021-03-20 185552.jpeg

Attaching Elastic Block Storage volume:

We can attach an EBS volume to a running or stopped instance and exposes it to the instance with the specified device name.

Synopsis:

attach-volume — device — instance-id — volume-id [ — dry-run | — no-dry-run] [ — cli-input-json | — cli-input-yaml] [ — generate-cli-skeleton ] [ — cli-auto-prompt ]

Command:

aws ec2 attach-volume --instance-id i-0a07219835ebf14e0  --volume-id vol-06aa4d03f72784ea3  --device xvdf

Screenshot 2021-03-20 190202.jpeg

Screenshot 2021-03-20 190443.jpeg

EBS is attached to the above created instance.