Skip to main content

Command Palette

Search for a command to run...

Automating EC2 Instance Launch using AWS CLI and Shell Scripting

Using shell script to automate common AWS tasks

Published
•8 min read•View as Markdown

Introduction

In today's fast-paced world of cloud infrastructure management, automation plays a crucial role in ensuring efficiency and scalability. With the AWS Command Line Interface (CLI) and shell scripting, you can automate the process of launching EC2 instances on Amazon Web Services (AWS) with ease. In this blog post, we will explore how to set up the AWS CLI, generate credentials, and create a shell script that automates the launch of EC2 instances. By the end, you'll have a solid foundation for automating your infrastructure provisioning.

Setting up AWS CLI

1.1. Installing AWS CLI: To get started, you'll need to install the AWS CLI on your local machine. The installation process varies depending on your operating system. Here are the steps for common platforms:

  • For Windows:

  • For macOS:

    • Open a terminal window.

    • Execute the following command to install the AWS CLI using Homebrew:

        codebrew install awscli
      
    • Verify the installation by running the command aws --version.

  • For Linux:

    • Open a terminal.

    • Execute the appropriate command based on your distribution:

      • Ubuntu/Debian:

      •   sudo apt-get install awscli
        
      • CentOS/RHEL:

          sudo yum install awscli
        
      • Other distributions: Refer to the official AWS CLI documentation.

    • Verify the installation by running the command aws --version.

1.2. Configuring AWS CLI: Once the AWS CLI is installed, you need to configure it with your AWS account credentials.

  • Generate AWS Access Keys:

    • Go to the AWS Management Console and navigate to the IAM (Identity and Access Management) service.

    • Create a new IAM user or use an existing one.

    • Generate an access key for the user, which consists of an access key ID and a secret access key.

    • Save the access key ID and secret access key in a secure location.

  • Configure AWS CLI:

    • Open a terminal or command prompt.

    • Run the command

        aws configure.
      
    • Enter the access key ID and secret access key when prompted.

    • Specify your preferred default region and output format (e.g., JSON).

    • Press Enter to complete the configuration.

Section 2: Understanding the Shell Script

2.1. Shebang and Echo Statements

Let's dive into the shell script that automates the EC2 instance launch. Open a text editor and save the following code as launch_ec2.sh:

shellCopy code#!/bin/sh
echo "We are going to launch an EC2 Instance through CLI";

# Rest of the code goes here...

In this code snippet, the shebang (#!/bin/sh) indicates that the script should be interpreted by the shell. The echo statement is used to display a message indicating the start of the automation process.

2.2. VPC and Subnet Creation

The script automates the creation of a custom VPC and subnet within AWS. Add the following code below the echo statement:

shellCopy codeecho "We are first going to create our custom VPC------------";

# VPC Creation
echo "The VPC ID of the newly created VPC is:";
aws ec2 create-vpc --cidr-block 172.32.0.0/16 --query Vpc.VpcId --output text

# Subnet Creation
echo "Now we are going to create our own custom Subnet---------------";
echo "Enter The VPC ID from above";
read vpcId;
echo "Enter CIDR block ";
read cidr;
echo "The Subnet ID of the newly created Subnet:";
aws ec2 create-subnet --vpc-id $vpcId --cidr-block $cidr --output text

This code segment uses the aws ec2 create-vpc command to create a custom VPC with the specified CIDR block. The resulting VPC ID is displayed. Then, the script prompts the user to enter the VPC ID and CIDR block to create a custom subnet using the aws ec2 create-subnet command. The subnet ID is displayed afterward.

2.3. Making the VPC and Subnet Private

To make the VPC and subnet private, we need to create an internet gateway and a route table. Add the following code:

shellCopy codeecho "Making the VPC and Subnet private----------";

echo "Internet Gateway ID of the newly created internet gateway";
aws ec2 create-internet-gateway --query InternetGateway.InternetGatewayId --output text
echo "Enter Internet Gateway ID";
read igId;

# Attach the internet gateway to VPC
aws ec2 attach-internet-gateway --vpc-id $vpcId --internet-gateway-id $igId

# Creating Route Table
echo "Route Table ID of the newly created Route Table:";
aws ec2 create-route-table --vpc-id $vpcId --query RouteTable.RouteTableId --output text

echo "Enter Route Table ID from above:";
read rId;

# Route all traffic to the internet gateway
aws ec2 create-route --route-table-id $rId --destination-cidr-block 0.0.0.0/0 --gateway-id $igId
# Associate route with the created subnet
aws ec2 associate-route-table  --subnet-id $subId --route-table-id $rId
# Map public IP when the instance is launched automatically
aws ec2 modify-subnet-attribute --subnet-id $subId --map-public-ip-on-launch

This code segment creates an internet gateway using the aws ec2 create-internet-gateway command and prompts the user to enter the internet gateway ID. Then, the script attaches the internet gateway to the VPC using aws ec2 attach-internet-gateway. Next, it creates a route table and associates it with the subnet. Finally, it modifies the subnet attributes to map a public IP when instances are launched within the subnet.

2.3. Making the VPC and Subnet Private

To make the VPC and subnet private, we need to create an internet gateway and a route table. Add the following code:

shellCopy codeecho "Making the VPC and Subnet private----------";

echo "Internet Gateway ID of the newly created internet gateway";
aws ec2 create-internet-gateway --query InternetGateway.InternetGatewayId --output text
echo "Enter Internet Gateway ID";
read igId;

# Attach the internet gateway to VPC
aws ec2 attach-internet-gateway --vpc-id $vpcId --internet-gateway-id $igId

# Creating Route Table
echo "Route Table ID of the newly created Route Table:";
aws ec2 create-route-table --vpc-id $vpcId --query RouteTable.RouteTableId --output text

echo "Enter Route Table ID from above:";
read rId;

# Route all traffic to the internet gateway
aws ec2 create-route --route-table-id $rId --destination-cidr-block 0.0.0.0/0 --gateway-id $igId
# Associate route with the created subnet
aws ec2 associate-route-table  --subnet-id $subId --route-table-id $rId
# Map public IP when the instance is launched automatically
aws ec2 modify-subnet-attribute --subnet-id $subId --map-public-ip-on-launch

This code segment creates an internet gateway using the aws ec2 create-internet-gateway command and prompts the user to enter the internet gateway ID. Then, the script attaches the internet gateway to the VPC using aws ec2 attach-internet-gateway. Next, it creates a route table and associates it with the subnet. Finally, it modifies the subnet attributes to map a public IP when instances are launched within the subnet.

2.4. Creating a Custom Security Group

Security groups play a vital role in controlling inbound and outbound traffic for EC2 instances. Add the following code:

shellCopy codeecho "Creating Security Group----------";
echo "Security group ID of the newly created Security Group";
aws ec2 create-security-group --group-name "my-sec-grps"  --description "my custom security group" --vpc-id $vpcId
echo "Enter Security group ID:";
read sgId;

echo "Adding TCP, SSH, HTTP Protocol as inbound rule--------------";
# Add Rule to security group
aws ec2 authorize-security-group-ingress --group-id $sgId  --protocol tcp --port 22 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id $sgId  --protocol icmp --port -1 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id $sgId --protocol tcp --port 80 --cidr 0.0.0.0/0

In this code snippet, the script creates a custom security group using the aws ec2 create-security-group command and prompts the user to enter the security group ID. Then, it adds inbound rules to the security group to allow TCP traffic on port 22 (SSH), ICMP traffic, and TCP traffic on port 80 (HTTP).

2.5. Launching EC2 Instances

Finally, the script automates the launch of EC2 instances. Add the following code:

shellCopy codeecho "Adding userdata-------------";
cat /home/vkaul/httpds.sh > /home/vkaul/data.txt

echo "Launching EC2 instance------------------";
# Launching EC2 Instance
echo "Enter No of Instances:";
read no;
echo "Enter Instances Type: ";
read instancetype;

aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text > MyKeyPair.pem
chmod 400 MyKeyPair.pem
echo "Key pair created"

aws ec2 run-instances --image-id resolve:ssm:/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 --count $no --instance-type $instancetype --key-name MyKeyPair --security-group-ids $sgId  --subnet-id $subId --associate-public-ip-address --user-data file://data.txt

echo "Our EC2 Instance is launched successfully";

echo "Reviewing instance details:";
aws ec2 describe-instances

This code segment creates a file named data.txt and adds the contents of /home/vkaul/httpds.sh to it. It then prompts the user to enter the number of instances to launch and the instance type. Next, it creates a key pair using the aws ec2 create-key-pair command and sets the necessary permissions on the private key file. Finally, it launches the EC2 instances using the aws ec2 run-instances command, specifying the image ID, instance count, instance type, key pair, security group, subnet, and user data.

Section 3: Downloading the Script and Running it

3.1. Modifying the Script

Before running the script, make the necessary modifications based on your requirements. Update the VPC and subnet details, security group rules, and any other parameters you wish to customize.

3.3. Running the Script

To launch EC2 instances using the script, follow these steps:

  • Open a terminal or command prompt.

  • Navigate to the directory where the script is saved.

  • Run the command to make the script executable.

      chmod +x launch_ec2.sh
    
  • Execute the script using the command

      ./launch_ec2.sh.
    
  • Follow the prompts and input the required information when prompted.

Conclusion:

In this blog post, we explored the process of automating the launch of EC2 instances using the AWS CLI and shell scripting. By leveraging the power of automation, you can streamline your infrastructure provisioning, save time, and reduce manual errors. We covered the steps to download and configure the AWS CLI, examined the shell script code, and discussed its various components. Now, armed with this knowledge, you can take your infrastructure automation to the next level. Start automating and enjoy the benefits of a more efficient and scalable AWS environment.

I hope you found this blog post helpful. If you have any questions or feedback, feel free to leave a comment below. Happy automating!