The awscalleridentity data source is the standard mechanism in Terraform for retrieving the effective AWS identity under which Terraform is running. It returns the Account ID, User ID, and ARN of the calling entity without requiring any arguments. This capability removes the need to hardcode account identifiers and enables configurations that adapt automatically to different credentials, roles, and environments.
Introduction to Dynamic Account Identity
Hardcoding an AWS account ID into Terraform modules creates fragility. A configuration that contains a static account string must be edited for each account, increases the risk of deploying resources to the wrong account, and complicates multi-account setups. The awscalleridentity data source provides a dynamic alternative by querying the STS GetCallerIdentity API at plan time. The data source has no arguments and returns three attributes that describe the caller.
Dynamic configurations, multi-account setups, and security are the primary motivations for using the data source. Dynamic configurations avoid hardcoding the account ID in Terraform files. Multi-account setups simplify configurations for environments spanning multiple AWS accounts. Security improves by reducing the risk of errors caused by incorrect account IDs.
Defining the Data Source
The definition is minimal.
hcl
data "aws_caller_identity" "current" {}
This code snippet sets up a data source named current that fetches the caller's identity information. The name current is conventional but any meaningful name can be used for better code readability.
Once defined, the attributes are accessible as data.awscalleridentity.current.accountid, data.awscalleridentity.current.arn, and data.awscalleridentity.current.userid.
A common pattern is to expose the values as outputs for reuse in other configurations.
hcl
output "account_id" {
value = data.aws_caller_identity.current.account_id
}
output "caller_arn" {
value = data.aws_caller_identity.current.arn
}
output "caller_user" {
value = data.aws_caller_identity.current.user_id
}
Outputs export the account_id as an output for reuse in other configurations and allow inspection after apply.
Attributes Returned by awscalleridentity
The data source returns three attributes.
| Attribute | Description |
|---|---|
| account_id | The AWS Account ID number of the account that owns or contains the calling entity |
| arn | The AWS ARN associated with the calling entity |
| user_id | The unique identifier of the calling entity |
There are no arguments available for this data source. Use this data source to get access to the effective Account ID, User ID, and ARN in which Terraform is authorized.
Practical Usage Patterns
Resource Naming and Dynamic References
The account ID can be used in resource names or configurations to make configurations dynamic.
hcl
resource "aws_s3_bucket" "example" {
bucket = "example-bucket-${data.aws_caller_identity.current.account_id}"
acl = "private"
}
This configuration creates an S3 bucket with a name that includes the account ID.
A similar example:
hcl
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-bucket-${data.aws_caller_identity.current.account_id}"
# ... other configurations
}
You can now use the data.awscalleridentity.current.account_id variable in your Terraform code wherever you need to reference your AWS account ID.
IAM Policy Construction
The ARN attribute is useful for constructing IAM policies dynamically for the calling user or role.
```hcl
data "awsiampolicy_document" "this" {
statement {
sid = "Default"
actions = ["kms:"]
resources = [""]
principals {
type = "AWS"
identifiers = [data.aws_caller_identity.current.arn]
}
}
}
output "policy" {
value = data.awsiampolicy_document.this.json
}
```
The approach ensures that your Terraform code always uses the correct account ID, even if you switch AWS credentials or environments.
Conditional Logic and Filtering
Beyond resource naming, awscalleridentity is useful for:
- Conditional logic based on the caller's identity
- Constructing IAM policies dynamically for the calling user or role
- Filtering resources based on the account ID
Benefits of Dynamic Retrieval
Using awscalleridentity instead of manually entering your AWS account ID provides a more reliable and flexible approach.
Key takeaways:
- Avoid hardcoding: Instead of manually entering your AWS account ID, use the awscalleridentity data source for a more reliable and flexible approach
- Data source definition: Define a data source named current to fetch your caller identity information
- Access and store: Access the account ID from the data source and store it in an output variable
- Dynamic usage: Utilize the data.awscalleridentity.current.account_id variable throughout your Terraform code, such as in resource names or configurations
Benefits:
- Accuracy: Ensures the correct account ID is used, even when switching credentials or environments
- Flexibility: Eliminates the need to manually update the account ID in multiple places
- Best practice: Promotes cleaner and more maintainable Terraform code
By leveraging the awscalleridentity data source, you can dynamically retrieve and utilize your AWS account ID within your Terraform projects.
Security Considerations and Best Practices
Security considerations apply when exposing identity information.
While convenient, avoid directly embedding the awscalleridentity output into publicly accessible resources like bucket names if your use case demands stricter security. This is because it might expose your account ID.
Alternatives for sensitive contexts include:
- Fetching the account ID from secure sources like AWS Secrets Manager or environment variables
- If you need more than just the account ID, explore the aws_arn data source to parse ARNs and extract specific components
Best practices:
- Use meaningful names for your data sources and output variables for better code readability
- Add comments to explain the purpose and usage of the awscalleridentity data source
- Avoid hardcoding: Always use the awscalleridentity data source to retrieve the account ID dynamically
- Use outputs: Export the account_id as an output for reuse in other configurations
Troubleshooting and Operational Notes
If you encounter issues, ensure your AWS credentials are correctly configured and have the necessary permissions to access the sts:GetCallerIdentity API action.
Use terraform console to experiment with the awscalleridentity data source and debug any issues.
The data source requires valid AWS credentials at plan time. When writing Terraform code, you might need to reference your AWS account ID. Instead of hardcoding this value, it's recommended to use the awscalleridentity data source. This approach provides a more reliable and dynamic way to fetch your account ID within your Terraform scripts.
To use your AWS account ID as a variable in your Terraform code, you should use the awscalleridentity data source. This is a more reliable and dynamic approach than hardcoding the ID or fetching it from other sources.
Role Assumption Behavior
An important operational nuance exists with assumed roles. When providing an IAM role to assume in the provider via assumerole, the expected behavior is that the data source awscaller_identity would return the ARN of the role to be assumed.
In practice, with Terraform Core Version 1.3.6 and AWS Provider Version 4.46.0, the role ARN returned is an STS session ARN for the SDK rather than the assumed role ARN.
Example configuration illustrating the behavior:
```hcl
provider "aws" {
region = "us-east-1"
assumerole {
rolearn = "arn:aws:iam::111111111111:role/terraform"
}
}
data "awscalleridentity" "current" {}
data "awsiampolicy_document" "this" {
statement {
sid = "Default"
actions = ["kms:"]
resources = [""]
principals {
type = "AWS"
identifiers = [data.aws_caller_identity.current.arn]
}
}
}
output "policy" {
value = data.awsiampolicy_document.this.json
}
```
Using the code provided above, after terraform init and terraform plan, the ARN in the output is not the ARN of the assumed role passed. This behavior affects use cases that rely on the exact assumed role ARN for policy construction.
Conclusion
The awscalleridentity data source provides a reliable method to retrieve Account ID, User ID, and ARN dynamically in Terraform. It eliminates hardcoding, supports multi-account workflows, and enables identity-aware resource naming and policy generation. Accuracy improves when credentials or environments change, and code remains maintainable across teams.
Attention is required for security exposure when embedding identity values in public names, and for the known nuance with role assumption where the returned ARN reflects an STS session ARN. With correct credentials and sts:GetCallerIdentity permissions, the data source integrates cleanly into modules and pipelines for dynamic, account-aware infrastructure.