Home How to Use Terraform With AWS
Post
Cancel

How to Use Terraform With AWS

In this post, we will be discussing how to use HashiCorp Terraform to connect to Amazon Web Services (AWS). Terraform is a popular open-source tool that allows users to define and provision infrastructure as code. By using Terraform, you can easily manage and provision your AWS resources in a consistent and repeatable way. In this post, we will go over the basics of setting up Terraform and connecting it to your AWS account.

Prerequisites

Before getting started, you will need to have an AWS account and have the AWS CLI installed on your machine. You will also need to have Terraform installed. If you do not have Terraform installed, you can download it from the HashiCorp website. If you are new to Terraform, I recommend checking out my Beginners Guide to Terraform post first, which will help you learn the fundamentals of Terraform before proceeding.

Setting up Terraform

The first step in using Terraform to connect to AWS is to set up the provider. The provider is what connects Terraform to the specific cloud platform, in this case, AWS. To set up the provider, you will need to create a file called “provider.tf” in the root of your Terraform project. In this file, you will need to include the following code:

1
2
3
4
5
6
7
8
9
10
11
12
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "<Version Number>"
    }
  }
}

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

This code tells Terraform to use the specified version of the AWS provider and to connect to the “us-east-1” region by default. You can change the region to match the region you want to use.

Authenticating with AWS

Once the provider is set up, you will need to authenticate Terraform with your AWS account. There are a few different ways to do this, but the easiest way is to use the AWS CLI. To do this, you will need to run the following command:

1
aws configure --profile <profile-name>

This command will prompt you for your AWS access key and secret key. Once you have entered these values, a profile will be set up on the machine in the shared credentials file using the profile name specified in the command. Terraform will now be able to connect to your AWS account by specifying the profile attribute in the AWS provider block.

1
2
3
4
5
6
7
8
9
10
11
12
13
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "<Version Number>"
    }
  }
}

provider "aws" {
  region  = "us-east-1"
  profile = <profile-name> 
}

If the AWS account being used is managed under an AWS Organization that is using AWS IAM Identity Center (formerly known as AWS SSO), profile credentials are generated differently than those of their IAM user counterparts. For this reason, a different workflow was created for Identity Center users to create AWS Profiles. If you are using AWS Identity Center for AWS access, you should check out my guide How to Get Programmatic Access to AWS Using AWS Identity Center

When setting up the AWS Profile, if the profile name default is used, then Terraform and the AWS CLI will treat that profile as the default value, and you will not need to specify the profile in the AWS provider block or in AWS CLI commands.

Provisioning Resources

Now that Terraform is set up and authenticated with your AWS account, you can start provisioning resources. To do this, you will need to create a file called “main.tf” in the root of your Terraform project. This file is where you will define the resources you want to provision.

For example, to provision an EC2 instance, you would include the following code in your “main.tf” file:

1
2
3
4
resource "aws_instance" "example" {
  ami           = "ami-0ff8a91507f77f867"
  instance_type = "t2.micro"
}

This code tells Terraform to create an EC2 instance using the specified Amazon Machine Image (AMI) and instance type. EC2 instances are just one of the hundreds of resources that can be created in AWS using Terraform. For more information about the available resources and more, check out the Terraform AWS Provider documentation.

Before you start provisioning resources, be sure to understand the cost and the best practices to manage your infrastructure and avoid unnecessary charges.

In this post, we have discussed how to use Terraform to connect to AWS and provision resources. By using Terraform, you can easily manage and provision your AWS resources in a consistent and repeatable way. With Terraform, you can also version control your infrastructure and collaborate with your team.

I hope this post was helpful and you can start using Terraform to manage your AWS resources. Happy provisioning!

Author’s Note: This post was created in collaboration with OpenAI’s GPT-3. It helped me to come up with the ideas and structure of this post, but all the opinions and thoughts expressed here are mine.

This post is licensed under CC BY 4.0 by the author.
Contents