Deploying Your First EC2 Instance with AWS CLI

Marc Mandela
4 min readJan 14, 2024

--

Bytes & Brews | Featured scripts have been tested for Linux & Windows

Bytes & Brews

Introduction

As cloud computing continues to reshape the landscape of modern IT infrastructure, Amazon Web Services (AWS) stands at the forefront, providing a robust and scalable platform for various applications. One fundamental building block of AWS is the Elastic Compute Cloud (EC2), which allows users to launch virtual servers in the cloud.

This week, we will explore how to leverage the AWS Command Line Interface (AWS CLI) to create an EC2 instance within a Virtual Private Cloud (VPC). The AWS CLI offers a powerful and efficient way to interact with AWS services, enabling users to automate tasks and streamline their cloud infrastructure deployment.

Prerequisites

Before diving into the EC2 instance creation, ensure you have the AWS CLI installed on your local machine. If not, you can download and install it by following the instructions in the AWS CLI User Guide.

Step 1: Configure AWS CLI

Once the AWS CLI is installed, configure it with your AWS credentials by running the following command:

Configure AWS CLI
aws-cli

This command will prompt you to enter your AWS Access Key ID, Secret Access Key, default region, and output format. Provide the required information to establish a connection with your AWS account.

Step 2: Create a VPC

Let’s start by creating a VPC to “house” our EC2 instance. Use the following command to create a VPC with a specified CIDR block:

Create VPC
aws-cli

This command will return a JSON output containing information about the newly created VPC, including its VPC ID.

Step 3: Create Subnet

Next, create a subnet within the VPC. Replace ‘vpc-id’ with the actual VPC ID obtained from the previous step:

aws-cli

Note/: You can adjust the CIDR block to match the desired IP range for your subnet.

Step 4: Create Internet Gateway

To enable internet access for our EC2 instance, create an internet gateway:

Create IGW
aws-cli

This command returns the Internet Gateway ID, which we will use in the next step.

Step 5: Attach Internet Gateway to VPC

Attach the internet gateway to the VPC:

aws-cli

Replace ‘gateway-id’ and ‘vpc-id’ with the appropriate values.

Step 6: Create Route Table

Create a route table for the subnet:

Create RT
aws-cli

Retrieve the Route Table ID from the output.

Step 7: Add Route to Route Table

Add a route to the route table to enable internet access:

aws-cli

Replace ‘route-table-id’ and ‘gateway-id’ with the correct values.

Step 8: Launch EC2 Instance

Now, it’s time to launch the EC2 instance in the created VPC. Choose an Amazon Machine Image (AMI) and specify the subnet ID:

aws-cli

Replace ‘ami-id’, ‘subnet-id’ and ‘your-key-pair-name’ with the appropriate values.

Final Step: Success!

We have leveraged the power of the AWS CLI to create an EC2 instance inside a VPC!

Conclusion

The AWS CLI is a powerful tool that empowers users to harness the full potential of AWS services, providing a seamless and scalable cloud computing environment. From this simple guide, you have now gained insight into the powerful capabilities of the AWS CLI for automating infrastructure tasks.

As you explore further, consider customizing your VPC, configuring additional security measures, and automating repetitive tasks to enhance your AWS experience.

Let me know in the comments below once you give it a try on your development environment!

Happy Clouding!

Amazon EC2
Bytes & Brews

--

--

Marc Mandela

Marc is a passionate cloud & development expert with a knack for optimizing digital landscapes, transforming businesses of all sizes with innovative solutions.