-
Notifications
You must be signed in to change notification settings - Fork 7
/
iam.tf
106 lines (94 loc) · 2.59 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*== ec2 CLUSTER INSTANCES IAM ==*/
resource "aws_iam_instance_profile" "ec2_instance" {
name = "${var.environment}-${var.prefix}${var.app_name}${var.suffix}-ec2-profile"
role = aws_iam_role.ec2_instance.name
}
resource "aws_iam_role" "ec2_instance" {
name = "${var.environment}-${var.prefix}${var.app_name}${var.suffix}-ec2-role"
path = "/"
assume_role_policy = data.aws_iam_policy_document.ec2_instance.json
tags = merge(
var.extra_tags,
map("Name", "${var.environment}-${var.prefix}${var.app_name}${var.suffix}-ec2-role")
)
}
data "aws_iam_policy_document" "ec2_instance" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy" "ec2_role_policy" {
name = "${var.environment}-${var.prefix}${var.app_name}${var.suffix}-ec2-role-policy"
role = aws_iam_role.ec2_instance.id
policy = data.aws_iam_policy_document.ec2_role_policy.json
}
data "aws_iam_policy_document" "ec2_role_policy" {
statement {
sid = "VaultKMSUnseal"
effect = "Allow"
resources = ["*"]
actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:DescribeKey",
"ec2:DescribeInstances"
]
}
statement {
sid = "allowParameterStore"
effect = "Allow"
resources = [
"arn:aws:ssm:*:*:parameter/${var.prefix}${var.app_name}${var.suffix}/${var.environment}/*"
]
actions = [
"ssm:PutParameter",
"ssm:GetParameter"
]
}
statement {
sid = "allowLoggingToCloudWatch"
effect = "Allow"
resources = [
"*"
]
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents",
]
}
statement {
sid = "allowDynamoDB"
effect = "Allow"
resources = [
aws_dynamodb_table.dynamodb_table.arn
]
actions = [
"dynamodb:DescribeLimits",
"dynamodb:DescribeTimeToLive",
"dynamodb:ListTagsOfResource",
"dynamodb:DescribeReservedCapacityOfferings",
"dynamodb:DescribeReservedCapacity",
"dynamodb:ListTables",
"dynamodb:BatchGetItem",
"dynamodb:BatchWriteItem",
"dynamodb:CreateTable",
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:GetRecords",
"dynamodb:PutItem",
"dynamodb:Query",
"dynamodb:UpdateItem",
"dynamodb:Scan",
"dynamodb:DescribeTable"
]
}
}
resource "aws_iam_role_policy_attachment" "this" {
role = aws_iam_role.ec2_instance.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}