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:
- We define the AWS provider and set the region to
us-west-2
. - We use the
aws_caller_identity
data source to retrieve information about the current AWS account, including theaccount_id
. - We interpolate the
account_id
into an ARN string for an IAM role. - 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 upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.