generated from dxw/terraform-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from dxw/conditionally-create-ecs-cluster-pen…
…ding-task-alert Conditionally create ECS Cluster Pending Task Alert
- Loading branch information
Showing
9 changed files
with
350 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
resource "aws_cloudwatch_metric_alarm" "infrastructure_ecs_cluster_pending_task" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert ? 1 : 0 | ||
|
||
alarm_name = "${local.resource_prefix}-infrastructure-ecs-cluster-infrastructure-pending-task" | ||
comparison_operator = "GreaterThanOrEqualToThreshold" | ||
evaluation_periods = local.infrastructure_ecs_cluster_pending_task_alert_evaluation_periods | ||
metric_name = "PendingTasksCount" | ||
namespace = "ECS" | ||
period = local.infrastructure_ecs_cluster_pending_task_alert_period | ||
statistic = "Maximum" | ||
threshold = local.infrastructure_ecs_cluster_pending_task_alert_threshold | ||
alarm_description = "Pending Tasks for ${aws_ecs_cluster.infrastructure[0].name} Cluster" | ||
actions_enabled = "true" | ||
alarm_actions = concat( | ||
local.infrastructure_ecs_cluster_pending_task_alert_slack ? [data.aws_sns_topic.infrastructure_slack_sns_topic[0].arn] : [], | ||
local.infrastructure_ecs_cluster_pending_task_alert_opsgenie ? [data.aws_sns_topic.infrastructure_opsgenie_sns_topic[0].arn] : [] | ||
) | ||
ok_actions = concat( | ||
local.infrastructure_ecs_cluster_pending_task_alert_slack ? [data.aws_sns_topic.infrastructure_slack_sns_topic[0].arn] : [], | ||
local.infrastructure_ecs_cluster_pending_task_alert_opsgenie ? [data.aws_sns_topic.infrastructure_opsgenie_sns_topic[0].arn] : [] | ||
) | ||
dimensions = { | ||
ClusterName = aws_ecs_cluster.infrastructure[0].name | ||
} | ||
} |
164 changes: 164 additions & 0 deletions
164
ecs-cluster-infrastructure-pending-task-metric-lambda.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
resource "aws_cloudwatch_log_group" "ecs_cluster_infrastructure_pending_task_metric_lambda_log_group" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert ? 1 : 0 | ||
|
||
name = "/aws/lambda/${local.resource_prefix_hash}-ecs-cluster-infrastructure-pending-task-metric" | ||
kms_key_id = local.infrastructure_kms_encryption ? aws_kms_key.infrastructure[0].arn : null | ||
retention_in_days = local.infrastructure_ecs_cluster_pending_task_metric_lambda_log_retention | ||
} | ||
|
||
resource "aws_iam_role" "ecs_cluster_infrastructure_pending_task_metric_lambda" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-${substr(sha512("ecs-cluster-infrastructure-pending-task-metric"), 0, 6)}" | ||
description = "${local.resource_prefix}-ecs-cluster-infrastructure-pending-task-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_pending_task_metric_lambda" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-ecs-cluster-infrastructure-pending-task-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-pending-task-metric" | ||
} | ||
) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "ecs_cluster_infrastructure_pending_task_metric_lambda" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert ? 1 : 0 | ||
|
||
role = aws_iam_role.ecs_cluster_infrastructure_pending_task_metric_lambda[0].name | ||
policy_arn = aws_iam_policy.ecs_cluster_infrastructure_pending_task_metric_lambda[0].arn | ||
} | ||
|
||
resource "aws_iam_policy" "ecs_cluster_infrastructure_pending_task_metric_cloudwatch_put_metric_data_lambda" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-ecs-cluster-infrastructure-pending-task-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_pending_task_cloudwatch_metric_put_metric_data_lambda" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert ? 1 : 0 | ||
|
||
role = aws_iam_role.ecs_cluster_infrastructure_pending_task_metric_lambda[0].name | ||
policy_arn = aws_iam_policy.ecs_cluster_infrastructure_pending_task_metric_cloudwatch_put_metric_data_lambda[0].arn | ||
} | ||
|
||
resource "aws_iam_policy" "ecs_cluster_infrastructure_pending_task_metric_ecs_describe_cluster_lambda" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-ecs-cluster-infrastructure-pending-task-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_pending_task_metric_ecs_describe_cluster_lambda" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert ? 1 : 0 | ||
|
||
role = aws_iam_role.ecs_cluster_infrastructure_pending_task_metric_lambda[0].name | ||
policy_arn = aws_iam_policy.ecs_cluster_infrastructure_pending_task_metric_ecs_describe_cluster_lambda[0].arn | ||
} | ||
|
||
resource "aws_iam_policy" "ecs_cluster_infrastructure_pending_task_metric_kms_encrypt" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert && local.infrastructure_kms_encryption ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-ecs-cluster-infrastructure-pending-task-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_pending_task_kms_encrypt" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert && local.infrastructure_kms_encryption ? 1 : 0 | ||
|
||
role = aws_iam_role.ecs_cluster_infrastructure_pending_task_metric_lambda[0].name | ||
policy_arn = aws_iam_policy.ecs_cluster_infrastructure_pending_task_metric_kms_encrypt[0].arn | ||
} | ||
|
||
data "archive_file" "ecs_cluster_infrastructure_pending_task_metric_lambda" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert ? 1 : 0 | ||
|
||
type = "zip" | ||
source_dir = "lambdas/ecs-pending-task-metric" | ||
output_path = "lambdas/.zip-cache/ecs-pending-task-metric.zip" | ||
} | ||
|
||
resource "aws_lambda_function" "ecs_cluster_infrastructure_pending_task_metric" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert ? 1 : 0 | ||
|
||
filename = data.archive_file.ecs_cluster_infrastructure_pending_task_metric_lambda[0].output_path | ||
function_name = "${local.resource_prefix_hash}-ecs-cluster-infrastructure-pending-task-metric" | ||
description = "${local.resource_prefix} ECS Cluster Infrastructure Pending Task Metric" | ||
handler = "function.lambda_handler" | ||
runtime = "python3.11" | ||
role = aws_iam_role.ecs_cluster_infrastructure_pending_task_metric_lambda[0].arn | ||
source_code_hash = data.archive_file.ecs_cluster_infrastructure_pending_task_metric_lambda[0].output_base64sha256 | ||
memory_size = 128 | ||
package_type = "Zip" | ||
timeout = 900 | ||
|
||
environment { | ||
variables = { | ||
ecsClusterName = local.infrastructure_ecs_cluster_name | ||
} | ||
} | ||
|
||
tracing_config { | ||
mode = "Active" | ||
} | ||
|
||
depends_on = [ | ||
aws_iam_role_policy_attachment.ecs_cluster_infrastructure_pending_task_metric_lambda, | ||
aws_iam_role_policy_attachment.ecs_cluster_infrastructure_pending_task_cloudwatch_metric_put_metric_data_lambda, | ||
aws_iam_role_policy_attachment.ecs_cluster_infrastructure_pending_task_metric_ecs_describe_cluster_lambda, | ||
aws_iam_role_policy_attachment.ecs_cluster_infrastructure_pending_task_kms_encrypt | ||
] | ||
} | ||
|
||
resource "aws_cloudwatch_event_rule" "ecs_cluster_infrastructure_pending_task_metric_1_min_cron" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert ? 1 : 0 | ||
|
||
name = "${local.resource_prefix_hash}-ecs-cluster-infrastructure-pending-task-metric-1-min" | ||
description = "Triggers the ${aws_lambda_function.ecs_cluster_infrastructure_pending_task_metric[0].function_name} Lambda every 1 minute" | ||
schedule_expression = "rate(1 minute)" | ||
} | ||
|
||
resource "aws_cloudwatch_event_target" "ecs_cluster_infrastructure_pending_task_metric_1_min_cron" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert ? 1 : 0 | ||
|
||
rule = aws_cloudwatch_event_rule.ecs_cluster_infrastructure_pending_task_metric_1_min_cron[0].name | ||
target_id = "lambda" | ||
arn = aws_lambda_function.ecs_cluster_infrastructure_pending_task_metric[0].arn | ||
} | ||
|
||
resource "aws_lambda_permission" "ecs_cluster_infrastructure_pending_task_metric_allow_cloudwatch_execution" { | ||
count = local.enable_infrastructure_ecs_cluster_pending_task_alert ? 1 : 0 | ||
|
||
statement_id = "AllowExecutionFromCloudWatch" | ||
action = "lambda:InvokeFunction" | ||
function_name = aws_lambda_function.ecs_cluster_infrastructure_pending_task_metric[0].function_name | ||
principal = "events.amazonaws.com" | ||
source_arn = aws_cloudwatch_event_rule.ecs_cluster_infrastructure_pending_task_metric_1_min_cron[0].arn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import boto3 | ||
import os | ||
|
||
CLUSTER_NAME = os.environ['ecsClusterName'] | ||
|
||
def lambda_handler(event, context): | ||
ecs_client = boto3.client('ecs') | ||
cloudwatch_client = boto3.client('cloudwatch') | ||
|
||
response = ecs_client.describe_clusters( | ||
clusters=[CLUSTER_NAME] | ||
) | ||
|
||
pending_tasks = response['clusters'][0]['pendingTasksCount'] | ||
|
||
response = cloudwatch_client.put_metric_data( | ||
Namespace='ECS', | ||
MetricData=[ | ||
{ | ||
'MetricName': 'PendingTasksCount', | ||
'Dimensions': [ | ||
{ | ||
'Name': 'ClusterName', | ||
'Value': CLUSTER_NAME | ||
}, | ||
], | ||
'Value': pending_tasks, | ||
'Unit': 'Count' | ||
}, | ||
] | ||
) | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': f'Successfully created custom metric for {CLUSTER_NAME} with {pending_tasks} pending tasks' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"cloudwatch:PutMetricData" | ||
], | ||
"Effect": "Allow", | ||
"Resource": "*", | ||
"Condition": { | ||
"ForAnyValue:StringEquals": { | ||
"cloudwatch:namespace": [ | ||
%{for k, v in namespaces} | ||
"${v}"%{if k+1 != length(namespaces)},%{endif} | ||
%{endfor} | ||
] | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"ecs:DescribeClusters" | ||
], | ||
"Effect": "Allow", | ||
"Resource": [ | ||
%{for k, v in cluster_names} | ||
"arn:aws:ecs:${region}:${account_id}:cluster/${v}"%{if k+1 != length(cluster_names)},%{endif} | ||
%{endfor} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters