!aws.account_id
The !aws.account_id YAML function retrieves the AWS account ID of the current caller identity
by calling the AWS STS GetCallerIdentity API.
Usage
The !aws.account_id function takes no parameters:
# Get the AWS account ID of the current caller identity
account_id: !aws.account_id
Arguments
This function takes no arguments. It uses the AWS credentials from the environment or the Atmos authentication context if configured.
How It Works
When processing the !aws.account_id YAML function, Atmos:
-
Loads AWS Configuration - Uses the standard AWS SDK credential resolution chain:
- Environment variables (
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKEN) - Shared credentials file (
~/.aws/credentials) - Shared config file (
~/.aws/config) - EC2 Instance Metadata Service (IMDS)
- ECS Task credentials
- Web Identity Token credentials
- Environment variables (
-
Calls STS GetCallerIdentity - Makes an API call to retrieve the caller identity
-
Returns Account ID - Extracts and returns the 12-digit AWS account ID as a string
When using Atmos Authentication, the function automatically uses credentials
from the active identity. This enables seamless integration with SSO, assume role chains, and other
authentication methods configured in your atmos.yaml.
Caching
The !aws.account_id function caches its results in memory for the duration of the CLI invocation.
This means:
- Multiple uses of
!aws.account_idin the same command only make one STS API call - Different authentication contexts (e.g., different profiles) get separate cache entries
- Each new CLI command starts with a fresh cache
This caching significantly improves performance when the function is used in multiple places across your stack manifests.
Atmos supports type-aware merging of YAML functions and concrete values, allowing them to coexist in the inheritance chain without type conflicts. See the full explanation: YAML Function Merging
Examples
Basic Usage
stack.yaml
Use in Backend Configuration
stack.yaml
Conditional Logic with Account ID
stack.yaml
Multiple Components Using Account ID
stack.yaml
Comparison with Terragrunt
This function is equivalent to Terragrunt's get_aws_account_id() function:
| Terragrunt | Atmos |
|---|---|
get_aws_account_id() | !aws.account_id |
Error Handling
If the function fails to retrieve the AWS caller identity (e.g., no credentials available, network issues, or insufficient permissions), Atmos will log an error and exit.
Common error scenarios:
- No AWS credentials configured
- Expired credentials
- Network connectivity issues
- Missing STS permissions
Considerations
- Requires valid AWS credentials - The function will fail if no valid credentials are available
- Network dependency - Requires connectivity to AWS STS endpoint
- Performance - Results are cached per CLI invocation, so there's minimal overhead when used multiple times
- IAM permissions - Requires
sts:GetCallerIdentitypermission (usually available to all authenticated principals)
Related Functions
- !aws.caller_identity_arn - Get the full ARN of the caller identity
- !aws.caller_identity_user_id - Get the unique user ID
- !aws.region - Get the AWS region