Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build output map in terraform

I have a list of users to create, a list of sns topics and to create policies to give permissions to users on topics. These are all namespaced against the user...

Given:

main.tf


provider "aws" {
  region                  = "eu-west-1"
  profile                 = "terraform"
}

module "topics" {
  source = "./queues/topics"
}

module "users" {
  source = "./users"
}

module "policies" {
  source = "./policies"

  sns_topics = "${module.topics.sns_topics}"
}

./queues/topics.tf

resource "aws_sns_topic" "svc_topic" {
  count = "${length(var.sns_topics)}"
  name = "${element(var.sns_topics, count.index)}"
}

./queues/topics/vars.tf

# List of topics
variable "sns_topics" {
  type = "list"

  default = [
    "a-topic",
    "b-topic",
    "c-topic",
  ]
}

./queues/topics/output.tf

output "sns_topics" {
  value = "${var.sns_topics}"
}

./users/main.tf

resource "aws_iam_user" "usrs" {
  count = "${length(var.topic_user)}"
  name = "usr-msvc-${element(var.topic_user, count.index)}"
}

./users/vars.tf

variable "topic_user" {
  type = "list"

  default =[
    "user-a",
    "user-b",
    "user-c",
  ]
}

./users/output.tf

output "topic_user" {
  value = "${var.topic_user}"
}

./policies/main.tf

resource "aws_iam_policy" "sns_publisher" {
  count = "${length(var.sns_topics)}"

  name = "sns-${element(var.sns_topics, count.index)}-publisher"
  policy = <<POLICY
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "sns:Publish",
      "Resource": "arn:aws:sns:*:*:${element(var.sns_topics, count.index)}"
    }
  ]
}
POLICY
}

This is where I'd like to build a map in the output to map user to topic

output "usr_topic_map" {
  value = {
    "user-a" = "a-topic
    "user-b" = "c-topic
    "user-c" = "c-topic
  }
}

I can pass the list of users in to the policy module but I've no idea how to generate this map in the output.

I want to use this to attach the policy to the corresponding user.

Open to improving structure too if it simplifies tasks.

like image 203
MardyDev Avatar asked Dec 02 '22 10:12

MardyDev


1 Answers

You can use this approach as well.

output "outputs" {
  value       = {
    vpc_id        = aws_vpc.vpc.id
    pub_sbnt_ids  = aws_subnet.public.*.id
    priv_sbnt_ids = aws_subnet.private.*.id
  }
  description = "VPC id, List of all public, private and db subnet IDs"
}
like image 122
tmetodie Avatar answered Dec 20 '22 18:12

tmetodie