Terraform is an open-source tool for defining and managing infrastructure as code (IaC). It allows you to write code that describes the desired state of your infrastructure, including servers, networks, storage, and other resources.

Terraform introduces many benefits to infrastructure management, including:

  • Infrastructure automation: Terraform automates the provisioning and management of infrastructure, reducing manual errors and improving consistency.
  • Repeatable infrastructure: Define your infrastructure once in Terraform code and easily replicate it across different environments (development, staging, production).
  • Version control: Terraform configuration files can be version controlled like any other code, allowing you to track changes and rollback to previous versions if necessary.
  • Multi-cloud support: Terraform supports a wide range of cloud providers and infrastructure platforms, allowing you to manage infrastructure across different environments.

Part 1: Getting Started with Terraform

  1. Create a new directory for your Terraform project and navigate to it using the terminal.
      mkdir terraform-project
      cd terraform-project
    
  2. Create a Terraform configuration file named main.tf to define your infrastructure.
      nano main.tf
    

You can use any text editor to create and edit the Terraform configuration file.

  1. Define an AWS EC2 instance in the main.tf file.

The AWS provider requires your AWS access key and secret key to authenticate with the AWS API. These credentials can be found in the AWS Management Console under “My Security Credentials”.

  provider "aws" {
    access_key = "YOUR_ACCESS_KEY"
    secret_key = "YOUR_SECRET_KEY"
    region     = "us-west-2"
  }

  resource "aws_instance" "example" {
    ami           = "ami-0c55b159cbfafe1f0"
    instance_type = "t2.micro"
  }

Replace YOUR_ACCESS_KEY and YOUR_SECRET_KEY with your AWS access key and secret key.

You can provide these credentials directly in the main.tf file, but it’s recommended to use environment variables or more secure methods to store sensitive information.

Part 2: Installing Terraform with Docker

The easiest way to use Terraform with Docker is to leverage the official Terraform Docker image provided by HashiCorp.

Terraform could be installed directly onto your local machine by downloading the binary from the official website and adding it to your PATH. However, using Docker allows you to isolate the Terraform environment and dependencies from your local machine.

  1. Pull the latest Terraform Docker image from the official repository.
      docker run --it `# run interactively` \
       --rm `# remove container after exit` \
       --name terraform `# name the container` \
       --volume $(pwd):/app/code `# mount the current directory to /app/code` \
       hashicorp/terraform:latest `# image name` \
       bash `# start a bash shell`
    

This command uses the -v flag to mount the current working directory $(pwd) on your host machine to the /app/code directory within the container. This allows you to access your local Terraform configuration files from within the container.

  1. Verify that Terraform is installed. You should see the Terraform version information printed to the console.
      terraform -v
    
  1. Navigate to the /app/code directory within the container to access your Terraform configuration files.
      cd /app/code
    
  1. Initialize the project and download the necessary providers and modules specified in the configuration file.
      terraform init
    
  1. Preview the changes that will be made to the infrastructure. This generates an execution plan that shows what actions Terraform will take to change the infrastructure to match the configuration.
      terraform plan
    
  1. Create the resources specified in the main.tf file. This command will create the AWS EC2 instance defined in the configuration file.
      terraform apply
    
  1. Confirm the action by typing yes when prompted. Terraform will begin creating the resources as defined in the configuration file.
  2. Once the resources have been created, verify the status of the EC2 instance in the us-west-2 AWS Management Console or using the AWS CLI.

You can also use the terraform show command to display the current state of the infrastructure managed by Terraform.

More Info