> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Setting Up AWS Credentials for Terraform

> Guide to creating an AWS IAM user, generating access keys, and configuring credentials for Terraform with example configuration and security best practices.

This guide walks through creating an IAM user in the AWS Management Console, generating programmatic credentials, and configuring those credentials so Terraform can authenticate to your AWS account.

Prerequisites:

* An AWS account and a user with permission to create IAM users and access keys.
* Terraform installed locally.
* (Optional) AWS CLI installed for managed credential storage.

1. Open the AWS Management Console

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/urHSIc4JjzJnqdR4/images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-console-home-page-services-overview.jpg?fit=max&auto=format&n=urHSIc4JjzJnqdR4&q=85&s=073a53e6d5c76582bfca6947fb0ba84b" alt="The image shows an AWS Console Home page, displaying a list of recently visited services and application management options, as well as sections for AWS Health, cost and usage, and getting started resources." width="1920" height="1080" data-path="images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-console-home-page-services-overview.jpg" />
</Frame>

2. Navigate to IAM

Open IAM from Recently visited services or type “IAM” in the top search box and open the IAM console.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/urHSIc4JjzJnqdR4/images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-iam-user-interface-management-dashboard.jpg?fit=max&auto=format&n=urHSIc4JjzJnqdR4&q=85&s=515c9136251df57002e69fa2f7038078" alt="The image shows an AWS Identity and Access Management (IAM) user interface with a list of users and details like last activity, password age, and console last sign-in times. There are navigation options for managing groups, roles, policies, and access reports on the left side." width="1920" height="1080" data-path="images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-iam-user-interface-management-dashboard.jpg" />
</Frame>

3. Create a new IAM user for Terraform

* Click “Users” in the left navigation, then “Create user”.
* Give the user a name (for example, `terraform`) — you can choose any naming convention you prefer.
* Enable programmatic access so AWS generates an access key ID and secret access key for the user.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/urHSIc4JjzJnqdR4/images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-iam-console-create-user-terraform.jpg?fit=max&auto=format&n=urHSIc4JjzJnqdR4&q=85&s=bf817d02dc287b5f4d905d352560186b" alt="The image displays the AWS IAM console where a user named &#x22;terraform&#x22; is being specified in the &#x22;Create user&#x22; process, within the &#x22;Specify user details&#x22; step. There are options for providing AWS Management Console access and generating programmatic access credentials." width="1920" height="1080" data-path="images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-iam-console-create-user-terraform.jpg" />
</Frame>

4. Attach permissions (least privilege recommended)

You can attach policies directly to the user on the permissions step. For the purposes of provisioning VPC resources (which typically do not incur charges), attach a managed policy that grants VPC permissions.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/urHSIc4JjzJnqdR4/images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-iam-console-permissions-policy.jpg?fit=max&auto=format&n=urHSIc4JjzJnqdR4&q=85&s=27f446ab9289e9a10f31bba7313cf1df" alt="The image shows the AWS IAM (Identity and Access Management) console where a user is setting permissions by selecting policy options. There's a section for permission policies with various listed policies that can be attached to a new user." width="1920" height="1080" data-path="images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-iam-console-permissions-policy.jpg" />
</Frame>

Search for “VPC” and choose **AmazonVPCFullAccess** (or attach `AdministratorAccess` only if you truly need full administrative scope). Limiting permissions to what Terraform requires is a best practice.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/urHSIc4JjzJnqdR4/images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-iam-console-vpc-permission-policies.jpg?fit=max&auto=format&n=urHSIc4JjzJnqdR4&q=85&s=c11a7b764ff1501639cfd632e86d42c3" alt="The image shows the AWS IAM console with a list of permission policies related to &#x22;VPC&#x22; being displayed. The policies are AWS managed, and none are currently attached to any entities." width="1920" height="1080" data-path="images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-iam-console-vpc-permission-policies.jpg" />
</Frame>

After creating the user, verify the policy is attached on the user’s permissions page.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/urHSIc4JjzJnqdR4/images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-iam-user-permissions-amazonvpc.jpg?fit=max&auto=format&n=urHSIc4JjzJnqdR4&q=85&s=7a29163b490cc64309c745b86806becd" alt="The image shows an AWS Identity and Access Management (IAM) interface, specifically the user permissions page where &#x22;AmazonVPCFullAccess&#x22; is attached as a policy." width="1920" height="1080" data-path="images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-iam-user-permissions-amazonvpc.jpg" />
</Frame>

5. Create an access key for programmatic access

Open the user’s “Security credentials” tab and create a new access key. You will receive an Access Key ID and a Secret Access Key. Copy and store them immediately — the Secret Access Key is shown only once.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/urHSIc4JjzJnqdR4/images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-iam-dashboard-security-credentials.jpg?fit=max&auto=format&n=urHSIc4JjzJnqdR4&q=85&s=2d53eb90dd94d2e890d34b7598b44bc7" alt="The image shows the AWS Identity and Access Management (IAM) dashboard focused on security credentials, including options for multi-factor authentication, access keys, and SSH public keys." width="1920" height="1080" data-path="images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-iam-dashboard-security-credentials.jpg" />
</Frame>

You may add a description or tags for the access key during creation.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/urHSIc4JjzJnqdR4/images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-console-access-key-description-tag.jpg?fit=max&auto=format&n=urHSIc4JjzJnqdR4&q=85&s=e7f1f331f889d08a17bb24664c721638" alt="The image shows a screen from the AWS console where a user can set a description tag for creating an access key, with instructions and options to proceed or cancel." width="1920" height="1080" data-path="images/HashiCorp-Certified-Terraform-Associate-004/Preparing-Your-Environment/Setting-Up-AWS-Credentials-for-Terraform/aws-console-access-key-description-tag.jpg" />
</Frame>

6. Example Terraform configuration

If you don’t yet have any .tf files, save a minimal example (for instance `main.tf`) in your working directory before running `terraform plan`:

```hcl theme={null}
provider "aws" {}

resource "aws_vpc" "production" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "production"
  }
}

resource "aws_vpc" "dev" {
  cidr_block = "10.10.0.0/16"

  tags = {
    Name = "dev"
  }
}

resource "aws_subnet" "workloads" {
  vpc_id     = aws_vpc.production.id
  cidr_block = "10.1.0.0/24"

  tags = {
    Name = "workloads"
  }
}
```

<Callout icon="lightbulb" color="#1CB2FE">
  By default the empty `provider "aws" {}` block lets Terraform pick up credentials and the region from environment variables or the AWS shared credentials file. To set a region explicitly, add `region = "us-east-1"` inside the provider block (or configure `required_providers` if your Terraform workflow requires it).
</Callout>

7. Configure AWS credentials for Terraform

You can provide the Access Key ID and Secret Access Key to Terraform in multiple ways. Below are common approaches:

| Platform / Method                        |                                                                                                                                            Command / File | Notes                                                                                                              |
| ---------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------: | ------------------------------------------------------------------------------------------------------------------ |
| macOS / Linux (bash, zsh) — session-only |       `bash export AWS_ACCESS_KEY_ID="AKIAEXAMPLEACCESSKEY" export AWS_SECRET_ACCESS_KEY="examplesecretKEY+chars" export AWS_DEFAULT_REGION="us-east-1" ` | Exports apply only to the current shell session. Persist in `~/.bashrc` or `~/.zshrc` if needed (beware security). |
| Windows PowerShell — session-only        | `powershell $Env:AWS_ACCESS_KEY_ID = "AKIAEXAMPLEACCESSKEY" $Env:AWS_SECRET_ACCESS_KEY = "examplesecretKEY+chars" $Env:AWS_DEFAULT_REGION = "us-east-1" ` | Session-only — values disappear when the shell closes.                                                             |
| AWS CLI (recommended for persistence)    |                                                                       Run `aws configure` and follow prompts (stores credentials in `~/.aws/credentials`) | Cross-platform managed file — safer than repeatedly exporting keys in shells. See AWS CLI docs below.              |

If you use the AWS CLI, run:

* `aws configure` — then enter Access Key ID, Secret Access Key, default region, and output format when prompted.

8. Verify Terraform and credentials

With credentials set and your `.tf` files in the working directory, run Terraform commands:

* `terraform init` to initialize providers.
* `terraform plan` to see what Terraform intends to create.

If you run `terraform plan` with no configuration files you will see the familiar message:

```bash theme={null}
$ terraform plan
Error: No configuration files

Plan requires configuration to be present. Planning without a configuration would mark everything for destruction, which is normally not what is desired. If you would like to destroy everything, run plan with the -destroy option. Otherwise, create a Terraform configuration file (.tf file) and try again.
```

Security best practices

* Use the principle of least privilege when attaching IAM policies.
* Prefer short-lived credentials or managed profiles when possible.
* Rotate or delete access keys when they are no longer required.

<Callout icon="warning" color="#FF6B6B">
  After you finish using these credentials, delete the access key in the IAM console (or rotate it). Treat access keys like passwords and follow the principle of least privilege.
</Callout>

Links and references

* [AWS IAM documentation](https://docs.aws.amazon.com/iam/)
* [AWS CLI configure quickstart](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html)
* [Terraform AWS Provider documentation](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)

This completes the setup for creating an IAM user and configuring AWS credentials for Terraform. Use `export` on macOS/Linux, `$Env:` for Windows PowerShell, or the AWS CLI `aws configure` command for persistent credentials.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/hashicorp-certified-terraform-associate-004/module/df5b5815-c1ea-45f5-ba18-7a5c53ded28a/lesson/885c3bca-859c-4ac9-8836-ecb3e10c9a70" />
</CardGroup>
