0

I have a terraform script that builds a MSK cluster, I have outputted the zookeeper & broker information and I would like to add this data to a AWS secret's manager secret.

The problem is they are comma separated.

This is the output in the pipeline

 ~ zookeeper_connect_string = "z-1.example-loadtesting.qukw3u.c2.kafka.eu-west-1.amazonaws.com:2181,z-2.example-loadtesting.qukw3u.c2.kafka.eu-west-1.amazonaws.com:2181,z-3.example-loadtesting.qukw3u.c2.kafka.eu-west-1.amazonaws.com:2181

This is the terrafrom for the secrets manager.

resource "aws_secretsmanager_secret_version" "connection" {
  secret_id = aws_secretsmanager_secret.kafka.id
  secret_string = jsonencode({
  "bootstrap_brokers_tls_1": aws_msk_cluster.example.bootstrap_brokers_tls,
  "bootstrap_brokers_tls_2": aws_msk_cluster.example.bootstrap_brokers_tls,
  "zookeeper_connect_string_1": aws_msk_cluster.example.zookeeper_connect_string,
  "zookeeper_connect_string_2":aws_msk_cluster.example.zookeeper_connect_string,
  })
}

Is there a way to use terraform indexes or some other method to use this data?

Thanks

Output

output "zookeeper_connect_string_0" {
  value = element(split(",", aws_msk_cluster.example.zookeeper_connect_string),0)
}
 output "zookeeper_connect_string1_1" {
   value = element(split(",", aws_msk_cluster.example.zookeeper_connect_string),1)
 }

1 Answers1

1

You could reference each item using an index. Here the split function will split up the string into a list(string) and the [0] will get the first indexed value.

split(",", aws_msk_cluster.example.bootstrap_brokers_tls)[0]

Or do something like this where the JSON string saved to secrets manager contains a list of values under each key.

resource "aws_secretsmanager_secret_version" "connection" {
  secret_id = aws_secretsmanager_secret.kafka.id

  secret_string = jsonencode({
    bootstrap_brokers_tls = split(",", aws_msk_cluster.example.bootstrap_brokers_tls)

    zookeeper_connect_string = split(",", aws_msk_cluster.example.zookeeper_connect_string)
  })
}
SomeGuyOnAComputer
  • 5,414
  • 6
  • 40
  • 72