Scenario:
You are in a position where you need to set the credentials for multiple AWS accounts in a single workspace in Terraform Cloud.
Solution:
Our recommended suggestion would be to use aliases. Example terraform code provided below:
terraform {
backend "remote" {
hostname = "app.terraform.io"
organization = "your_terraform_cloud_org_name"
workspaces {
name = "your_terraform_cloud_workspace_name"
}
}
}
variable "nik1_access_key" {}
variable "nik1_secret_key" {}
variable "nik2_access_key" {}
variable "nik2_secret_key" {}
provider "aws" {
alias = "nik1"
region = "us-east-1"
access_key = var.nik1_access_key
secret_key = var.nik1_secret_key
}
provider "aws" {
alias = "nik2"
region = "us-east-1"
access_key = var.nik2_access_key
secret_key = var.nik2_secret_key
}
resource "aws_instance" "ec1" {
ami = "ami-0fc61db8544a617ed"
instance_type = "t2.micro"
provider = aws.nik1
tags = {
Name = "nik-1"
}
}
resource "aws_instance" "ec2" {
ami = "ami-09a5b0b7edf08843d"
instance_type = "t2.micro"
provider = aws.nik2
tags = {
Name = "nik-2"
}
}
Next you will need to declare the variables with the credential values in your workspace in Terraform Cloud, making sure the variable names from your terraform code match the variable names in Terraform Cloud.