Dan Ionis
Dan Ionis

Reputation: 33

Accessing a Terraform variable from a map?

I'm trying to reach the aws_partner_event_source variable that sits within sink{} inside the resource.

resource "auth0_log_stream" "aws_logstream" {
  name   = "AWS Eventbridge Auth0 Log Stream"
  type   = "eventbridge"
  status = "active"

  sink {
    aws_account_id = "xxx"
    aws_region     = "eu-west-1"
    aws_partner_event_source = "xxx"
  }
}

resource "aws_cloudwatch_event_bus" "event_bridge_event_bus" {
  event_source_name = auth0_log_stream.aws_logstream.sink[aws_partner_event_source]
}

This however, doesn't work. I get: A reference to a resource type must be followed by at least one attribute access, specifying the resource name.

I also tried:

event_source_name = auth0_log_stream.aws_logstream.sink[2]

Which throws:

auth0_log_stream.aws_logstream.sink is list of object with 1 element │ │ The given key does not identify an element in this collection value.

These are the possible solutions I found when looking through Terraform Variable docs, but I can't seem to get it to work. Any help appreciated, thanks.

Upvotes: 0

Views: 463

Answers (3)

Mondeep Maity
Mondeep Maity

Reputation: 1

you can access the attribute by using attribute

auth0_log_stream.aws_logstream.sink[0].aws_partner_event_source

Upvotes: 0

Marko E
Marko E

Reputation: 18138

Since sink is a list of objects with one element (as per the error output), the correct syntax is:

event_source_name = auth0_log_stream.aws_logstream.sink[0].aws_partner_event_source

Upvotes: 1

javier
javier

Reputation: 192

You can access the variable using:

auth0_log_stream.aws_logstream.sink.aws_partner_event_source

as it is not a list to index with [], but an object with named attribute.

Upvotes: 0

Related Questions