Create EC2 instance Using AWS CLI
AWS CLI is a super useful tool that lets you manage your Amazon Web Services (AWS) from your computers command line. Once you download and install it, you can control lots of AWS services right from your command line. For developers, it's a super handy tool to manage everything AWS-related.
Step 1: Install and update the AWS CLI version 1 on Windows.
Start with installing and updating AWS CLI version 1 on Windows. And to install AWS CLI version 1 on Windows:
To update AWS CLI version 1 on Windows:
o Open a command prompt or Power Shell window.
o Use the aws --version command to check your current version.
o If an update is available, you can download the latest installer from the AWS CLI documentation page and run it to update your installation.
Step 2: Configuration
After installation, you need to configure AWS CLI with your AWS credentials. You can do this by running the aws configure command and providing your Access Key ID, Secret Access Key, default region, and default output format (JSON).
Step 3: Creation of VPC
To get started, let's create a Virtual Private Cloud (VPC) using the command line interface (CLI). Here's the command you need to type into your command prompt:
aws ec2 create-vpc --cidr-block 10.0.0.0/16
I've used the CIDR block 10.0.0.0/16, but you can choose any range that fits your needs. Take note of the VPC ID provided in the output.
Once you've executed the command, you can verify in the AWS Management Console that the VPC has been successfully created.
Step 4: Creating Subnets
Next, let's set up two subnets—one for public access and another for private use.
aws ec2 create-subnet --vpc-id <vpcId> --cidr-block 10.0.1.0/24
Make sure to note the generated SubnetId for future reference. We've used the CIDR block 10.0.1.0/24 for this subnet.
Once you've executed the command, you can verify in the AWS Management Console that the public subnet has been successfully created.
aws ec2 create-subnet --vpc-id <vpcId> --cidr-block 10.0.0.0/24
Make sure to note the generated SubnetId for future reference
Once you've executed the command, you can verify in the AWS Management Console that the second subnet has been successfully created.
Step 5: Setting Up Internet Gateway
An internet gateway is crucial for the private subnet to access the internet for updates and installations of other packages.
aws ec2 create-internet-gateway
aws ec2 attach-internet-gateway --vpc-id <vpcId> --internet-gateway-id <InternetGatewayId>
Replace <vpcId> with the noted VPC ID and <InternetGatewayId> with the generated Internet Gateway ID.
Once you've executed the command, you can verify in the AWS Management Console that the internet gateway has been successfully created and attached to the VPC.
Step 6.1: Setting Up Route Table
Next, let's create a route table and associate it with the previously created VPC. Once the route table is created, we'll assign a route to it using the following commands:
aws ec2 create-route-table --vpc-id <vpcId>
Make sure to note down the RouteTableId generated.
Once you've executed the command, you can verify in the AWS Management Console that the route table has been successfully created.
aws ec2 create-route --route-table-id <RouteTableId> --destination-cidr-block 0.0.0.0/0 --gateway-id <InternetGatewayId>
In this command, we've used 0.0.0.0/0 as the destination CIDR block.
Replace <vpcId> with the noted VPC ID, <RouteTableId> with the generated Route Table ID, and <InternetGatewayId> with the Internet Gateway ID.
Once you've executed the command, you can verify in the AWS Management Console that the route table used 0.0.0.0/0 as the destination CIDR block.
Recommended by LinkedIn
Step 6.2: Checking Route Table and Subnets
To ensure that the route table and subnets have been successfully created and assigned, you can use the following commands:
aws ec2 describe-route-tables --route-table-id <RouteTableId>
aws ec2 describe-subnets --filters "Name=vpc-id,Values=<vpcId>" --query "Subnets[*].{ID:SubnetId,CIDR:CidrBlock}"
Make sure to replace <vpcId> with your VPC ID.
These commands will provide you with details about the route table and subnets associated with your VPC.
Step 7: Configuring Route Table Association and Subnet Modification
Next, let's associate the route table with the subnet and make that subnet public by enabling the mapping of public IP addresses to it.
aws ec2 associate-route-table --subnet-id <SubnetId> --route-table-id <RouteTableId>
aws ec2 modify-subnet-attribute --subnet-id <SubnetId> --map-public-ip-on-launch
Make sure to replace <SubnetId> and <RouteTableId> with the respective IDs you noted earlier.
These commands will ensure that the route table is correctly associated with the subnet and that the subnet is configured to receive public IP addresses.
Step 8: Setting Up Key Pair and Security Group
One of the most crucial steps is to create a key pair, which provides secure access to the EC2 instance.
aws ec2 create-key-pair --key-name AWS-Keypair --query "KeyMaterial" --output text > "C:\Users\HP\Downloads\AWS_Keypair.pem"
Here, we've named the key pair file (a .pem file) as AWS-Keypair, and it will be saved to the specified path (C:\Users\HP\Downloads\AWS_Keypair.pem). You can customize both the key pair name and file path.
aws ec2 create-security-group --group-name <security-group-name> --description "<description>" --vpc-id <vpcId>
Replace <security-group-name> and <description> with the desired name and description for your security group. Note the generated GroupId for the next step.
aws ec2 authorize-security-group-ingress --group-id <GroupId> --protocol tcp --port 22 --cidr 0.0.0.0/0
In this command, we're allowing inbound traffic on TCP port 22 (SSH) from any IP address (0.0.0.0/0). Make sure to replace <GroupId> with the GroupId noted earlier.
These steps ensure secure access to your EC2 instance and define the security group rules for inbound traffic.
Once you've executed the commands, you can verify in the AWS Management Console that the port 22 has been open for security group successfully.
Step 9: Launching the EC2 Instance
Now that all the setup is done, it's time to start the EC2 instance. Follow these steps:
aws ec2 run-instances --image-id <ami-id> --count 1 --instance-type t2.micro --key-name <Keypair-name> --security-group-ids <SecurityGroupId> --subnet-id <SubnetId>
Replace <ami-id> with the copied AMI ID, <Keypair-name> with your key pair name, <SecurityGroupId> with the security group ID, and <SubnetId> with the subnet ID.
After running the command, you'll receive an Instance ID. Make sure to note it down. These steps will start your EC2 instance, allowing you to use it for your desired applications.
aws ec2 create-tags --resources i-1234567890abcdef0 --tags Key=Name,Value=YourInstanceName
Replace i-1234567890abcdef0 with the instance ID of your newly created instance, and YourInstanceName with the desired name for your instance.
Once you've executed the command, you can verify in the AWS Management Console the name of your Ec2 instance.
Step10: Checking the Instance
aws ec2 describe-instances --instance-id <InstanceId>
Replace <InstanceId> with the ID of your instance.
This command will give you comprehensive information about your EC2 instance, allowing you to monitor its status and configuration.
Really productive post Swati Kanungo!! Well done 🙂
Well done Swati, useful blog!
Thanks for posting, I will use this lab.
Keep rolling, amazing posts like this, Swati! And people, if you have any constructive feedback for her, please feel free to comment and help her improve.