Create EC2 Instance , Key Pair, Security Group by using CLI.
Today We doing grate particle by using CLI in windows 10. We can configure AWs Services by using 3 methods
1. WebUI
2. CLI
3. By using Programming languages.
Why CLI?
AWS CLI is a tool that pulls all the AWS services together in one central console, giving you easy control of multiple AWS services with a single tool.
Lets do step by step
AWS Provide #aws command to configure and manage aws services,
Problem Statement :
🔅 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 you created in the previous steps.
Prerequisite:
Install AWS CLI in any operating system
AWS account
Configuration :
Check instance in AWS
aws ec2 describe-instances
by using this command aws cli show output in json format.
# aws ec2 describe-instances
{
"Reservations": [
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-0e306788ff2473ccb",
"InstanceId": "Current instance id",
"InstanceType": "t2.micro",
"KeyName": "your key pair",
"LaunchTime": "2020-10-14T04:00:53+00:00",
"Monitoring": {
"State": "disabled"
},
"Placement": {
"AvailabilityZone": "ap-south-1a",
"GroupName": "",
"Tenancy": "default"
},
"PrivateDnsName": "private ip",
"PrivateIpAddress": "ip",
"ProductCodes": [],
"PublicDnsName": "dns",
"PublicIpAddress": "Public ip",
"State": {
"Code": 16,
"Name": "running"
},
They Also the Current state is running , pending or stop.
🔅 Create a key pair
In aws Key pair is major role to manage instance security
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.
This command use to creating key pair.
aws ec2 create-key-pair --key-name keypair
Output:
After Creating Key pair we need to create Security Group for instance
🔅 Create a security group
What is Security Group in AWS?
A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic
Command :
aws ec2 create-security-group --group-name my-sg --vpc-id "VPC iD" --description "cli sg"
Above Command Create Security group for you.
Output:
#aws ec2 describe-security-groups use to show all details of SG.
🔅 Launch an instance using the above created key pair and security group.
What is EC2 Instance?
An EC2 instance is a virtual server in Amazon's Elastic Compute Cloud (EC2) for running applications on the Amazon Web Services (AWS) infrastructure.
command:
aws ec2 run-instances --image-id ami-0e306788ff2473ccb --instance-type t2.micro --count 1 --key-name keypa --security-group-ids sg-05d8e4491 --subnet-id subnet-0ae2
Above command launch instance for you.
If you use the Describe command :
{
"Reservations": [
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-0e306788ff2473ccb",
"InstanceId": "i-2",
"InstanceType": "t2.micro",
"KeyName": "keypair",
"LaunchTime": "2020-10-14T04:00:53+00:00",
"Monitoring": {
"State": "disabled"
},
"Placement": {
"AvailabilityZone": "ap-south-1a",
"GroupName": "",
"Tenancy": "default"
},
"PrivateDnsName": "ip",
"PrivateIpAddress": "Private Ip",
"ProductCodes": [],
"PublicDnsName": "public dns",
"PublicIpAddress": "public Ip",
"State": {
"Code": 16,
"Name": "running"
},
🔅 Create an EBS volume of 1 GB.
Create Extra Volume and Attached our Instance
Command:
aws ec2 create-volume --size 1 --availability-zone ap-south-1a
Output:
{
"AvailabilityZone": "ap-south-1a",
"CreateTime": "2020-10-14T04:17:21+00:00",
"Encrypted": false,
"Size": 1,
"SnapshotId": "",
"State": "creating",
"VolumeId": "vol-0257dd01f",
"Iops": 100,
"Tags": [],
"VolumeType": "gp2"
}
When You Create Volume they Need Availability Zone. this is JSON format.
After Creating Volume Attached our instance those we create Previous.
🔅 The final step is to attach the above created EBS volume to the instance you created in the previous steps.
Command :
aws ec2 attach-volume --volume-id vol-025dd01f --instance-id i-070e6a602 --device /dev/sdf
This Command Very Use full to show the your volume attached with your instance.
Output:
{
"AttachTime": "2020-10-14T04:19:35.535000+00:00",
"Device": "/dev/sdf",
"InstanceId": "i-070ga602",
"State": "attaching",
"VolumeId": "vol-02580gd01f"
}
When We check our WebUI Volume.
If You want to delete instance by using CLI.
Command:
aws ec2 terminate-instances --instance-ids i-120
If You More Details :
This Training under the Linux world Private limited.
Thank for Supporting us.
Follow me on: https://www.garudax.id/in/vishal-dalvi-490b07134/