For system, network and cloud administrators

How to log in AWS using Terraform

Terraform is a wonderful IaaC software. We can login into AWS in a couple of ways:

  • by declaring our AWS credentials (key, secret) inside one of the .tf files (main.tf, for example);
  • by declaring our AWS credentials using the Terraform CLI.

Log in AWS using credentials declared in main.tf

provider "aws" {
  region     = "us-west-2"
  access_key = "your-access-key-from-the-AWS-account"
  secret_key = "your-secret-key-from-the-AWS-account"
}

Basically, what happens in the above example is that Terraform will log you into AWS using the provider plugin which require your access and secret keys – these are part of a different story.

Log in AWS using the Terraform CLI

During the execution of the terraform init command, there is also a possibility to get logged in AWS during that Terraform project initialization. One straightforward code example can just use AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY:

AWS_ACCESS_KEY_ID="this-is-your-access-key" AWS_SECRET_ACCESS_KEY="this-is-your-secret-key" terraform init

Or you can export your AWS credentials before executing any terraform plan or terraform apply commands like so:

export AWS_ACCESS_KEY_ID="AKIAXSKLCRINE3TY5FIZ"
export AWS_SECRET_ACCESS_KEY="uH9bQZ2tyD8b/chYxY52emJp036HBMwww6d6eaun"

Another format used to log in AWS during the project initialization is by using the -backend-config option:

terraform init -backend-config="access_key=this-is-your-access-key" -backend-config="secret_key=this-is-your-secret-key"

Leave a Reply

Your email address will not be published. Required fields are marked *