Video Thumbnail for Lesson
2.2: Setting up Terraform with AWS

Install and Set up Terraform with AWS

In this lesson, we will guide you through the process of installing Terraform, authenticating with AWS, and creating a basic configuration to provision a virtual machine on AWS.

We will also demonstrate how to use Terraform commands to initialize, plan, apply, and destroy resources.

Installing Terraform:

  • For macOS users, use Homebrew to install Terraform by running brew install terraform.

  • For other operating systems, visit the HashiCorp Terraform installation page and follow the instructions for your specific OS.

Authenticating with AWS:

  1. Create a user with the necessary IAM roles for your project. In this example, we used the following permissions:

    • RDS access (AmazonRDSFullAccess)
    • EC2 access (AmazonEC2FullAccess)
    • IAM role management (IAMFullAccess)
    • S3 access (AmazonS3FullAccess)
    • DynamoDB access (AmazonDynamoDBFullAccess)
    • Route 53 access (AmazonRoute53FullAccess)
  2. Install the AWS Command Line Interface (CLI) by following the instructions on the AWS CLI installation page.

  3. Run aws configure and enter your access key ID, secret access key, and default region. This will create a credentials file in your home directory at ~/.aws/credentials.

Creating a Basic Terraform Configuration:

  1. Create a file named main.tf with the following content:
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}

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

resource "aws_instance" "example" {
  ami           = "ami-011899242bb902164" # Ubuntu 20.04 LTS // us-east-1
  instance_type = "t2.micro"
}
  1. This basic configuration specifies the AWS provider and an EC2 instance resource using an Ubuntu 20.04 AMI and the t2.micro instance type.

Using Terraform Commands:

  1. Initialize Terraform in the directory containing main.tf by running terraform init. This sets up the backend and state storage.
  2. Run terraform plan to view the changes Terraform will make to your infrastructure.
  3. Run terraform apply to create the specified resources. Confirm the action when prompted.
  4. To clean up resources and avoid unnecessary costs, run terraform destroy and confirm the action when prompted.

By following these steps, you have installed Terraform, authenticated with AWS, and created a basic configuration to provision a virtual machine on AWS!

You also learned how to use Terraform commands to manage your infrastructure. In the next lessons, we will explore more Terraform features and build out our infrastructure using it.