generated from dxw/terraform-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecs-cluster-infrastructure-ecs-asg-diff-lambda.tf
181 lines (145 loc) · 8.42 KB
/
ecs-cluster-infrastructure-ecs-asg-diff-lambda.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
resource "aws_cloudwatch_log_group" "ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda_log_group" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
name = "/aws/lambda/${local.resource_prefix_hash}-ecs-cluster-infrastructure-ecs-asg-diff-metric"
kms_key_id = local.infrastructure_kms_encryption ? aws_kms_key.infrastructure[0].arn : null
retention_in_days = local.infrastructure_ecs_cluster_ecs_asg_diff_metric_lambda_log_retention
}
resource "aws_iam_role" "ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
name = "${local.resource_prefix}-${substr(sha512("ecs-cluster-infrastructure-ecs-asg-diff-metric"), 0, 6)}"
description = "${local.resource_prefix}-ecs-cluster-infrastructure-ecs-asg-diff-metric"
assume_role_policy = templatefile(
"${path.root}/policies/assume-roles/service-principle-standard.json.tpl",
{ services = jsonencode(["lambda.amazonaws.com"]) }
)
}
resource "aws_iam_policy" "ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
name = "${local.resource_prefix}-ecs-cluster-infrastructure-ecs-asg-diff-metric"
policy = templatefile(
"${path.root}/policies/lambda-default.json.tpl",
{
region = local.aws_region
account_id = local.aws_account_id
function_name = "${local.resource_prefix_hash}-ecs-cluster-infrastructure-ecs-asg-diff-metric"
}
)
}
resource "aws_iam_role_policy_attachment" "ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
role = aws_iam_role.ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda[0].name
policy_arn = aws_iam_policy.ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda[0].arn
}
resource "aws_iam_policy" "ecs_cluster_infrastructure_ecs_asg_diff_metric_cloudwatch_put_metric_data_lambda" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
name = "${local.resource_prefix}-ecs-cluster-infrastructure-ecs-asg-diff-metric-cloudwatch-put-metric-data"
policy = templatefile(
"${path.root}/policies/cloudwatch-put-metric-data.json.tpl",
{
region = local.aws_region
account_id = local.aws_account_id
namespaces = ["ECS"]
}
)
}
resource "aws_iam_role_policy_attachment" "ecs_cluster_infrastructure_ecs_asg_diff_cloudwatch_metric_put_metric_data_lambda" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
role = aws_iam_role.ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda[0].name
policy_arn = aws_iam_policy.ecs_cluster_infrastructure_ecs_asg_diff_metric_cloudwatch_put_metric_data_lambda[0].arn
}
resource "aws_iam_policy" "ecs_cluster_infrastructure_ecs_asg_diff_metric_ecs_describe_cluster_lambda" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
name = "${local.resource_prefix}-ecs-cluster-infrastructure-ecs-asg-diff-metric-ecs-describe-cluster"
policy = templatefile(
"${path.root}/policies/ecs-describe-cluster.json.tpl",
{
region = local.aws_region
account_id = local.aws_account_id
cluster_names = [local.infrastructure_ecs_cluster_name]
}
)
}
resource "aws_iam_role_policy_attachment" "ecs_cluster_infrastructure_ecs_asg_diff_metric_ecs_describe_cluster_lambda" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
role = aws_iam_role.ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda[0].name
policy_arn = aws_iam_policy.ecs_cluster_infrastructure_ecs_asg_diff_metric_ecs_describe_cluster_lambda[0].arn
}
resource "aws_iam_policy" "ecs_cluster_infrastructure_ecs_asg_diff_metric_asg_describe_asg_lambda" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
name = "${local.resource_prefix}-ecs-cluster-infrastructure-ecs-asg-diff-metric-asg-describe-asg"
policy = templatefile(
"${path.root}/policies/asg-describe-asg.json.tpl", {}
)
}
resource "aws_iam_role_policy_attachment" "ecs_cluster_infrastructure_ecs_asg_diff_metric_asg_describe_asg_lambda" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
role = aws_iam_role.ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda[0].name
policy_arn = aws_iam_policy.ecs_cluster_infrastructure_ecs_asg_diff_metric_asg_describe_asg_lambda[0].arn
}
resource "aws_iam_policy" "ecs_cluster_infrastructure_ecs_asg_diff_metric_kms_encrypt" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert && local.infrastructure_kms_encryption ? 1 : 0
name = "${local.resource_prefix}-ecs-cluster-infrastructure-ecs-asg-diff-metric-kms-encrypt"
policy = templatefile(
"${path.root}/policies/kms-encrypt.json.tpl",
{ kms_key_arn = aws_kms_key.infrastructure[0].arn }
)
}
resource "aws_iam_role_policy_attachment" "ecs_cluster_infrastructure_ecs_asg_diff_kms_encrypt" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert && local.infrastructure_kms_encryption ? 1 : 0
role = aws_iam_role.ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda[0].name
policy_arn = aws_iam_policy.ecs_cluster_infrastructure_ecs_asg_diff_metric_kms_encrypt[0].arn
}
data "archive_file" "ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
type = "zip"
source_dir = "lambdas/ecs-asg-diff-metric"
output_path = "lambdas/.zip-cache/ecs-asg-diff-metric.zip"
}
resource "aws_lambda_function" "ecs_cluster_infrastructure_ecs_asg_diff_metric" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
filename = data.archive_file.ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda[0].output_path
function_name = "${local.resource_prefix_hash}-ecs-cluster-infrastructure-ecs-asg-diff-metric"
description = "${local.resource_prefix} ECS Cluster Infrastructure Container Instance / ASG Instance Difference Metric"
handler = "function.lambda_handler"
runtime = "python3.11"
role = aws_iam_role.ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda[0].arn
source_code_hash = data.archive_file.ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda[0].output_base64sha256
memory_size = 128
package_type = "Zip"
timeout = 900
environment {
variables = {
ecsClusterName = local.infrastructure_ecs_cluster_name
asgName = aws_autoscaling_group.infrastructure_ecs_cluster[0].name
}
}
tracing_config {
mode = "Active"
}
depends_on = [
aws_iam_role_policy_attachment.ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda,
aws_iam_role_policy_attachment.ecs_cluster_infrastructure_ecs_asg_diff_cloudwatch_metric_put_metric_data_lambda,
aws_iam_role_policy_attachment.ecs_cluster_infrastructure_ecs_asg_diff_metric_ecs_describe_cluster_lambda,
aws_iam_role_policy_attachment.ecs_cluster_infrastructure_ecs_asg_diff_kms_encrypt
]
}
resource "aws_cloudwatch_event_rule" "ecs_cluster_infrastructure_ecs_asg_diff_metric_1_min_cron" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
name = "${local.resource_prefix_hash}-ecs-cluster-infrastructure-ecs-asg-diff-metric-1-min"
description = "Triggers the ${aws_lambda_function.ecs_cluster_infrastructure_ecs_asg_diff_metric[0].function_name} Lambda every 1 minute"
schedule_expression = "rate(1 minute)"
}
resource "aws_cloudwatch_event_target" "ecs_cluster_infrastructure_ecs_asg_diff_metric_1_min_cron" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
rule = aws_cloudwatch_event_rule.ecs_cluster_infrastructure_ecs_asg_diff_metric_1_min_cron[0].name
target_id = "lambda"
arn = aws_lambda_function.ecs_cluster_infrastructure_ecs_asg_diff_metric[0].arn
}
resource "aws_lambda_permission" "ecs_cluster_infrastructure_ecs_asg_diff_metric_allow_cloudwatch_execution" {
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.ecs_cluster_infrastructure_ecs_asg_diff_metric[0].function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.ecs_cluster_infrastructure_ecs_asg_diff_metric_1_min_cron[0].arn
}