Terraform is widely used to manage cloud infrastructure as code. It works by using providers, which connect Terraform to different platforms like AWS, Azure, and others. One of the most common providers is the Terraform AWS Provider, which lets you create and manage AWS resources directly from your Terraform files.
If you’re running Terraform on AWS, this provider is what makes that possible. It helps automate everything from EC2 and S3 to VPCs, IAM roles, and more. In this guide, we’ll walk through what the AWS provider is, how to set it up, and some best practices to follow.
What is a Terraform Provider in Terraform on AWS?
A wide array of organizations heavily utilize Terraform for its ability to support diverse infrastructure needs distributed over different cloud providers. A Terraform Provider is a plugin that serves as Terraform’s interface for managing their external APIs. Depending on your requirements, you can choose from official providers such as AWS, Azure, and GCP, or third-party and community-maintained providers for platforms like Kubernetes, Oracle, Alibaba and ControlMonkey and more. This flexibility lets you tailor your infrastructure solutions across different technologies and environments.
Terraform uses plug-in-based architecture to work with hundreds of such infrastructure platforms. Terraform providers are distributed by HashiCorp and its publicly available on Terraform registry or OpenTofu Registry. There are 3 tiers of providers.
- Official Provider: Owned and maintained by HashiCorp and includes major cloud providers AWS, Azure, and GCP.
- Partner Provider: Owned and maintained by a 3rd party technology company that has gone through a partner provider process. e.g. Heroku, Digital Ocen, etc.
- Community Providers: Published and maintained by individual contributors of the community.
Terraform AWS Provider: Overview & Key Features
The Terraform AWS Provider plugin specifically enables interaction with AWS resources. It creates a connection between Terraform and the AWS Cloud, allowing you to define AWS infrastructure (such as EC2 instances, S3 buckets, or VPCs) in your Terraform configuration files.
Using Terraform on AWS: Setup Guide
Terraform AWS provider is central to provisioning and managing cloud resources in an automated way. As a Terraform admin, you will go through the following steps,
- Step1: Install Terraform CLI: Install Terraform CLI on your local computer.
- Step 2: Initialize the Provider: When you run terraform init, Terraform automatically downloads the AWS provider plugin.
- Step 3: Define AWS Resources: In your configuration files (.tf files), specify which AWS resources you want to create.
- Step 4: Validate, Plan, Apply: Use terraform validate, terraform plan, and terraform apply to review and deploy your changes. Terraform communicates with AWS APIs via the provider to create or update infrastructure.
How to Set Up Terraform AWS Provider
- Step 1: Download the latest version of Terraform from terraform.io and add it to your system path.
- Step 2: Download and configure AWS CLI. Enter the below command, and give the access key, secret access key, region, and output format in JSON.
- Step 3: aws configure – Create a Terraform configuration file
Create a file named main.tf with the AWS provider block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# Configure the AWS Provider provider "aws" { region = "us-east-1" } resource "aws_vpc" "demo_vpc" { cidr_block = "10.0.0.0/16" tags = { name = "demo_vpc" } } |
- Step 4: Initialize and Deploy – Run the “terraform init” command for initializing your backend. After that, you can run the plan for infrastructure changes, and finally apply them to AWS.
terraform init
terrafor plan
terraform apply

Terraform AWS provider Features and Benefits
A provider block is used to configure and set up authentication access from the provider plugin to the respective service provider. Below is a basic example of a provider block for Terraform provider specifying the cloud region. The block configures Terraform to use AWS and create resources in the region specified. For authentication, Terraform will automatically use credentials from your AWS CLI, environment variables (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY), or IAM roles.
1 2 3 |
provider "aws" { region = "us-east-1" # Set your AWS region } |
Resource Management with Terraform AWS:
With the AWS Provider, it’s easy to specify AWS resources in a few lines of code. Whether you need an EC2 instance, an S3 bucket, or a VPC, you can define everything in a declarative style. This approach makes your infrastructure easy to version, share, and reuse.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# main.tf provider "aws" { region = "us-east-1" } # Create a VPC resource "aws_vpc" "demo_vpc" { cidr_block = "10.0.0.0/16" tags = { Name = "demo-vpc" } } # Create a Subnet resource "aws_subnet" "demo_subnet" { vpc_id = aws_vpc.demo_vpc.id cidr_block = "10.0.1.0/24" tags = { Name = "demo-subnet" } } # Create an EC2 Instance resource "aws_instance" "demo_instance" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" subnet_id = aws_subnet.demo_subnet.id tags = { Name = "demo-instance" } } |
State Management for Terraform on AWS
The AWS Provider works seamlessly with Terraform’s state management. You can store your Terraform state remotely in AWS S3 and use DynamoDB for state locking, preventing concurrent modifications and helping avoid configuration drift. Also note that there are other backend options available, with the default being the local (Read More).
Example Configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# backend.tf terraform { backend "s3" { bucket = "terraform-state-bucket" # Pre-existing S3 bucket key = "prod/terraform.tfstate" # State file path region = "us-east-1" # Same as provider region dynamodb_table = "terraform-locks" # Pre-existing DynamoDB table encrypt = true # Encrypt state } } |
Modular Deployments with Terraform on AWS
Terraform configurations can be organized into modules. When combined with the AWS Provider, you can tackle large-scale deployments by breaking infrastructure definitions into reusable, maintainable modules.
# Configure the AWS Provider (inherited from root module)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
resource "aws_vpc" "demo-vpc" { cidr_block = var.cidr_block tags = { Name = var.name } } resource "aws_subnet" "public" { vpc_id = aws_vpc.demo-vpc.id cidr_block = cidrsubnet(var.cidr_block, 8, 1) # Creates subnet from VPC CIDR tags = { Name = "${var.name}-subnet" } } |
Environment Variables in Terraform AWS Setup
Export AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_REGION in your terminal.
1 2 3 4 5 6 7 |
# main.tf (no credentials in code) provider "aws" { region = "us-east-1" # Optional if AWS_REGION is set } |
Using Shared Credentials with Terraform AWS Provider
Terraform can automatically read from the default AWS CLI credentials file (~/.aws/credentials and ~/.aws/config).
1 2 3 4 5 6 7 8 9 |
provider "aws" { region = "us-east-1" shared_credentials_files = ["~/.aws/credentials"] # Default path profile = "dev-profile" # Non-default profile } |
Terraform AWS Provider Security Best Practices
- Don’t hardcode credentials:
- Never put AWS access keys or secrets in Terraform files. Instead, use environment variables or shared credentials files.
- # Use environment variables (no hardcoded secrets)
- provider “aws” {region = “us-east-1” }
- Never put AWS access keys or secrets in Terraform files. Instead, use environment variables or shared credentials files.
- Encrypt state files:
- When you store Terraform state in an S3 bucket, enable server-side encryption (SSE) and use KMS if possible. Also configure DynamoDB for state locking to prevent concurrent modifications.
1 2 3 4 5 6 7 8 9 10 11 |
#backend.tf terraform { backend "s3" { bucket = "terraform-state-bucket" key = "prod/terraform.tfstate" region = "us-east-1" dynamodb_table = "terraform-locks" encrypt = true kms_key_id = "alias/terraform-state-key" } } |
- Use sensitive variables:
- Prevent Terraform from recording sensitive values in plain text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
variable "aws_access_key" { type = string sensitive = true # Hides value in outputs } variable "aws_secret_key" { type = string sensitive = true # Hides value in outputs } provider "aws" { access_key = var.aws_access_key secret_key = var.aws_secret_key region = "us-east-1" } |
Conclusion: Using Terraform on AWS Securely
Terraform AWS Provider is one of the key solutions to successfully manage cloud infrastructure using Infrastructure as Code (IaC) practice. It enables you to easily get automation for your AWS infrastructure provisioning and management by integrating nicely with AWS services. You can ensure a secure and manageable infrastructure by adopting best practices like remote state management, IAM role-based authentication, and modular configurations.
FAQ: Terraform on AWS & More
Q: How do I authenticate Terraform AWS Provider?
A: Terraform AWS Provider can be authenticated using environment variables, shared credentials files, IAM roles, and AWS CLI profiles.
Q: Can I use Terraform AWS Provider for multi-account setups?
A: Yes, you can have multiple accounts configured using different provider aliases in Terraform.
Q: How does Terraform manage state files?
A: Terraform uses state files to track resource changes. It is recommended to store state files remotely, for example, in AWS S3 with state locking using DynamoDB, to prevent conflicts in a team environment.
Q: What are the benefits of Terraform modules?
A: Terraform modules promote reusability, maintainability, and consistency in infrastructure code, allowing teams to manage complex deployments better.
Q: Can I use Terraform AWS Provider with CI/CD pipelines?
A: You can use Terraform with CI/CD pipelines like GitHub Actions, Jenkins, and AWS CodePipeline to provision and modify infrastructure programmatically.
Q: How do I get the Terraform AWS Provider up to date?
A: To update the provider, modify the provider version within your Terraform configuration and run terraform init -upgrade. Terraform AWS Provider automates infrastructure management by providing scalability, security, and automation. It can easily integrate with AWS services to offer smooth deployment and management of cloud infrastructure. With Terraform AWS Provider, organizations can achieve consistency, reduce manual intervention, and maximize cloud governance.