Terraform: retrieve AWS information

1 min read | by Jordi Prats

When running terraform on an AWS account we might need to have some context information such as it's account ID or the region we are in. Instead of having to set them as variables we can use the aws_caller_identity, aws_partition and aws_region datasources to retrieve this information

It's usage can't possibly be more straightforward:

data "aws_caller_identity" "current" {}

data "aws_partition" "current" {} 

data "aws_region" "current" {}
  • With the aws_caller_identity we can retrieve the account ID.

  • With aws_partition we can check what's the partition we are using, for example, the commercial AWS, the government AWS or AWS CN. Depending on the partition AWS used a different DNS domain which can also be retrieved using the dns_suffix and reverse_dns_prefix attributes

  • Finally, using aws_region we can get the region we are currently using

Posted on 19/01/2022