-
Notifications
You must be signed in to change notification settings - Fork 1
/
iam.tf
127 lines (120 loc) · 2.72 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
locals {
bastion_code_arn = "${aws_s3_bucket.this.arn}/${var.resources.bucket.code_key}"
principal_arns = concat(
[aws_iam_role.codebuild.arn],
var.resources.security.principal_arns
)
}
data aws_iam_policy_document assume_role_codebuild {
statement {
actions = ["sts:AssumeRole"]
effect = "Allow"
principals {
identifiers = ["codebuild.amazonaws.com"]
type = "Service"
}
}
}
data aws_iam_policy_document codebuild {
statement {
actions = [
"s3:PutObject",
"s3:GetObject",
"s3:ListBucketVersions",
"s3:ListBucket",
"s3:GetBucketVersioning",
"s3:GetObjectVersion",
"secretsmanager:GetSecretValue"
]
effect = "Allow"
resources = [
aws_s3_bucket.this.arn,
"${aws_s3_bucket.this.arn}/*"
]
}
statement {
actions = [
"secretsmanager:GetSecretValue"
]
effect = "Allow"
resources = [var.credentials.docker_hub_arn]
}
statement {
actions = [
"ec2:*",
"logs:*",
"cloudwatch:*",
"dynamodb:*",
"iam:*"
]
effect = "Allow"
resources = ["*"]
}
}
data aws_iam_policy_document create_instance_lambda {
statement {
actions = [
"codeBuild:StartBuild"
]
effect = "Allow"
resources = [aws_codebuild_project.create_bastion_stack.arn]
}
}
data aws_iam_policy_document destroy_idle_instances_lambda {
statement {
actions = [
"dynamodb:DescribeTable",
"dynamodb:ListTables",
"dynamodb:DeleteItem",
"dynamodb:Scan"
]
effect = "Allow"
resources = [aws_dynamodb_table.this.arn]
}
statement {
actions = [
"ssm:DescribeSessions",
"ec2:DescribeInstanceStatus"
]
effect = "Allow"
resources = ["*"]
}
statement {
actions = [
"codeBuild:StartBuild"
]
effect = "Allow"
resources = [aws_codebuild_project.destroy_bastion_stack.arn]
}
}
data aws_iam_policy_document terraform_state_policy {
statement {
actions = [
"s3:*"
]
condition {
test = "StringNotEquals"
values = local.principal_arns
variable = "aws:PrincipalArn"
}
effect = "Deny"
principals {
identifiers = ["*"]
type = "AWS"
}
resources = [
"arn:aws:s3:::${var.resources.bucket.name}/*",
"arn:aws:s3:::${var.resources.bucket.name}"
]
sid = "BastionCreationAccess"
}
}
resource aws_iam_role codebuild {
assume_role_policy = data.aws_iam_policy_document.assume_role_codebuild.json
name = var.code_build_role_name
}
resource aws_iam_role_policy codebuild {
name = var.code_build_role_name
policy = data.aws_iam_policy_document.codebuild.json
role = aws_iam_role.codebuild.id
}