forked from cloudposse/terraform-aws-iam-system-user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
58 lines (52 loc) · 1.5 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module "label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.17.0"
namespace = var.namespace
stage = var.stage
environment = var.environment
name = var.name
attributes = var.attributes
delimiter = var.delimiter
tags = var.tags
enabled = var.enabled
}
# Defines a user that should be able to write to you test bucket
resource "aws_iam_user" "default" {
count = var.enabled ? 1 : 0
name = module.label.id
path = var.path
force_destroy = var.force_destroy
tags = module.label.tags
}
# Generate API credentials
resource "aws_iam_access_key" "default" {
count = var.enabled ? 1 : 0
user = aws_iam_user.default[0].name
}
# policies -- inline and otherwise
locals {
inline_policies_map = merge(
var.inline_policies_map,
{ for i in var.inline_policies : md5(i) => i },
)
policy_arns_map = merge(
var.policy_arns_map,
{ for i in var.policy_arns : i => i },
)
}
resource "aws_iam_user_policy" "inline_policies" {
for_each = var.enabled ? local.inline_policies_map : {}
lifecycle {
create_before_destroy = true
}
name_prefix = module.label.id
user = join("", aws_iam_user.default.*.name)
policy = each.value
}
resource "aws_iam_user_policy_attachment" "policies" {
for_each = var.enabled ? local.policy_arns_map : {}
lifecycle {
create_before_destroy = true
}
user = join("", aws_iam_user.default.*.name)
policy_arn = each.value
}