Mar*_*Dev 4 terraform terraform-provider-aws
我有一个要创建的用户列表、一个 sns 主题列表并创建策略以授予用户对主题的权限。这些都是针对用户的命名空间...
鉴于:
主文件
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}"
}
Run Code Online (Sandbox Code Playgroud)
./queues/topics.tf
resource "aws_sns_topic" "svc_topic" {
count = "${length(var.sns_topics)}"
name = "${element(var.sns_topics, count.index)}"
}
Run Code Online (Sandbox Code Playgroud)
./queues/topics/vars.tf
# List of topics
variable "sns_topics" {
type = "list"
default = [
"a-topic",
"b-topic",
"c-topic",
]
}
Run Code Online (Sandbox Code Playgroud)
./queues/topics/output.tf
output "sns_topics" {
value = "${var.sns_topics}"
}
Run Code Online (Sandbox Code Playgroud)
./users/main.tf
resource "aws_iam_user" "usrs" {
count = "${length(var.topic_user)}"
name = "usr-msvc-${element(var.topic_user, count.index)}"
}
Run Code Online (Sandbox Code Playgroud)
./users/vars.tf
variable "topic_user" {
type = "list"
default =[
"user-a",
"user-b",
"user-c",
]
}
Run Code Online (Sandbox Code Playgroud)
./users/output.tf
output "topic_user" {
value = "${var.topic_user}"
}
Run Code Online (Sandbox Code Playgroud)
./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
}
Run Code Online (Sandbox Code Playgroud)
这是我想在输出中构建地图以将用户映射到主题的地方
output "usr_topic_map" {
value = {
"user-a" = "a-topic
"user-b" = "c-topic
"user-c" = "c-topic
}
}
Run Code Online (Sandbox Code Playgroud)
我可以将用户列表传递给策略模块,但我不知道如何在输出中生成此映射。
我想用它来将策略附加到相应的用户。
如果可以简化任务,也愿意改进结构。
您可以使用 Terraform 函数zipmap来完成此操作。users由于您的键作为列表从模块输出module.users.topic_user,并且您的值作为topics列表从模块输出module.topics.sns_topics(模块输出文档),因此您可以将它们作为输出中函数的参数:
output "user_topic_map" {
value = "${zipmap(module.users.topic_user, module.topics.sns_topics)}"
}
Run Code Online (Sandbox Code Playgroud)
请记住,两个参数列表需要zipmap具有相同的长度,因此也可能在资源/变量/输出块中的某个位置保护该参数列表的代码。
小智 8
您也可以使用这种方法。
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"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12879 次 |
| 最近记录: |