Amazon Web Services (AWS) is the leading platform for cloud computing. It has nearly every conceivable thing you’d need. AWS is used extensively by top companies and startups because of its wide range of products, it’s super easy to dynamically scale, and it’s very secure.
Launching EC2 Instance with EBS and Security Group using AWS CLI
- 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.
Step 1: Create a key pair
aws ec2 create-key-pair --key-name aws-cli-key
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. If a key with the specified name already exists, Amazon EC2 returns an error.
You need to save the key to a file.
Step 2: Create a security group
aws ec2 create-security-group --description "allow-all-traffic" --group-name aws-cli-allowallaws ec2 authorize-security-group-ingress --group-name aws-cli-allowall --protocol -1 --cidr 0.0.0.0/0
A security group acts as a virtual firewall for your instance to control inbound and outbound traffic.
These two commands will create a Security Group named “allow-all-traffic” and will allow traffic from 0.0.0.0/0 (Anywhere in the world)
*Remember Security Group allowing 0.0.0.0/0 is not safe in production servers. But you can use it for testing purposes.
Step 3: Launch an instance using the above created key pair and security group.
aws ec2 run-instances --image-id ami-0e306788ff2473ccb --instance-type t2.micro --count 1 --subnet-id subnet-0f20d73e825bd2692 --security-group-ids sg-053a28599332e48c1 --keyname aws-cli-key
Amazon EC2 provides a wide selection of instance types optimized to fit different use cases. Instance types comprise varying combinations of CPU, memory, storage, and networking capacity and give you the flexibility to choose the appropriate mix of resources for your applications.
You can add tags to your EC2 Instance. Each tag is a simple label consisting of a customer-defined key and an optional value that can make it easier to manage, search for, and filter resources.
aws ec2 create-tags --resources "i-0d00ca977a1021183" --tags Key=Name,Value=aws-cli-instance
Step 4: Create an EBS volume of 1 GB.
An Amazon EBS volume is a durable, block-level storage device that you can attach to your instances. After you attach a volume to an instance, you can use it as you would use a physical hard drive. EBS volumes are flexible or elastic.
aws ec2 create-volume --availability-zone ap-south-1a --size 1 --no-encrypted
Creates an EBS volume 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.
Step 5: The final step is to attach the above created EBS volume to the instance you created in the previous steps.
aws ec2 attach-volume --volume-id vol-09a0c5a87775b6ef7 --instance-id i-0d00ca977a1021183 --device /dev/sdf
Attaches an EBS volume to a running or stopped instance and exposes it to the instance with the specified device name.
Congratulations you have successfully launched EC2 Instance with EBS and Security Group using AWS CLI.
Now you can verify this in the AWS Web Console.
That’s all for today! I’ll be back with some new articles very soon, thanks!
Muhammad Tabish Khanday