Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

897
Visualizações
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?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda