Terraform with AWS : Provisioning EC2 Instances Made Easy

Terraform with AWS : Provisioning EC2 Instances Made Easy


Introduction

Provisioning resources on AWS can be a daunting task, but with Terraform, it's straightforward and efficient.

Terraform is an open-source infrastructure as code tool created by HashiCorp that allows you to define and manage your cloud infrastructure using a simple configuration language.

In this guide, we will walk you through the steps to provision an EC2 instance on AWS using Terraform.

Prerequisites

Before we start, make sure you have the following:

  1. AWS CLI Installed : The AWS Command Line Interface (CLI) lets you manage AWS services from the command line and automate them through scripts. You can download and configure the AWS CLI from here.

  2. AWS IAM User : AWS Identity and Access Management (IAM) helps you securely control access to AWS resources. You'll need an IAM user with sufficient permissions to create and manage EC2 instances.

  3. Terraform Installed : Download and install Terraform.

Setting Up AWS Credentials

To connect your AWS account with Terraform, you need to export your AWS access keys to your machine. Replace <access key> and <secret access key> with your actual keys:

export AWS_ACCESS_KEY_ID=<access key>
export AWS_SECRET_ACCESS_KEY=<secret access key>

Installing Required Providers

Terraform uses providers to interact with different cloud services. For AWS, we use the AWS provider. Here’s how you can configure it in your Terraform script:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }
  required_version = ">= 1.2.0"
}

Specifying the AWS Region

Define the region where you want to deploy your instances. For this guide, we will use the us-east-1 region:

provider "aws" {
  region = "us-east-1"
}

Task 01 : Provisioning an AWS EC2 Instance

In this task, we will provision an EC2 instance using Terraform. Here is the Terraform configuration script:

resource "aws_instance" "aws_ec2_test" {
  count         = 4
  ami           = "ami-08c40ec9ead489470"
  instance_type = "t2.micro"
  tags = {
    Name = "TerraformTestServerInstance"
  }
}

Detailed Solution Explained

  1. AWS CLI Configuration:

    • Install the AWS CLI: Follow the instructions on the AWS CLI Installation Guide to install the AWS CLI.

    • Configure the AWS CLI: Open your terminal and run aws configure. You will be prompted to enter your AWS Access Key ID, AWS Secret Access Key, default region name (e.g., us-east-1), and default output format (e.g., json).

        aws configure
      
  2. Create a Terraform Configuration File:

    • Create a new directory for your Terraform project. Inside the directory, create a file named main.tf.

    • Add the following content to main.tf:

        terraform {
          required_providers {
            aws = {
              source  = "hashicorp/aws"
              version = "~> 4.16"
            }
          }
          required_version = ">= 1.2.0"
        }
      
        provider "aws" {
          region = "us-east-1"
        }
      
        resource "aws_instance" "aws_ec2_test" {
          count         = 4
          ami           = "ami-08c40ec9ead489470"
          instance_type = "t2.micro"
          tags = {
            Name = "TerraformTestServerInstance"
          }
        }
      
  3. Initialize Terraform:

    • In your terminal, navigate to your project directory and run the terraform init command. This command initializes the project and downloads the necessary provider plugins.

        terraform init
      
  4. Plan the Deployment:

    • Run terraform plan to see a preview of the resources that Terraform will create. This step helps ensure that everything is configured correctly before making any changes.

        terraform plan
      
  5. Apply the Configuration:

    • Run terraform apply to create the EC2 instances. Terraform will show you a summary of the changes and prompt you to confirm. Type yes to proceed.

        terraform apply
      
  6. Verify the Instances:

    • After the apply command completes, go to the AWS Management Console, navigate to the EC2 Dashboard, and verify that the instances have been created. You should see four t2.micro instances with the tag Name: TerraformTestServerInstance.

Detailed Explanation of Terraform Script

  • terraform block : This block specifies the required providers and the minimum version of Terraform needed to run this configuration. We are using the AWS provider version ~> 4.16.

  • provider block : This block configures the AWS provider with the region where we want to deploy our resources, which in this case is us-east-1.

  • resource block : This block defines an AWS EC2 instance resource.

    • resource "aws_instance" "aws_ec2_test": This defines a resource of type aws_instance with the name aws_ec2_test.

    • count = 4: This creates four instances of the defined resource.

    • ami = "ami-08c40ec9ead489470": This specifies the Amazon Machine Image (AMI) ID to use for the instances. You can find the AMI ID for your region in the AWS Management Console.

    • instance_type = "t2.micro": This specifies the instance type to use. t2.micro is a free tier eligible instance type suitable for low-traffic workloads.

    • tags = { Name = "TerraformTestServerInstance" }: This adds a tag to the instances with the key Name and value TerraformTestServerInstance.

Conclusion

By following these steps, you have successfully provisioned EC2 instances on AWS using Terraform.

This guide provided a comprehensive walkthrough, ensuring that even new learners can understand and apply the concepts.

Terraform is a powerful tool that simplifies infrastructure management, making it easier to deploy, manage, and scale resources in a reproducible manner. With Terraform, you can manage your infrastructure as code, ensuring consistency and enabling version control. Happy provisioning!