How to configure AWS infrastructure using Terraform

Amazon Web Services (AWS) infrastructure management can be difficult and time-consuming. AWS offers many different services, and setting them up by hand can be difficult to maintain and prone to errors. Provisioning and maintaining the AWS resources may be easier and more automated using Terraform, an effective infrastructure-as-code tool. In this Answer, we’ll walk through configuring AWS infrastructure with Terraform.

What is Terraform?

HashiCorp developed the open-source infrastructure-as-code utility Terraform. It enables us to use a declarative configuration languageA declarative configuration language defines system settings by specifying the desired state rather than procedural steps. to describe and provision infrastructure resources. Terraform allows us to specify the ideal state of your infrastructure and manages the process of adding, removing, or altering resources to bring it into compliance.

Prerequisites

Before getting started, we'll need the following:

  1. AWS account: We must have an AWS account and access key credentials.

  2. AWS CLI: Configure the AWS account, using aws configure command, through the console.

  3. Terraform installed: Install Terraform on the local machine by following the official documentationhttps://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli.

Steps to configure AWS infrastructure with Terraform

Here is a detailed step-by-step guide on deploying infrastructure on AWS using Terraform.

Step 1: Configure AWS CLI

Configure the AWS CLI using aws configure command and use your access and secret access key for configuration. Provide us-east-1 as region and use json as output format.

Step 2: Initialize the Terraform Configuration

Create a new folder/directory for your Terraform configuration and create a file named main.tf. This file will contain the configuration for your AWS infrastructure. Initialize the Terraform configuration by running:

terraform init

This command downloads the necessary provider plugins and sets up your working directory.

Step 3: Define AWS provider

In your main.tf file, define the AWS provider by specifying your AWS access key and secret access key. Make sure to store sensitive data like access keys securely, preferably using environment variables.

provider "aws" {
region = "us-east-1"
}
Command to initialize AWS provider

Step 4: Create AWS resources

Now, you can start defining your AWS resources. For example, let’s create a dynamoDB table. Add the following code to your main.tf:

resource "aws_dynamodb_table" "basic-dynamodb-table" {
name = "Users"
billing_mode = "PROVISIONED"
read_capacity = 20
write_capacity = 20
hash_key = "UserId"
range_key = "UserName"
attribute {
name = "UserId"
type = "S"
}
attribute {
name = "UserName"
type = "S"
}
tags = {
Name = "dynamodb-table-1"
Environment = "production"
}
}

This code creates a DynamoDB table with the tags mentioned in the code. You can find more resources on Terraform in the Terraform registry.

Step 5: Plan and apply changes

To observe the modifications Terraform will make to your infrastructure, run terraform plan. You can preview the resources that will be produced, altered, or removed with the aid of this command.

If everything in the plan seems good, you may use terraform apply and start the modifications. Before moving further, Terraform will ask you to validate the changes. Type yes for confirmation. It will generate the designated AWS resources after confirmation.

Step 6: Monitor and manage resources

Terraform can be used to manage and monitor your AWS resources. For instance, if you want to edit your DynamoDB table name, you can edit the configuration in your main.tf file and then rerun terraform apply. Terraform will make the necessary updates to the current resources.

Step 7: Destroy resources

When you no longer need the AWS resources, you can use Terraform to destroy them. Run terraform destroy, and Terraform will remove all the resources defined in your configuration.

Best practices

  • Version control: Store your Terraform configurations in version control systems like Git to track changes over time.

  • State management: Terraform employs state files to monitor your infrastructure’s condition. A remote state backend might be useful for improved durability and teamwork.

Test your code

To connect to your AWS CLI, use the following terminal command:

aws configure

Then, utilize the file main.tf located in the usercode directory to execute and follow the steps given above to implement the resources on the cloud. Use cat main.tf to see the file already created for you.
Use the terraform init command to initialize terraform in the usercode directory and then follow Step 5 and onward for deployment.

Terminal 1
Terminal
Loading...

Conclusion

Terraform is an effective tool for managing and configuring infrastructure on AWS, GCP, and Azure. We especially examined how defining your infrastructure as code simplifies the process of creating, updating, and destroying resources on AWS in a stable and reproducible manner in this answer. You may start using Terraform and streamline your AWS infrastructure management by following the instructions above.

To guarantee a seamless and safe experience, always use caution while changing your AWS resources and adhere to standard practices for infrastructure as code.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved