How to AssumeRole in Terraform LocalExec Provisioner Block

0 min read 167 words

I needed to execute a Terraform null_resource provisioner (local-exec) block to run an awscli command, but assume a role passed down to it.

There was no obvious way to pass the credentials to it, or assume a role directly, so the following workaround did the trick:

AssumeRole and Pass LocalExec Provisioner Command

resource "null_resource" "start-appstream-fleet" {
  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    command = <<EOF
set -e
CREDENTIALS=(`aws sts assume-role \
  --role-arn ${local.workspace.role} \
  --role-session-name "start-appstream-fleet" \
  --query "[Credentials.AccessKeyId,Credentials.SecretAccessKey,Credentials.SessionToken]" \
  --output text`)

unset AWS_PROFILE
export AWS_DEFAULT_REGION=us-east-1
export AWS_ACCESS_KEY_ID="$${CREDENTIALS[0]}"
export AWS_SECRET_ACCESS_KEY="$${CREDENTIALS[1]}"
export AWS_SESSION_TOKEN="$${CREDENTIALS[2]}"

aws appstream start-fleet --name sample-app-${var.environment}-fleet --region ${var.region} --output json
EOF
  }
}

The above code snippet runs 2 aws cli commands. The first is to get the credentials, which are then stored in environment variables, followed by consuming them in the actual aws cli command at the bottom.

The role that we want to assume, has been setup in the local.workspace.role local variable.

It is an ARN string that looks something like this:

"arn:aws:iam::<ACCOUNTID>:role/<PROJECT>-Pipeline-Role"
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags