This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
image-pipeline.tf
224 lines (200 loc) · 7.59 KB
/
image-pipeline.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
# Define the EC2 image builder instance role (instance used to rebuild the image for new updates or at defined cadence)
# https://docs.aws.amazon.com/imagebuilder/latest/userguide/image-builder-setting-up.html#image-builder-IAM-prereq
resource "aws_iam_role" "vdi_image_builder_instance_role" {
name = "${var.project}-${var.environment}-${local.os}-${local.os_version}-vdi-image-builder"
path = "/"
managed_policy_arns = [
"arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore",
"arn:aws:iam::aws:policy/AmazonSSMDirectoryServiceAccess",
"arn:aws:iam::aws:policy/EC2InstanceProfileForImageBuilder",
"arn:aws:iam::aws:policy/EC2InstanceProfileForImageBuilderECRContainerBuilds",
"arn:aws:iam::aws:policy/AmazonS3FullAccess",
"arn:aws:iam::aws:policy/SecretsManagerReadWrite",
"arn:aws:iam::aws:policy/CloudWatchFullAccess",
"arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
]
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
# Needed to allow to write to KMS encrypted bucket
resource "aws_iam_role_policy" "vdi_image_builder_instance_role_kms_policy" {
name = "${var.project}-${var.environment}-${local.os}-${local.os_version}-vdi-image-builder-instance-role-policy"
role = aws_iam_role.vdi_image_builder_instance_role.id
# Terraform's "jsonencode" function converts a
# Terraform expression result to valid JSON syntax.
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"kms:GenerateDataKey",
"kms:Decrypt"
]
Effect = "Allow"
Resource = [var.kms_key_arn]
},
{
"Effect" : "Allow",
"Action" : [
"logs:CreateLogStream",
"logs:CreateLogGroup",
"logs:PutLogEvents"
],
"Resource" : "arn:aws:logs:${var.region}:*:log-group:/aws/imagebuilder/*"
}
]
})
}
# Define the instance profile applied to EC2 builder instance
resource "aws_iam_instance_profile" "vdi_image_builder_instance_profile" {
name_prefix = "${var.project}-${var.environment}-${local.os}-${local.os_version}-vdi-image-builder"
role = aws_iam_role.vdi_image_builder_instance_role.name
}
# Define the instance role for connection gateway running instance
resource "aws_iam_role" "vdi_instance_role" {
name_prefix = "${var.project}-${var.environment}-${local.os}-${local.os_version}-vdi"
path = "/"
managed_policy_arns = [
"arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore",
"arn:aws:iam::aws:policy/AmazonSSMDirectoryServiceAccess",
"arn:aws:iam::aws:policy/EC2InstanceProfileForImageBuilder",
"arn:aws:iam::aws:policy/EC2InstanceProfileForImageBuilderECRContainerBuilds",
"arn:aws:iam::aws:policy/SecretsManagerReadWrite",
"arn:aws:iam::aws:policy/AmazonS3FullAccess",
"arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
]
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
# Needed to allow to builder instance read/write to KMS encrypted bucket
resource "aws_iam_role_policy" "vdi_instance_role_kms_policy" {
name = "${var.project}-${var.environment}-${local.os}-${local.os_version}-vdi-instance-role-policy"
role = aws_iam_role.vdi_instance_role.id
# Terraform's "jsonencode" function converts a
# Terraform expression result to valid JSON syntax.
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"kms:GenerateDataKey",
"kms:Decrypt"
]
Effect = "Allow"
Resource = [var.kms_key_arn]
},
{
"Effect" : "Allow",
"Action" : [
"logs:CreateLogStream",
"logs:CreateLogGroup",
"logs:PutLogEvents"
],
"Resource" : "arn:aws:logs:${var.region}:*:log-group:/aws/imagebuilder/*"
}
]
})
}
# Define the instance profile applied to ALS running instance
resource "aws_iam_instance_profile" "vdi_instance_profile" {
name_prefix = "${var.project}-${var.environment}-${local.os}-${local.os_version}-vdi"
role = aws_iam_role.vdi_instance_role.name
}
# Define the security group applied to EC2 builder instance
resource "aws_security_group" "vdi_builder_sg" {
name = "${var.project}-${var.environment}-${local.os}-${local.os_version}-vdi-image-builder-sg"
vpc_id = var.vpc_id
description = "${local.os}-${local.os_version} image builder sg"
egress {
description = "Allow outbound to anywhere"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
}
# Define the connection gateway image pipeline infrastructure
resource "aws_imagebuilder_infrastructure_configuration" "vdi_image_builder_infrastructure_configuration" {
name = "${var.project}-${var.environment}-${local.os}-${local.os_version}-vdi"
instance_profile_name = aws_iam_instance_profile.vdi_image_builder_instance_profile.name
instance_types = [var.instance_type]
terminate_instance_on_failure = true
subnet_id = var.public_subnets_id[0]
security_group_ids = [aws_security_group.vdi_builder_sg.id]
logging {
s3_logs {
s3_bucket_name = aws_s3_bucket.software_bucket.id
s3_key_prefix = "${local.os}-${local.os_version}/pipeline-logs"
}
}
}
# Define distribution settings
resource "aws_imagebuilder_distribution_configuration" "vdi_image_builder_distribution_configuration" {
name = "${var.project}-${var.environment}-${local.os}-${local.os_version}-vdi"
description = "AMI to launch ${local.os}-${local.os_version} VDIs"
distribution {
region = var.region
ami_distribution_configuration {
name = "${var.project}-${var.environment}-${local.os}-${local.os_version}-vdi-{{ imagebuilder:buildDate }}"
kms_key_id = var.kms_key_id
}
launch_template_configuration {
launch_template_id = var.launch_template_id
default = true
}
}
}
# Define the pipeline for Windows Server 2022
resource "aws_imagebuilder_image_pipeline" "vdi_image_builder_pipeline" {
name = "${var.project}-${var.environment}-${local.os}-${local.os_version}-vdi"
description = "${local.os}-${local.os_version} VDI Image Pipeline"
image_recipe_arn = aws_imagebuilder_image_recipe.vdi_image_builder_recipe.arn
distribution_configuration_arn = aws_imagebuilder_distribution_configuration.vdi_image_builder_distribution_configuration.arn
infrastructure_configuration_arn = aws_imagebuilder_infrastructure_configuration.vdi_image_builder_infrastructure_configuration.arn
enhanced_image_metadata_enabled = false
image_tests_configuration {
image_tests_enabled = false
}
schedule {
pipeline_execution_start_condition = "EXPRESSION_MATCH_AND_DEPENDENCY_UPDATES_AVAILABLE"
schedule_expression = var.run_schedule
}
}
data "aws_lambda_invocation" "build_image_vdi" {
function_name = var.build_image_function
input = <<JSON
{
"apply_date": "${timestamp()}",
"image_pipeline_arn": "${aws_imagebuilder_image_pipeline.vdi_image_builder_pipeline.arn}"
}
JSON
}