-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiam.tf
More file actions
158 lines (144 loc) · 5.1 KB
/
Copy pathiam.tf
File metadata and controls
158 lines (144 loc) · 5.1 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
###############################################################################
# Trust policies. The MicroVM build/exec roles are assumed by the lambda service
# principal; the confused-deputy guard pins the source account.
###############################################################################
data "aws_iam_policy_document" "lambda_trust" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "aws:SourceAccount"
values = [local.account_id]
}
}
}
# The dispatcher is an ordinary Lambda function; no SourceAccount condition.
data "aws_iam_policy_document" "dispatcher_trust" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
###############################################################################
# Build role - assumed by Lambda DURING the MicroVM image build. Reads the code
# artifact from S3 and writes build logs.
###############################################################################
resource "aws_iam_role" "build" {
name = "${var.name_prefix}-build-role"
assume_role_policy = data.aws_iam_policy_document.lambda_trust.json
tags = local.tags
}
data "aws_iam_policy_document" "build" {
statement {
sid = "ReadArtifactWriteBuildOutput"
effect = "Allow"
actions = ["s3:GetObject", "s3:PutObject", "s3:ListBucket"]
resources = [aws_s3_bucket.artifacts.arn, "${aws_s3_bucket.artifacts.arn}/*"]
}
statement {
sid = "BuildLogs"
effect = "Allow"
actions = ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"]
resources = ["arn:aws:logs:${local.region}:${local.account_id}:*"]
}
}
resource "aws_iam_role_policy" "build" {
name = "build"
role = aws_iam_role.build.id
policy = data.aws_iam_policy_document.build.json
}
###############################################################################
# Exec role - assumed AT RUNTIME by each MicroVM. Writes runner/dockerd logs and
# may self-terminate when an ephemeral job ends (stops billing immediately).
###############################################################################
resource "aws_iam_role" "exec" {
name = "${var.name_prefix}-exec-role"
assume_role_policy = data.aws_iam_policy_document.lambda_trust.json
tags = local.tags
}
data "aws_iam_policy_document" "exec" {
statement {
sid = "RuntimeLogs"
effect = "Allow"
actions = ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"]
resources = ["arn:aws:logs:${local.region}:${local.account_id}:*"]
}
statement {
sid = "SelfTerminate"
effect = "Allow"
actions = ["lambda:TerminateMicrovm"]
resources = ["*"]
}
}
resource "aws_iam_role_policy" "exec" {
name = "exec"
role = aws_iam_role.exec.id
policy = data.aws_iam_policy_document.exec.json
}
###############################################################################
# Dispatcher role - assumed by the dispatcher Lambda. Launches/terminates
# MicroVMs, passes the egress connector + exec role, and reads the secret.
###############################################################################
resource "aws_iam_role" "dispatcher" {
name = "${var.name_prefix}-dispatcher-role"
assume_role_policy = data.aws_iam_policy_document.dispatcher_trust.json
tags = local.tags
}
data "aws_iam_policy_document" "dispatcher" {
statement {
sid = "Logs"
effect = "Allow"
actions = ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"]
resources = ["arn:aws:logs:${local.region}:${local.account_id}:*"]
}
statement {
sid = "MicroVMOps"
effect = "Allow"
actions = ["lambda:RunMicrovm", "lambda:TerminateMicrovm", "lambda:GetMicrovm", "lambda:GetMicrovmImage", "lambda:ListMicrovms"]
resources = ["*"]
}
statement {
sid = "PassNetworkConnector"
effect = "Allow"
actions = ["lambda:PassNetworkConnector"]
resources = ["arn:aws:lambda:${local.region}:aws:network-connector:aws-network-connector:*"]
}
statement {
sid = "PassExecRole"
effect = "Allow"
actions = ["iam:PassRole"]
resources = [aws_iam_role.exec.arn]
}
statement {
sid = "ReadDispatcherParam"
effect = "Allow"
actions = ["ssm:GetParameter"]
resources = [aws_ssm_parameter.dispatcher.arn]
}
statement {
sid = "DecryptDispatcherParam"
effect = "Allow"
actions = ["kms:Decrypt"]
resources = ["*"]
# Scope the AWS-managed aws/ssm key decrypt to SSM only.
condition {
test = "StringEquals"
variable = "kms:ViaService"
values = ["ssm.${local.region}.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy" "dispatcher" {
name = "dispatcher"
role = aws_iam_role.dispatcher.id
policy = data.aws_iam_policy_document.dispatcher.json
}