Environment Variables

Setting AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as environment variables.

Environment Variables method involves setting the AWS Access Key ID and Secret Access Key as environment variables in your system. Terraform automatically reads these environment variables to authenticate with AWS. This method is secure as it prevents hardcoding sensitive data in your scripts.

Example:

For Windows, you can set these environment variables using the command prompt:

setx AWS_ACCESS_KEY_ID "your-access-key-id"
setx AWS_SECRET_ACCESS_KEY "your-secret-access-key"

In the above commands, replace "your-access-key-id" and "your-secret-access-key" with your actual AWS Access Key ID and Secret Access Key respectively.

Now, you can define the AWS provider in your Terraform script without hardcoding the access key and secret key:

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

Note:

Please remember to close and reopen your command prompt to make sure the changes are effective. Also, never share these environment variables as they provide full access to your AWS account.

Last modified July 21, 2024: update (e2ae86c)