-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam.tf
60 lines (56 loc) · 1.68 KB
/
iam.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
59
60
resource "aws_iam_role" "iam" {
for_each = local.host
name_prefix = "${each.value.name}-"
tags = merge(local.tags, { Name = each.value.name })
assume_role_policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Action" : "sts:AssumeRole",
"Principal" : {
"Service" : "ec2.amazonaws.com"
},
"Effect" : "Allow",
"Sid" : ""
}
]
})
}
resource "aws_iam_instance_profile" "iam" {
for_each = local.host
name_prefix = "${each.value.name}-"
role = aws_iam_role.iam[each.value.name].name
}
resource "aws_iam_policy" "iam" {
for_each = local.host
name_prefix = "${each.value.name}-"
tags = merge(local.tags, { Name = each.value.name })
path = "/"
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"ssm:GetParametersByPath",
"ssm:GetParameters",
"ssm:PutParameter"
],
"Resource" : [
"arn:aws:ssm:${data.aws_region.default.name}:${data.aws_caller_identity.id.account_id}:parameter/${each.value.name}/*",
"arn:aws:ssm:${data.aws_region.default.name}:${data.aws_caller_identity.id.account_id}:parameter/${each.value.name}"
]
}
]
})
}
resource "aws_iam_role_policy_attachment" "iam" {
for_each = local.host
role = aws_iam_role.iam[each.value.name].name
policy_arn = aws_iam_policy.iam[each.value.name].arn
}
resource "aws_iam_role_policy_attachment" "ssm" {
for_each = local.host
role = aws_iam_role.iam[each.value.name].name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}