Infrastructure as Code with Terraform on AWS

Nayana Dharmasiri
4 min readJul 4, 2023

--

Infrastructure as Code (IaC) is a practice that involves managing and provisioning infrastructure resources using machine-readable definition files rather than manual processes. Terraform is a popular tool for implementing IaC, and it provides a way to define and create infrastructure resources across various cloud providers, including Amazon Web Services (AWS).

To use Terraform with AWS, you’ll need to perform the following steps:

  1. Install Terraform
  2. Configure AWS credentials
  3. Create a Terraform configuration file
  4. Define AWS provider
  5. Define resources
  6. Initialize Terraform
  7. Preview the changes
  8. Apply the changes
  9. Destroy the infrastructure
Image copyright: https://developer.hashicorp.com/terraform/tutorials/aws-get-started/infrastructure-as-code

Install Terraform

Download and install Terraform from the official website. Make sure it is added to your system’s PATH.

Try the below command to verify the installation

To verify installation worked by listing terraform available subcommands try below command

terraform -help

Configure AWS credentials

Create an AWS account if you don’t have one. Then, generate an Access Key ID and Secret Access Key from the AWS Identity and Access Management (IAM) console. Configure the AWS CLI or set environment variables with these credentials, so Terraform can interact with AWS on your behalf.

Add a new user details by clicking Create user under IAM service.

To run the terraform samples in this blog series, the easiest way to get started is to add the AdministratorAccess Managed Policy to IAM user

Once you create user AWS will show you the security credentials for that user, which consist of an Access Key ID and a Secret Access Key, as shown below.

Terraform able to make changes in your AWS account. You need to set the AWS credentials for IAM user which you created earlier as enviornment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY . Follow the below commands do it in a Unix/ Linux/ MacOS terminals.

$ export AWS_ACCESS_KEY_ID=(your access key id)
$ export AWS_SECRET_ACCESS_KEY=(your secret access key)

Find below commands for the windows terminal

$ set AWS_ACCESS_KEY_ID=(your access key id)
$ set AWS_SECRET_ACCESS_KEY=(your secret access key)

These enviornment variables only apply to the current shell, once you reboot computer or open new terminal, you need to export again these variables.

Apart from enviornment variables, Terraform supports same authentications mechanism as AWS CLI and SDK. It will also be able to use credentials in $HOME/.aws/credentials . This will automatically generated by execute aws configure command.

Create a Terraform configuration file

Create a file with a .tf extension, such as main.tf, to define your infrastructure resources and their configuration. This file will contain the Terraform code that specifies what resources you want to create and how they should be configured.

Define AWS provider

Add a provider block to your configuration file to specify that you’re using AWS as the cloud provider. You’ll need to provide your AWS region and any necessary authentication information.

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

Define resources

Use Terraform’s resource blocks to define the AWS resources you want to create. For example, to create an AWS EC2 instance, you can add the following code

resource "aws_instance" "app_server" {
ami = "ami-024fc608af8f886bc"
instance_type = "t2.micro"

tags = {
Name = "NayanaTerraformEC2"
}
}

Initialize Terraform

In the directory containing your configuration file, run the terraform initcommand. This will initialize Terraform and download any necessary provider plugins.

Preview the changes

Run theterraform plancommand to see a preview of the changes that Terraform will make to your infrastructure. It will show you which resources will be created, modified, or destroyed.

Apply the changes

Once you’re ready to create or update your infrastructure, run the terraform applycommand. Terraform will create or modify the necessary resources based on your configuration. You’ll be prompted to confirm the changes before they are applied.

Destroy the infrastructure

Using terraform destroy command can destroy the entire Terraform managed infrastructure.

These are the basic steps to get started with Terraform and AWS. However, Terraform offers a rich set of features for managing infrastructure, including modules, variables, and remote state management. I recommend referring to the official Terraform documentation for detailed information on working with AWS resources and using advanced features.

--

--