Skip to content

Instantly share code, notes, and snippets.

@mhmdio
Last active April 9, 2024 08:53
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhmdio/fc0ba674ae1059f7422cc21dec335526 to your computer and use it in GitHub Desktop.
Save mhmdio/fc0ba674ae1059f7422cc21dec335526 to your computer and use it in GitHub Desktop.
terraform data account_id and region
data "aws_caller_identity" "current" {} # data.aws_caller_identity.current.account_id
data "aws_region" "current" {} # data.aws_region.current.name
output "account_id" {
description = "Selected AWS Account ID"
value = data.aws_caller_identity.current.account_id
}
output "region" {
description = "Details about selected AWS region"
value = data.aws_region.current.name
}
data "aws_default_tags" "current" {}
# The attribute `${data.aws_caller_identity.current.account_id}` will be current account number.
data "aws_caller_identity" "current" {}
# The attribue `${data.aws_iam_account_alias.current.account_alias}` will be current account alias
data "aws_iam_account_alias" "current" {}
# The attribute `${data.aws_region.current.name}` will be current region
data "aws_region" "current" {}
# The attribute `${data.aws_partition.current.partition}` will be current partition
data "aws_partition" "current" {}
# Set as [local values](https://www.terraform.io/docs/configuration/locals.html)
locals {
account_id = data.aws_caller_identity.current.account_id
account_alias = data.aws_iam_account_alias.current.account_alias
region = data.aws_region.current.name
partition = data.aws_partition.current.partition
default_tags = data.aws_default_tags.current.tags
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment