generated from dxw/terraform-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecs-cluster-infrastructure-datadog-agent-image-codebuild.tf
142 lines (112 loc) · 6.35 KB
/
ecs-cluster-infrastructure-datadog-agent-image-codebuild.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
resource "aws_iam_role" "infrastructure_ecs_cluster_datadog_agent_image_codebuild" {
count = local.enable_infrastructure_ecs_cluster_datadog_agent ? 1 : 0
name = "${local.resource_prefix}-${substr(sha512("ecs-cluster-datadog-agent-image-codebuild"), 0, 6)}"
description = "${local.resource_prefix}-ecs-cluster-datadog-agent-image-codebuild"
assume_role_policy = templatefile(
"${path.root}/policies/assume-roles/service-principle-standard.json.tpl",
{ services = jsonencode(["codebuild.amazonaws.com", "events.amazonaws.com"]) }
)
}
resource "aws_iam_policy" "infrastructure_ecs_cluster_datadog_agent_image_codebuild_cloudwatch_rw" {
count = local.enable_infrastructure_ecs_cluster_datadog_agent ? 1 : 0
name = "${local.resource_prefix}-${substr(sha512("ecs-cluster-datadog-agent-image-codebuild-cloudwatch-rw"), 0, 6)}"
description = "${local.resource_prefix}-ecs-cluster-datadog-agent-image-codebuild-cloudwatch-rw"
policy = templatefile("${path.root}/policies/cloudwatch-logs-rw.json.tpl", {})
}
resource "aws_iam_role_policy_attachment" "infrastructure_ecs_cluster_datadog_agent_image_codebuild_cloudwatch_rw" {
count = local.enable_infrastructure_ecs_cluster_datadog_agent ? 1 : 0
role = aws_iam_role.infrastructure_ecs_cluster_datadog_agent_image_codebuild[0].name
policy_arn = aws_iam_policy.infrastructure_ecs_cluster_datadog_agent_image_codebuild_cloudwatch_rw[0].arn
}
resource "aws_iam_policy" "infrastructure_ecs_cluster_datadog_agent_image_codebuild_allow_builds" {
count = local.enable_infrastructure_ecs_cluster_datadog_agent ? 1 : 0
name = "${local.resource_prefix}-${substr(sha512("ecs-cluster-datadog-agent-image-codebuild-allow-builds"), 0, 6)}"
description = "${local.resource_prefix}-ecs-cluster-datadog-agent-image-codebuild-allow-builds"
policy = templatefile("${path.root}/policies/codebuild-allow-builds.json.tpl", {})
}
resource "aws_iam_role_policy_attachment" "infrastructure_ecs_cluster_datadog_agent_image_codebuild_allow_builds" {
count = local.enable_infrastructure_ecs_cluster_datadog_agent ? 1 : 0
role = aws_iam_role.infrastructure_ecs_cluster_datadog_agent_image_codebuild[0].name
policy_arn = aws_iam_policy.infrastructure_ecs_cluster_datadog_agent_image_codebuild_allow_builds[0].arn
}
resource "aws_iam_policy" "infrastructure_ecs_cluster_datadog_agent_image_codebuild_ecr_push" {
count = local.enable_infrastructure_ecs_cluster_datadog_agent ? 1 : 0
name = "${local.resource_prefix}-${substr(sha512("ecs-cluster-datadog-agent-image-codebuild-ecr-push"), 0, 6)}"
description = "${local.resource_prefix}-ecs-cluster-datadog-agent-image-codebuild-ecr-push"
policy = templatefile(
"${path.root}/policies/ecr-push.json.tpl",
{ ecr_repository_arn = aws_ecr_repository.infrastructure_ecs_cluster_datadog_agent[0].arn }
)
}
resource "aws_iam_role_policy_attachment" "infrastructure_ecs_cluster_datadog_agent_image_codebuild_ecr_push" {
count = local.enable_infrastructure_ecs_cluster_datadog_agent ? 1 : 0
role = aws_iam_role.infrastructure_ecs_cluster_datadog_agent_image_codebuild[0].name
policy_arn = aws_iam_policy.infrastructure_ecs_cluster_datadog_agent_image_codebuild_ecr_push[0].arn
}
resource "aws_codebuild_project" "infrastructure_ecs_cluster_datadog_agent_image_build" {
count = local.enable_infrastructure_ecs_cluster_datadog_agent ? 1 : 0
name = "${local.resource_prefix}-ecs-cluster-datadog-agent-image-build"
description = "${local.resource_prefix} ECS Cluster Datadog Agent Image Build"
build_timeout = "20"
service_role = aws_iam_role.infrastructure_ecs_cluster_datadog_agent_image_codebuild[0].arn
artifacts {
type = "NO_ARTIFACTS"
}
environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "aws/codebuild/standard:7.0"
type = "LINUX_CONTAINER"
privileged_mode = true
environment_variable {
name = "AWS_ACCOUNT_ID"
value = local.aws_account_id
}
environment_variable {
name = "REPOSITORY_URI"
value = aws_ecr_repository.infrastructure_ecs_cluster_datadog_agent[0].repository_url
}
environment_variable {
name = "DOCKERHUB_USERNAME"
value = local.infrastructure_dockerhub_username
}
environment_variable {
name = "DOCKERHUB_TOKEN"
value = local.infrastructure_dockerhub_token
}
}
source {
type = "NO_SOURCE"
buildspec = templatefile("${path.root}/buildspecs/dalmatian-datadog.yml", {})
}
depends_on = [
aws_iam_role_policy_attachment.infrastructure_ecs_cluster_datadog_agent_image_codebuild_cloudwatch_rw,
aws_iam_role_policy_attachment.infrastructure_ecs_cluster_datadog_agent_image_codebuild_allow_builds,
aws_iam_role_policy_attachment.infrastructure_ecs_cluster_datadog_agent_image_codebuild_ecr_push,
]
}
resource "terraform_data" "infrastructure_ecs_cluster_datadog_agent_image_build_trigger_codebuild" {
count = local.enable_infrastructure_ecs_cluster_datadog_agent ? 1 : 0
triggers_replace = [
md5(templatefile("${path.root}/buildspecs/dalmatian-datadog.yml", {})),
]
provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
command = <<EOF
${path.root}/local-exec-scripts/trigger-codedeploy-project.sh \
-n "${aws_codebuild_project.infrastructure_ecs_cluster_datadog_agent_image_build[0].name}"
EOF
}
}
resource "aws_cloudwatch_event_rule" "infrastructure_ecs_cluster_datadog_agent_image_build_trigger_codebuild" {
count = local.enable_infrastructure_ecs_cluster_datadog_agent ? 1 : 0
name = "${local.resource_prefix_hash}-ecs-cluster-datadog-agent-image-build-trigger-codebuild"
description = "${local.resource_prefix} ECS Cluster Datadog Agent Image Build Trigger CodeBuild"
schedule_expression = "rate(24 hours)"
}
resource "aws_cloudwatch_event_target" "infrastructure_ecs_cluster_datadog_agent_image_build_trigger_codebuild" {
count = local.enable_infrastructure_ecs_cluster_datadog_agent ? 1 : 0
target_id = "${local.resource_prefix_hash}-ecs-cluster-datadog-agent-image-build-trigger-codebuild"
rule = aws_cloudwatch_event_rule.infrastructure_ecs_cluster_datadog_agent_image_build_trigger_codebuild[0].name
arn = aws_codebuild_project.infrastructure_ecs_cluster_datadog_agent_image_build[0].id
role_arn = aws_iam_role.infrastructure_ecs_cluster_datadog_agent_image_codebuild[0].arn
}