Clouder AWS

Creating AWS EC2 Instances using Infrastructure as Code (IaC) with AWS CLI and JSON Configuration

In this article, we’ll explore how to automate the deployment of EC2 instances using Infrastructure as Code (IaC) on AWS. By leveraging the AWS Command Line Interface (CLI) along with a JSON configuration file, we’ll create multiple EC2 instances with consistent settings and metadata. This approach allows for repeatable and scalable infrastructure management.

Introduction to AWS EC2 Instances

An EC2 (Elastic Compute Cloud) instance is a virtual server in Amazon’s cloud, designed to provide secure, scalable compute capacity. In our IaC example, we’ll set up multiple EC2 instances based on specifications provided in a JSON configuration file, simplifying deployment and ensuring uniformity across instances.

Key Benefits:

  • Region Specificity: Each instance can be deployed in a specified AWS region.
  • Tagging for Management: Tags enable effective tracking and management, such as identifying instances by cost center, project team, or environment.
  • Automated Deployment: Using a JSON configuration with the AWS CLI ensures instances are consistently configured with the right specifications.

AWS CLI Script to Deploy EC2 Instances

The provided deploy-ec2.sh script automates the creation of EC2 instances based on input from a JSON configuration file, parameters-ec2.json, which specifies the required parameters such as instance type, key name, region, tags, and security group.


AWS CLI Script Breakdown (deploy-ec2.sh):

#!/bin/bash

# Define the parameters file path
parametersFile="./parameters-ec2.json"

# Load parameters from JSON file
region=$(jq -r '.region' $parametersFile)
instanceType=$(jq -r '.instanceType' $parametersFile)
keyName=$(jq -r '.keyName' $parametersFile)
securityGroup=$(jq -r '.securityGroup' $parametersFile)
amiId=$(jq -r '.amiId' $parametersFile)
tags=$(jq -r '.tags | to_entries | map("\(.key)=\(.value)") | join(",")' $parametersFile)
instances=$(jq -c '.instances[]' $parametersFile)

# Set the AWS region
aws configure set region $region

# Loop through instances in the JSON file and create each EC2 instance
for instance in $instances; do
  name=$(echo $instance | jq -r '.name')
  
  echo "Creating EC2 instance: $name in $region"
  
  aws ec2 run-instances \
    --image-id $amiId \
    --instance-type $instanceType \
    --key-name $keyName \
    --security-groups $securityGroup \
    --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$name},$tags]"
    
  echo "EC2 instance $name created successfully."
done

Explanation of the Script

Parameters File Path

  • parametersFile: Specifies the path to the JSON file containing configuration data.

Loading JSON Parameters

  • Uses jq, a lightweight and flexible JSON processor, to parse the JSON file and retrieve values for the region, instance type, key pair, security group, and AMI ID.

AWS Region Configuration

  • The aws configure set region $region command sets the region context for the AWS CLI.

EC2 Instance Creation Loop

  • For each instance configuration defined in the JSON file, the script uses aws ec2 run-instances to create an EC2 instance with the defined specifications, applying tags and setting the name for easy identification.

JSON Parameter File Breakdown (parameters-ec2.json):

{
  "region": "us-east-1",
  "instanceType": "t2.micro",
  "keyName": "your-key-pair",
  "securityGroup": "default",
  "amiId": "ami-0abcdef1234567890",
  "tags": {
    "CostCenter": "Finance",
    "Owner": "Project Team",
    "Environment": "Production"
  },
  "instances": [
    {
      "name": "web-server-01"
    },
    {
      "name": "web-server-02"
    },
    {
      "name": "app-server-01"
    },
    {
      "name": "app-server-02"
    }
  ]
}

Explanation of the JSON File

  • Region: Specifies the AWS region in which the instances are created (e.g., us-east-1).
  • Instance Type: Defines the EC2 instance type, such as t2.micro, suitable for lightweight applications or testing.
  • Key Pair: Provides the name of the SSH key for accessing the EC2 instances.
  • Security Group: Assigns a security group to manage network access.
  • AMI ID: Defines the Amazon Machine Image (AMI) to use for the instances.
  • Tags: Sets tags such as CostCenter, Owner, and Environment to help with resource organization and billing.
  • Instances: Specifies the names of the EC2 instances to be created.

Conclusion

With AWS CLI and a JSON configuration file, creating multiple EC2 instances becomes a repeatable, efficient process. This approach to IaC enhances reliability, scalability, and consistency, making it ideal for DevOps practices and large-scale deployments.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.