Interaction with AWS CLI to configure EC2 instance at AWS
Priyanka Bharti
Software Engineer @ Samsung | C++ | Android Development | Kotlin | Linux
Amazon Web Services (AWS) is the world’s most comprehensive and broadly adopted cloud platform, offering over 175 fully featured services from data centers globally. Millions of customers—including the fastest-growing startups, largest enterprises, and leading government agencies—are using AWS to lower costs, become more agile, and innovate faster.
The AWS Command Line Interface (CLI) is an open source tool that enables to interact with AWS services using commands in our command-line shell. With minimal configuration, the AWS CLI enables us to start running commands that implement functionality equivalent to that provided by the browser-based AWS Management Console from the command prompt in our terminal program.
With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts..
We can access AWS services by three different ways :-
- WebUI
2. Automation through Scripts
3. Amazon Command Line Interface
See the AWS CLI command reference for the full list of supported services.
You can get help on the command line to see the supported services,
$ aws help aws <command> help aws <command> <subcommand> help
We now gonna perform following tasks using AWS CLI wherein we will
- Create a key pair
- Create a Security group
- Launch an instance using the above created key pair and security group.
- Create an EBS volume of 1 GB.
- The final step is to attach the above created EBS volume to the instance we created in previous steps.
Before starting with the tasks, we need an IAM account and AWS CLI installed on our OS. Link to download AWS CLI - https://awscli.amazonaws.com/AWSCLIV2.msi. AWS CLI can be used from any OS using commands. I have used windows here for the same!
Let's start with the task!
Step 1:
To use cli, we need an access key ID and secret access key for authentication to access aws cloud. We need to create a IAM user to access AWS CLI and AWS console,
Step 2:
Download AWS CLI software on your system using above link and configure it with above created account.
Step 3.
Creating a key pair which can be used to access our instances in future. The output is an ASCII version of the private key and key fingerprint. You need to save the key to a file.
Step 4:
A security group acts as a virtual firewall for our instance to control inbound and outbound traffic. Let's create a security group using CLI as shown below.
Now after creation of security group, we need to set inbound traffic rules in it. An inbound rule permits instances to receive traffic from the specified IPv4 or IPv6 CIDR address ranges, or from the instances associated with the specified destination security groups.
Here, we set the inbound traffic for all IPs and allow all port to access our instance.
The above created security group can be seen below.
Step 5:
Let's now launch an ec2 instance using the above created key pair and security group. To create an instance we need AMI name, instance type, count (no. of instances), subnet, storage root, security group name and key name. The complete command to launch a new instance using aws CLI :
aws ec2 run-instances --image-id <ami-image-id> --instance-type t2.micro --count<number> --subnet-id <id> --security-group-ids <id> --key-name <keyname>
The instance we created above using AWS CLI can be seen in our dashboard, as below :
Step 6:
Let's move a step ahead by creating an EBS volume of capacity 1 GB that can be attached to an instance in the same Availability Zone. The volume is created in the regional endpoint that you send the HTTP request to.
We can see the above created volume in the dashboard as follows :
Step 7:
Let's move to the final step i.e., to attach the above created EBS volume to the instance we created in the previous steps.
To attach an EBS volume to an ec2 instance, both must be in the same availability zone. This is because EBS is a zonal service !!
Step 8:
Now, let's check from the dashboard, if the EBS volume successfully attached to the ec2 instance.
Finally, let's conclude our work here. We have successfully launched an EC2 instance, an EBS volume and attached it to the instance using AWS CLI.
Thank you for reading !!
:)
Backend Engineer at LCX | Web3 | Ethereum | Cardano
4 年Awesome