Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use AWS account_id variable in Terraform

I want access to my AWS Account ID in terraform. I am able to get at it with aws_caller_identity per the documentation. How do I then use the variable I created? In the below case I am trying to use it in an S3 bucket name:

data "aws_caller_identity" "current" {}
output "account_id" {
  value = data.aws_caller_identity.current.account_id
}

resource "aws_s3_bucket" "test-bucket" {
  bucket = "test-bucket-${account_id}"
}

Trying to use the account_id variable in this way gives me the error A reference to a resource type must be followed by at least one attribute access, specifying the resource name. I expect I'm not calling it correctly?

like image 626
John Skiles Skinner Avatar asked Jul 15 '21 16:07

John Skiles Skinner


People also ask

How do I use AWS access key and secret key in Terraform?

So first I install the AWS CLI. Then we run aws configure. [ ] $ aws configure AWS Access Key ID []: ENTER-YOUR-ACCESS-KEY-HERE AWS Secret Access Key []: ENTER-YOUR-SECRET-KEY-HERE Default region name []: us-west-2 Default output format []

How do I authenticate AWS using Terraform?

Set the AWS_PROFILE environment variable. For example, in Linux, you'd run export AWS_PROFILE=user2 . After that, you can run any AWS CLI tool (e.g., terraform apply ), and it should use your Named Profile. Some tools let you specify the profile as a command-line parameter or an argument in code.

How do you use variables in Terraform?

Terraform variables can be defined within the infrastructure plan but are recommended to be stored in their own variables file. All files in your Terraform directory using the . tf file format will be automatically loaded during operations. Create a variables file, for example, variables.tf and open the file for edit.


Video Answer


2 Answers

If you have a

data "aws_caller_identity" "current" {}

then you need to define a local for that value:

locals {
    account_id = data.aws_caller_identity.current.account_id
}

and then use it like

output "account_id" {
  value = local.account_id
}

resource "aws_s3_bucket" "test-bucket" {
  bucket = "test-bucket-${local.account_id}"
}

Terraform resolves the locals based on their dependencies so you can create locals that depend on other locals, on resources, on data blocks, etc.

like image 151
luk2302 Avatar answered Oct 18 '22 23:10

luk2302


Any time you create a datasource in terraform , it will export some attributes related to that datasource so that you can reference it somewhere else in your configuration and interpolate it with various ways.

In your case, you are already referencing the value of your account id in output block

So that same way, you can construct the string for the bucket name as follows.

resource "aws_s3_bucket" "test-bucket" {
  bucket = "test-bucket-${data.aws_caller_identity.current.account_id}"
}

I would highly recommend you go through the terrraform syntax which can help you better understand the resource, datasource and expressions

https://www.terraform.io/docs/language/expressions/references.html

like image 25
aashitvyas Avatar answered Oct 18 '22 22:10

aashitvyas