Introduction to Terraform and Infrastructure as Code (IaC)

In cloud computing, automating infrastructure deployment is crucial for efficiency and scalability. Terraform, an open-source tool by HashiCorp, is a powerful solution. It enables you to define and manage your cloud resources in a human-readable configuration language called HashiCorp Configuration Language (HCL). This practice is called Infrastructure as Code (IaC), where infrastructure is managed through code, allowing for version control, collaboration, and repeatable deployments.

Setting Up Your Environment: Installing Terraform and AWS CLI

Before diving into Terraform, you need to set up your environment. This involves installing Terraform and the AWS Command Line Interface (CLI) on your local machine.

  1. Install Terraform:
    • Visit the official Terraform website and download the appropriate package for your operating system.
    • Follow the installation instructions to add Terraform to your system’s PATH.
  2. Install AWS CLI:
    • Follow the AWS CLI installation guide to download and install the AWS CLI.
    • Once installed, configure the AWS CLI with your credentials using aws configure.

Configuring AWS Credentials for Terraform

Terraform needs to interact with your AWS account to create resources. Doing this relies on your AWS credentials. Ensure your AWS credentials are correctly configured by running the following:

aws configure

Enter your AWS Access Key ID, Secret Access Key, region, and output format when prompted.

Writing Your First Terraform Script for EC2 Instance Creation

Now that your environment is set up, it’s time to write your first Terraform script. We’ll start with a simple script to create an EC2 instance.

Create a new directory for your Terraform project, and inside it, create a file named main.tf with the following content:

provider “aws” {

  region = “us-west-2”

}

resource “aws_instance” “example” {

  ami           = “ami-0c55b159cbfafe1f0”

  instance_type = “t2.micro”

  tags = {

    Name = “TerraformExampleInstance”

  }

}

This script defines an EC2 instance in the us-west-2 region using the specified Amazon Machine Image (AMI) and instance type.

Initializing Your Terraform Project

Before applying your Terraform script, you need to initialize your Terraform project. This process downloads the necessary provider plugins, such as AWS.

Run the following command in your project directory:

terraform init

Validating and Formatting Your Terraform Configuration

You can validate and format your script to ensure your Terraform configuration is correct and follows best practices.

  • Validation: Run terraform validate to check your script for syntax errors.
  • Formatting: Run terraform fmt to format your configuration according to the standard Terraform style.

Previewing Changes with Terraform Plan

Before applying your configuration, previewing the changes Terraform will make is a good practice. The terraform plan command shows you a detailed preview of what resources will be created, modified, or destroyed.

Run the following command:

terraform plan

Review the output to ensure everything is as expected.

Applying Your Terraform Configuration to Launch an EC2 Instance

Once satisfied with the plan, apply your configuration to create the resources.

terraform apply

Terraform will ask for confirmation before proceeding. Type yes to apply the changes.

Verifying EC2 Instance Creation on AWS Console

After the application process is complete, you can verify the creation of your EC2 instance by logging into the AWS Management Console. Navigate to the EC2 dashboard, and you should see your instance running.

Cleaning Up: Destroying Resources Created by Terraform

Once you’ve completed your infrastructure, cleaning up is essential to avoid unnecessary costs. Terraform makes this easy with the terraform destroy command.

Run the following command:

terraform destroy

When prompted, confirm the destruction of resources. This will delete all the resources Terraform created, leaving your environment clean.

References

Automate Microsoft web application deployments with GitHub Actions and Terraform

Deploy and manage AWS Control Tower controls by using Terraform