1. Answers
  2. Interpolating AWS Account ID into an ARN String

How do I interpolate AWS account ID into an ARN string?

In this example, you will learn how to interpolate your AWS account ID into an ARN string. This is useful when you need to reference resources by their ARNs dynamically within your infrastructure code.

The generated ARN string typically looks like arn:aws:<service>:<region>:<account-id>:<resource>. We will use the ${data.aws_caller_identity.current.account_id} to dynamically insert the AWS account ID into the ARN.

Here’s a complete example that defines the necessary configuration, retrieves the AWS caller identity, and interpolates the AWS account ID to create an ARN string.

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Retrieve the AWS account ID
const current = aws.getCallerIdentityOutput({});
export const myArn = current.apply(current => `arn:aws:iam::${current.accountId}:role/my-role`);
export const interpolatedArn = current.apply(current => current.accountId);

In this example:

  1. We define the AWS provider and set the region to us-west-2.
  2. We use the aws_caller_identity data source to retrieve information about the current AWS account, including the account_id.
  3. We interpolate the account_id into an ARN string for an IAM role.
  4. The interpolated ARN string is then set as an output.

This allows us to dynamically generate ARNs with the correct AWS account ID at runtime.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up