Terraform
ResourceExamples
Provider

AWS Provider Documentation for Terraform

1. Introduction

The AWS provider in Terraform allows you to manage AWS resources. It requires authentication to communicate with AWS services and supports a wide range of AWS services and configurations.

2. Provider Block

Basic Configuration:

provider "aws" {
  region  = "us-west-2"
  version = "~> 4.0" # Specify the provider version
}

Explanation:

  • region: Specifies the AWS region to use. Replace "us-west-2" with your desired region.
  • version: Optional. Defines the version of the AWS provider to use. Adjust to your specific needs.

3. Authentication Methods

1. Environment Variables

You can set the following environment variables for authentication:

export AWS_ACCESS_KEY_ID="your-access-key-id"
export AWS_SECRET_ACCESS_KEY="your-secret-access-key"
export AWS_SESSION_TOKEN="your-session-token" # Optional, for temporary credentials

2. AWS Credentials File

Create or update the ~/.aws/credentials file:

[default]
aws_access_key_id     = your-access-key-id
aws_secret_access_key = your-secret-access-key

3. AWS Config File

Create or update the ~/.aws/config file:

[default]
region = us-west-2

4. Explicitly in the Provider Block

You can also pass credentials directly in the provider block, though this is not recommended for security reasons:

provider "aws" {
  region  = "us-west-2"
  access_key = "your-access-key-id"
  secret_key = "your-secret-access-key"
}

4. Common Provider Arguments

1. region

Specifies the AWS region to work with.

provider "aws" {
  region = "us-west-2"
}

2. access_key and secret_key

For providing credentials directly (use environment variables or credentials file for better security):

provider "aws" {
  access_key = "your-access-key-id"
  secret_key = "your-secret-access-key"
  region     = "us-west-2"
}

3. profile

Specifies a named profile from your AWS credentials file.

provider "aws" {
  profile = "my-profile"
  region  = "us-west-2"
}

4. assume_role

To assume an IAM role in AWS:

provider "aws" {
  region  = "us-west-2"
  assume_role {
    role_arn = "arn:aws:iam::123456789012:role/role-name"
    session_name = "terraform"
  }
}

5. shared_credentials_file

Path to the AWS credentials file if it's not in the default location.

provider "aws" {
  shared_credentials_file = "/path/to/credentials"
  region                  = "us-west-2"
}

6. skip_credentials_validation

Skip validation of AWS credentials.

provider "aws" {
  region                      = "us-west-2"
  skip_credentials_validation = true
}

7. skip_requesting_account_id

Skip requesting the account ID, which is useful if you want to use an assumed role but don’t need the account ID.

provider "aws" {
  region                     = "us-west-2"
  skip_requesting_account_id = true
}

5. Provider Configuration Examples

1. Basic Configuration

provider "aws" {
  region  = "us-west-2"
}

2. Using Named Profile

provider "aws" {
  profile = "my-profile"
  region  = "us-west-2"
}

3. Using Role Assumption

provider "aws" {
  region  = "us-west-2"
  assume_role {
    role_arn = "arn:aws:iam::123456789012:role/role-name"
  }
}

4. Using Environment Variables

provider "aws" {
  region = "us-west-2"
}

Set environment variables before running Terraform commands:

export AWS_ACCESS_KEY_ID="your-access-key-id"
export AWS_SECRET_ACCESS_KEY="your-secret-access-key"

6. Provider Metadata and Versioning

1. Metadata

provider "aws" {
  region = "us-west-2"
  version = "~> 4.0"
}

2. Pinning Provider Version

terraform {
   required_providers {
     aws = {
       source = "hashicorp/aws"
       version = "5.58.0"
     }
   }
 }

3. Provider Documentation

You can find the official AWS provider documentation on Terraform Registry (opens in a new tab).

7. Debugging

1. Enabling Debug Mode

Set the TF_LOG environment variable to DEBUG to get detailed logs:

export TF_LOG=DEBUG

2. View Debug Logs

Run Terraform commands to see detailed logs:

terraform plan
terraform apply

8. Additional Resources


This guide provides an overview of configuring and using the AWS provider with Terraform. For specific needs or advanced configurations, refer to the official documentation and Terraform AWS provider resources. If you have any questions or need further assistance, feel free to ask! 🚀


🧙 AI Wizard - Instant Page Insights

Click the button below to analyze this page.
Get an AI-generated summary and key insights in seconds.
Powered by Perplexity AI!