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 #109 from dxw/conditionally-create-ecs-container-i…
…nstance-asg-instance-diff-alert Conditionally create ECS Container Instance / ASG Instance diff alert
- Loading branch information
Showing
8 changed files
with
345 additions
and
2 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_ecs_asg_diff" { | ||
count = local.enable_infrastructure_ecs_cluster_ecs_asg_diff_alert ? 1 : 0 | ||
|
||
alarm_name = "${local.resource_prefix}-infrastructure-ecs-cluster-infrastructure-ecs-asg-diff" | ||
comparison_operator = "GreaterThanOrEqualToThreshold" | ||
evaluation_periods = local.infrastructure_ecs_cluster_ecs_asg_diff_alert_evaluation_periods | ||
metric_name = "ContainerInstanceAsgInstanceDiff" | ||
namespace = "ECS" | ||
period = local.infrastructure_ecs_cluster_ecs_asg_diff_alert_period | ||
statistic = "Maximum" | ||
threshold = local.infrastructure_ecs_cluster_ecs_asg_diff_alert_threshold | ||
alarm_description = "Container Instance / ASG Instance Difference for ${aws_ecs_cluster.infrastructure[0].name} Cluster" | ||
actions_enabled = "true" | ||
alarm_actions = concat( | ||
local.infrastructure_ecs_cluster_ecs_asg_diff_alert_slack ? [data.aws_sns_topic.infrastructure_slack_sns_topic[0].arn] : [], | ||
local.infrastructure_ecs_cluster_ecs_asg_diff_alert_opsgenie ? [data.aws_sns_topic.infrastructure_opsgenie_sns_topic[0].arn] : [] | ||
) | ||
ok_actions = concat( | ||
local.infrastructure_ecs_cluster_ecs_asg_diff_alert_slack ? [data.aws_sns_topic.infrastructure_slack_sns_topic[0].arn] : [], | ||
local.infrastructure_ecs_cluster_ecs_asg_diff_alert_opsgenie ? [data.aws_sns_topic.infrastructure_opsgenie_sns_topic[0].arn] : [] | ||
) | ||
dimensions = { | ||
ClusterName = aws_ecs_cluster.infrastructure[0].name | ||
} | ||
} |
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,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 | ||
} |
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,51 @@ | ||
import boto3 | ||
import os | ||
|
||
CLUSTER_NAME = os.environ['ecsClusterName'] | ||
ASG_NAME = os.environ['asgName'] | ||
|
||
ecs = boto3.client('ecs') | ||
autoscaling = boto3.client('autoscaling') | ||
cloudwatch = boto3.client('cloudwatch') | ||
|
||
def lambda_handler(event, context): | ||
ecs_response = ecs.describe_clusters( | ||
clusters=[CLUSTER_NAME], | ||
) | ||
|
||
if not ecs_response['clusters']: | ||
return {'statusCode': 200, 'body': 'No ECS cluster found with the given name.'} | ||
|
||
ecs_instance_count = ecs_response['clusters'][0]['registeredContainerInstancesCount'] | ||
|
||
asg_response = autoscaling.describe_auto_scaling_groups( | ||
AutoScalingGroupNames=[ASG_NAME], | ||
) | ||
|
||
if not asg_response['AutoScalingGroups']: | ||
return {'statusCode': 200, 'body': 'No Auto Scaling Group found with the given name.'} | ||
|
||
asg_instance_count = len(asg_response['AutoScalingGroups'][0]['Instances']) | ||
|
||
instance_diff = asg_instance_count - ecs_instance_count | ||
|
||
cloudwatch.put_metric_data( | ||
Namespace="ECS", | ||
MetricData=[ | ||
{ | ||
'MetricName': "ContainerInstanceAsgInstanceDiff", | ||
'Dimensions': [ | ||
{ | ||
'Name': 'ClusterName', | ||
'Value': CLUSTER_NAME | ||
}, | ||
], | ||
'Value': instance_diff, | ||
'Unit': 'Count' | ||
}, | ||
] | ||
) | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': f'Container Instance / ASG Instance difference ({instance_diff}) calculated and published successfully.'} |
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,12 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"autoscaling:DescribeAutoScalingGroups" | ||
], | ||
"Effect": "Allow", | ||
"Resource": "*" | ||
} | ||
] | ||
} |
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