Launching EC2 Instance using CLI and attaching EBS volume

Sriw World of Coding
Analytics Vidhya
Published in
6 min readOct 14, 2020

Per-requisites :

AWS-cli setup

  1. Create an account on AWS.
  2. Go to Identity & Access Management (IAM), create a user and generate an access key to configure AWS on your machine. You need to give AdministratorAccess Permissions to this IAM user.
  3. Open the command line and configure AWS
aws configure

4. Provide access key and secret you just generated, along with that provide the region you are going to deploy your cluster in. AWS recommends users to choose regions geographically close to them to reduce latency and costs.

For those who are using AWS Educate Starter Account . Go to AWS Educate -> sign in -> AWS Account -> AWS Educate Starter Account -> Account Details

gedit ~/.aws/credentials

Delete the content of credentials file and paste the below content on credentials file

Create a key pair

A key pair, consisting of a private key and a public key, is a set of security credentials that you use to prove your identity when connecting to an instance. Amazon EC2 stores the public key, and you store the private key.

  • You can view all existing key pair using :
aws ec2 describe-key-pairs --region us-east-1
  • Generate the key to connect to aws using :
ssh-keygen -t rsa -C "key-name" -f ~/.ssh/key-name

The command will create an SSH key, using RSA encryption, with a comment of “key-name”, and save it in the .ssh directory of our home directory, with the private key file key-name, and public key file key-name.pub

  • Now that we have the SSH key pair, we can import to EC2
aws ec2 import-key-pair --region us-east-1 --key-name "key-name" --public-key-material file://$HOME/.ssh/key-name.pub
  • We can also use this command to create key-pair
aws ec2 create-key-pair --key-name "KEY_Name"

Now we can view all key pairs are present in Key Pairs of AWS EC2 Instances

Create a security group

A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic. Inbound rules control the incoming traffic to your instance, and outbound rules control the outgoing traffic from your instance.

  • You can view all existing security groups using :
  • To create a security group use following command :
aws ec2 create-security-group --region us-east-1 --group-name "task_security_group1" --description "Task given to create a security group using cmd line"

In this I am using default rule of security group but you can add a inbound rule using

aws ec2 authorize-security-group-ingress --group-name task_security_group1 --protocol all --cidr 0.0.0.0/0

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

  • We can view all the instance(running,stopped) using following command :
aws ec2 describe-instances --region us-east-1
  • We will be running Amazon Linux 2 AMI and each os has a unique id.
aws ec2 run-instances  --region us-east-1 --image-id ami-0947d2ba12ee1f --instance-type t2.micro --key-name task_key1 --security-group-ids sg-02738b39bda137727 --count 1

Now you can see above command has launched an ec2 instance with given security and key pair.

  • We can get information about the newly launched instance using and we can see that availability zone is us-east-1d :
aws ec2 describe-instances --instance-id _______ --region us-east-1

Create an EBS volume of 1 GB

Amazon Elastic Block Store provides raw block-level storage that can be attached to Amazon EC2 instances.

  • To Create a volume we need to type Following Command.
aws ec2 create-volume --availability-zone us-east-1d --region us-east-1 --volume-type "gp2" --size 1 --tag-specifications ResourceType="volume",Tags=[{Key=name,Value=attach_volume}]

Now we can see new volume is created in the Volume section of ec2 instances

Attach the above created EBS volume to the instance that we created in the previous steps

aws ec2 attach-volume --instance-id i-0dfbe52ecdc945a8f --volume-id vol-01cddc03c3b067384 --device /dev/xvdh --region us-east-1

We can see that after executing the command a volume is attached

Delete the Setup

aws ec2 detach-volume --instance-id i-0dfbe52ecdc945a8f --volume-id vol-01cddc03c3b067384aws ec2 delete-volume --volume-id vol-01cddc03c3b067384
aws ec2 terminate-instances --instance-ids i-0dfbe52ecdc945a8f
aws ec2 delete-security-group --group-name task_security_group1
aws ec2 delete-key-pair --key-name task_key1

I am excited to announce the launch of my new Udemy course, “Apache Airflow Bootcamp: Hands-On Workflow Automation.” This comprehensive course is designed to help you master the fundamentals and advanced concepts of Apache Airflow through practical, hands-on exercises.

You can enroll in the course using the following link: [Enroll in Apache Airflow Bootcamp](https://www.udemy.com/course/apache-airflow-bootcamp-hands-on-workflow-automation/?referralCode=F4A9110415714B18E7B5).

I would greatly appreciate it if you could take the time to review the course and share your feedback. Additionally, please consider sharing this course with your colleagues who may benefit from it.

--

--