## Highlights
- Upgrades Aurora from `terraform-aws-modules/rds-aurora/aws` `7.6.0` to
`9.16.1`
- Bumps the Terraform minimum version from `>= 1.3.8` to `>= 1.5.0`
- Adds CMK support across newly exposed RDS, Redis, VPC, ECS, and Fleet
surfaces
- Adds additive per-key `extra_kms_policies` support across the root ->
`byo-vpc` -> `byo-db` -> `byo-ecs` path
- Moves software-installers KMS policy ownership out of
`addons/cloudfront-software-installers` and back into `byo-ecs`
- Extends monitoring and ECS integration to work with CMK-encrypted DB
password secrets
- Adds migration tooling and docs for Aurora storage KMS replacement and
CloudWatch Logs KMS cutovers
## Aurora module upgrade: `7.6.0` -> `9.16.1`
This PR keeps a single Aurora module path on upstream `9.16.1` and
updates the Fleet wrapper modules to preserve compatibility where
possible while exposing the new KMS and observability capabilities.
Notable changes handled in the wrapper:
- `security_group_rules`
- upstream moved from older SG rule addressing to the newer
`security_group_rules` model
- added `moved` handling for the confirmed SG rule address rename to
avoid unnecessary destroy/create churn
- `manage_master_user_password`
- the wrapper continues to own the DB password flow
- Aurora is kept on `manage_master_user_password = false`
- Secrets Manager integration remains wrapper-managed rather than
switching to upstream-managed password behavior
- `final_snapshot_identifier`
- wrapper support was added so old behavior is preserved
- if unset, the legacy generated naming pattern is still used
- callers can now override it explicitly
- Observability / Database Insights wiring
- cluster-level Performance Insights / Database Insights configuration
was added to match Aurora's current control surface
- monitoring behavior was aligned to cluster-level management for
consistency with the Aurora module and Database Insights requirements
- `backtrack_window`
- added optional pass-through support for Aurora MySQL backtracking
- validated as `null` or `0..259200`
## Terraform version bump
This PR raises the Terraform minimum version from `>= 1.3.8` to `>=
1.5.0`.
Reason:
- deprecation warnings for legacy KMS `enabled` flags now use Terraform
`check` blocks
- `check` blocks require Terraform `1.5+`
This was done so published interfaces can remain backward-compatible for
existing `enabled` users while still warning during plan/apply and
preferring `cmk_enabled`.
## KMS surface expansion and pass-through
This PR adds or expands CMK support for:
- Aurora storage
- Aurora password secret
- Aurora observability / Performance Insights
- Aurora exported log groups
- Redis at-rest encryption
- Redis log groups
- VPC flow log CloudWatch log groups
- ECS cluster log groups
- Fleet private-key secret
- Fleet Fargate ephemeral storage
- Fleet application logs
- Fleet software installers
It also adds additive `extra_kms_policies` support across those
module-owned CMK surfaces so callers can append narrowly scoped
statements without replacing the inherited base policy.
During validation of the cron-monitoring integration, I found that the
root module schema was still dropping some nested KMS attributes before
they reached `byo-vpc`. This PR now carries those fields all the way
through to their final destinations, including:
- `rds_config.*.extra_kms_policies`
- `redis_config.*.extra_kms_policies`
- `ecs_cluster.cloudwatch_log_group.kms.extra_kms_policies`
- `fleet_config.private_key_secret_kms.extra_kms_policies`
- `fleet_config.fargate_ephemeral_storage_kms.extra_kms_policies`
- `fleet_config.awslogs.kms.extra_kms_policies`
- `fleet_config.software_installers.extra_kms_policies`
- `fleet_config.software_installers.cloudfront_distribution_arn`
- `vpc.flow_log_cloudwatch_log_group_kms.extra_kms_policies`
## DB password secret KMS handoff to monitoring
`byo-vpc` now exposes:
- `rds_password_secret_kms_key_arn`
The monitoring addon now accepts:
- `cron_monitoring.mysql_password_secret_kms_key_arn`
The monitoring addon also now exposes:
- `cron_monitoring_lambda_arn`
- `cron_monitoring_lambda_role_arn`
This is needed so the cron-monitoring Lambda can decrypt the Fleet DB
password secret when that secret is encrypted with a customer-managed
KMS key.
Expected wiring:
```hcl
module "monitoring" {
# ...
cron_monitoring = {
# ...
mysql_password_secret_name = "${local.customer}-database-password"
mysql_password_secret_kms_key_arn = module.main.byo-vpc.rds_password_secret_kms_key_arn
}
}
```
## Software installers KMS ownership change
This PR changes the software-installers KMS contract:
- `addons/cloudfront-software-installers` no longer manages the
software-installers KMS key policy
- `byo-ecs` now owns the software-installers CMK policy, including
required service / principal statements
- `fleet_config.software_installers.cloudfront_distribution_arn` is the
static input used to add the CloudFront KMS statement without
introducing a module cycle
- `byo-ecs` also adds the explicit Fleet task-role KMS statement
required under the narrower external key-policy model
- the CloudFront addon exposes `cloudfront_distribution_arn` for
visibility, but that output should not be fed back into `byo-ecs`
because that would create a dependency cycle
Important compatibility note:
- if software installers uses KMS at all, it must use a customer-managed
key
- CloudFront requires a key-policy statement on that key
- the AWS-managed default key cannot be modified to attach that policy
## Other notable items
- Added and expanded migration docs for RDS and Redis KMS adoption
- Added `byo-vpc/scripts/rds_storage_kms_migration.sh` to automate
Aurora storage-KMS migration workflows
- Added `scripts/cloudwatch_logs_kms_migration.sh` to help identify and
optionally remove pre-cutover log streams after associating a new KMS
key with a CloudWatch Logs group
- Normalized `cmk_enabled` usage while preserving deprecated `enabled`
compatibility only for previously published interfaces
- Refactored `addons/logging-alb` KMS policy handling to support
`kms_base_policy` and align it with the structured KMS statement model
used elsewhere
- Reduced repeated KMS policy boilerplate within modules where it was
materially duplicated
## Compatibility notes
- Existing published KMS interfaces that previously used `enabled` still
accept it, but now warn and prefer `cmk_enabled`
- New KMS surfaces are `cmk_enabled`-only
- Software-installers KMS policy ownership moved from
`addons/cloudfront-software-installers` to `byo-ecs`
## Validation
Validated module stacks after the refactors:
- `root`
- `byo-vpc`
- `byo-db`
- `byo-ecs`
- `addons/monitoring`
- `addons/cloudfront-software-installers`
- `addons/logging-alb`
537 lines
20 KiB
Terraform
537 lines
20 KiB
Terraform
// Database alarms
|
|
resource "aws_cloudwatch_metric_alarm" "cpu_utilization_too_high" {
|
|
for_each = toset(var.mysql_cluster_members)
|
|
alarm_name = "rds_cpu_utilization_too_high-${var.customer_prefix}-${each.key}"
|
|
comparison_operator = "GreaterThanThreshold"
|
|
evaluation_periods = "1"
|
|
metric_name = "CPUUtilization"
|
|
namespace = "AWS/RDS"
|
|
period = "300"
|
|
statistic = "Average"
|
|
threshold = 80
|
|
alarm_description = "Average database CPU utilization over last 5 minutes too high"
|
|
alarm_actions = lookup(var.sns_topic_arns_map, "rds_cpu_untilizaton_too_high", var.default_sns_topic_arns)
|
|
ok_actions = lookup(var.sns_topic_arns_map, "rds_cpu_untilizaton_too_high", var.default_sns_topic_arns)
|
|
dimensions = {
|
|
DBInstanceIdentifier = each.key
|
|
}
|
|
}
|
|
|
|
resource "aws_db_event_subscription" "default" {
|
|
count = length(var.mysql_cluster_members) == 0 || (contains(keys(var.sns_topic_arns_map), "rds_db_event_subscription") == false && length(var.default_sns_topic_arns) == 0) ? 0 : 1
|
|
name = "rds-event-sub-${var.customer_prefix}"
|
|
sns_topic = try(var.sns_topic_arns_map.rds_db_event_subscription[0], var.default_sns_topic_arns[0])
|
|
|
|
source_type = "db-instance"
|
|
source_ids = var.mysql_cluster_members
|
|
|
|
event_categories = [
|
|
"failover",
|
|
"failure",
|
|
"low storage",
|
|
"maintenance",
|
|
"notification",
|
|
"recovery",
|
|
]
|
|
|
|
}
|
|
|
|
locals {
|
|
alb_map = { for k, v in var.albs : k => v }
|
|
}
|
|
|
|
// Log-based monitoring
|
|
resource "aws_cloudwatch_log_metric_filter" "log_monitoring" {
|
|
for_each = var.log_monitoring
|
|
|
|
name = "${var.customer_prefix}-log-monitor-${each.key}"
|
|
log_group_name = each.value.log_group_name
|
|
pattern = each.value.pattern
|
|
|
|
metric_transformation {
|
|
name = "${var.customer_prefix}-${each.key}"
|
|
namespace = "${var.customer_prefix}-logs"
|
|
value = "1"
|
|
unit = "Count"
|
|
}
|
|
}
|
|
|
|
resource "aws_cloudwatch_metric_alarm" "log_monitoring" {
|
|
for_each = var.log_monitoring
|
|
alarm_name = "${var.customer_prefix}-log-monitor-${each.key}"
|
|
comparison_operator = "GreaterThanOrEqualToThreshold"
|
|
evaluation_periods = each.value.evaluation_periods
|
|
metric_name = "${var.customer_prefix}-${each.key}"
|
|
namespace = "${var.customer_prefix}-logs"
|
|
period = each.value.period
|
|
statistic = "Sum"
|
|
threshold = each.value.threshold
|
|
treat_missing_data = "notBreaching"
|
|
alarm_actions = lookup(var.sns_topic_arns_map, "log_monitoring", var.default_sns_topic_arns)
|
|
ok_actions = lookup(var.sns_topic_arns_map, "log_monitoring", var.default_sns_topic_arns)
|
|
}
|
|
|
|
|
|
// ECS Alarms
|
|
resource "aws_cloudwatch_metric_alarm" "alb_healthyhosts" {
|
|
for_each = local.alb_map
|
|
alarm_name = "backend-healthyhosts-${var.customer_prefix}-${each.value.name}"
|
|
comparison_operator = "LessThanThreshold"
|
|
evaluation_periods = "1"
|
|
metric_name = "HealthyHostCount"
|
|
namespace = "AWS/ApplicationELB"
|
|
period = "60"
|
|
statistic = "Minimum"
|
|
threshold = each.value.min_containers
|
|
alarm_description = "This alarm indicates the number of Healthy Fleet hosts is lower than expected. Please investigate the load balancer \"${each.value.name}\" or the target group \"${each.value.target_group_name}\" and the fleet backend service \"${each.value.ecs_service_name}\""
|
|
actions_enabled = "true"
|
|
alarm_actions = lookup(var.sns_topic_arns_map, "alb_helthyhosts", var.default_sns_topic_arns)
|
|
ok_actions = lookup(var.sns_topic_arns_map, "alb_helthyhosts", var.default_sns_topic_arns)
|
|
dimensions = {
|
|
TargetGroup = each.value.target_group_arn_suffix
|
|
LoadBalancer = each.value.arn_suffix
|
|
}
|
|
}
|
|
|
|
// alarm for target response time (anomaly detection)
|
|
resource "aws_cloudwatch_metric_alarm" "target_response_time" {
|
|
for_each = local.alb_map
|
|
alarm_name = "backend-target-response-time-${var.customer_prefix}-${each.value.name}"
|
|
comparison_operator = "GreaterThanUpperThreshold"
|
|
evaluation_periods = "2"
|
|
threshold_metric_id = "e1"
|
|
alarm_description = "This alarm indicates the Fleet server response time is greater than it usually is. Please investigate the ecs service \"${var.fleet_ecs_service_name}\" because the backend might need to be scaled up."
|
|
alarm_actions = lookup(var.sns_topic_arns_map, "backend_response_time", var.default_sns_topic_arns)
|
|
ok_actions = lookup(var.sns_topic_arns_map, "backend_response_time", var.default_sns_topic_arns)
|
|
insufficient_data_actions = []
|
|
|
|
metric_query {
|
|
id = "e1"
|
|
expression = "ANOMALY_DETECTION_BAND(m1)"
|
|
label = "TargetResponseTime (Expected)"
|
|
return_data = "true"
|
|
}
|
|
|
|
metric_query {
|
|
id = "m1"
|
|
return_data = "true"
|
|
metric {
|
|
metric_name = "TargetResponseTime"
|
|
namespace = "AWS/ApplicationELB"
|
|
period = "120"
|
|
stat = "p99"
|
|
unit = "Count"
|
|
|
|
dimensions = {
|
|
TargetGroup = each.value.target_group_arn_suffix
|
|
LoadBalancer = each.value.arn_suffix
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
locals {
|
|
http_5xx_alert_names = ["HTTPCode_ELB_5XX_Count", "HTTPCode_Target_5XX_Count"]
|
|
http_5xx_alerts_list = flatten([for alert in local.http_5xx_alert_names : [for alb in var.albs : merge(alb, { "alert" : alert })]])
|
|
http_5xx_alerts = { for k, v in local.http_5xx_alerts_list : k => v }
|
|
}
|
|
|
|
|
|
resource "aws_cloudwatch_metric_alarm" "lb" {
|
|
for_each = local.http_5xx_alerts
|
|
alarm_name = "${var.customer_prefix}-lb-${each.value.name}-${each.value.alert}"
|
|
comparison_operator = "GreaterThanThreshold"
|
|
evaluation_periods = "1"
|
|
metric_name = each.value.alert
|
|
namespace = "AWS/ApplicationELB"
|
|
period = each.value.alert_thresholds[each.value.alert].period
|
|
statistic = "Sum"
|
|
threshold = each.value.alert_thresholds[each.value.alert].threshold
|
|
alarm_description = "This alarm indicates there are an abnormal amount of 5XX responses. Either the lb cannot talk with the Fleet backend target or Fleet is returning an error."
|
|
alarm_actions = lookup(var.sns_topic_arns_map, "alb_httpcode_5xx", var.default_sns_topic_arns)
|
|
ok_actions = lookup(var.sns_topic_arns_map, "alb_httpcode_5xx", var.default_sns_topic_arns)
|
|
treat_missing_data = "notBreaching"
|
|
dimensions = {
|
|
LoadBalancer = each.value.arn_suffix
|
|
}
|
|
}
|
|
|
|
|
|
// Elasticache (redis) alerts https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/CacheMetrics.WhichShouldIMonitor.html
|
|
resource "aws_cloudwatch_metric_alarm" "redis_cpu" {
|
|
for_each = toset(var.redis_cluster_members)
|
|
alarm_name = "redis-cpu-utilization-${each.key}-${var.customer_prefix}"
|
|
alarm_description = "Redis cluster CPU utilization node ${each.key}"
|
|
comparison_operator = "GreaterThanThreshold"
|
|
evaluation_periods = "1"
|
|
metric_name = "CPUUtilization"
|
|
namespace = "AWS/ElastiCache"
|
|
period = "300"
|
|
statistic = "Average"
|
|
alarm_actions = lookup(var.sns_topic_arns_map, "redis_cpu_utilization", var.default_sns_topic_arns)
|
|
ok_actions = lookup(var.sns_topic_arns_map, "redis_cpu_utilization", var.default_sns_topic_arns)
|
|
|
|
threshold = "70"
|
|
|
|
dimensions = {
|
|
CacheClusterId = each.key
|
|
}
|
|
|
|
}
|
|
|
|
resource "aws_cloudwatch_metric_alarm" "redis_cpu_engine_utilization" {
|
|
for_each = toset(var.redis_cluster_members)
|
|
alarm_name = "redis-cpu-engine-utilization-${each.key}-${var.customer_prefix}"
|
|
alarm_description = "Redis cluster CPU Engine utilization node ${each.key}"
|
|
comparison_operator = "GreaterThanThreshold"
|
|
evaluation_periods = "1"
|
|
metric_name = "EngineCPUUtilization"
|
|
namespace = "AWS/ElastiCache"
|
|
period = "300"
|
|
statistic = "Average"
|
|
alarm_actions = lookup(var.sns_topic_arns_map, "redis_cpu_engine_utilization", var.default_sns_topic_arns)
|
|
ok_actions = lookup(var.sns_topic_arns_map, "redis_cpu_engine_utilization", var.default_sns_topic_arns)
|
|
|
|
threshold = "25"
|
|
|
|
dimensions = {
|
|
CacheClusterId = each.key
|
|
}
|
|
|
|
}
|
|
|
|
resource "aws_cloudwatch_metric_alarm" "redis-database-memory-percentage" {
|
|
for_each = toset(var.redis_cluster_members)
|
|
alarm_name = "redis-database-memory-percentage-${each.key}-${var.customer_prefix}"
|
|
alarm_description = "Percentage of the memory available for the cluster that is in use. This is calculated using used_memory/maxmemory."
|
|
comparison_operator = "GreaterThanThreshold"
|
|
evaluation_periods = "1"
|
|
metric_name = "DatabaseMemoryUsagePercentage"
|
|
namespace = "AWS/ElastiCache"
|
|
period = "300"
|
|
statistic = "Average"
|
|
alarm_actions = lookup(var.sns_topic_arns_map, "redis_database_memory_percentage", var.default_sns_topic_arns)
|
|
ok_actions = lookup(var.sns_topic_arns_map, "redis_database_memory_percentage", var.default_sns_topic_arns)
|
|
|
|
threshold = "80"
|
|
|
|
dimensions = {
|
|
CacheClusterId = each.key
|
|
}
|
|
|
|
}
|
|
|
|
resource "aws_cloudwatch_metric_alarm" "redis-current-connections" {
|
|
for_each = toset(var.redis_cluster_members)
|
|
alarm_name = "redis-current-connections-${each.key}-${var.customer_prefix}"
|
|
alarm_description = "Redis current connections for node ${each.key}"
|
|
comparison_operator = "LessThanLowerOrGreaterThanUpperThreshold"
|
|
evaluation_periods = "5"
|
|
threshold_metric_id = "e1"
|
|
alarm_actions = lookup(var.sns_topic_arns_map, "redis_current_connections", var.default_sns_topic_arns)
|
|
ok_actions = lookup(var.sns_topic_arns_map, "redis_current_connections", var.default_sns_topic_arns)
|
|
insufficient_data_actions = []
|
|
|
|
metric_query {
|
|
id = "e1"
|
|
expression = "ANOMALY_DETECTION_BAND(m1,20)"
|
|
label = "Current Connections (Expected)"
|
|
return_data = "true"
|
|
}
|
|
|
|
metric_query {
|
|
id = "m1"
|
|
return_data = "true"
|
|
metric {
|
|
metric_name = "CurrConnections"
|
|
namespace = "AWS/ElastiCache"
|
|
period = "600"
|
|
stat = "Average"
|
|
unit = "Count"
|
|
|
|
dimensions = {
|
|
CacheClusterId = each.key
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "aws_cloudwatch_metric_alarm" "redis-replication-lag" {
|
|
for_each = toset(var.redis_cluster_members)
|
|
alarm_name = "redis-replication-lag-${each.key}-${var.customer_prefix}"
|
|
alarm_description = "This metric is only applicable for a node running as a read replica. It represents how far behind, in seconds, the replica is in applying changes from the primary node. For Redis engine version 5.0.6 onwards, the lag can be measured in milliseconds."
|
|
comparison_operator = "GreaterThanUpperThreshold"
|
|
evaluation_periods = "3"
|
|
threshold_metric_id = "e1"
|
|
alarm_actions = lookup(var.sns_topic_arns_map, "redis_replication_lag", var.default_sns_topic_arns)
|
|
ok_actions = lookup(var.sns_topic_arns_map, "redis_replication_lag", var.default_sns_topic_arns)
|
|
insufficient_data_actions = []
|
|
|
|
metric_query {
|
|
id = "e1"
|
|
expression = "ANOMALY_DETECTION_BAND(m1)"
|
|
label = "ReplicationLag (expected)"
|
|
return_data = "true"
|
|
}
|
|
|
|
metric_query {
|
|
id = "m1"
|
|
return_data = "true"
|
|
metric {
|
|
metric_name = "ReplicationLag"
|
|
namespace = "AWS/ElastiCache"
|
|
period = "300"
|
|
stat = "p90"
|
|
|
|
dimensions = {
|
|
CacheClusterId = each.key
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// ACM Certificate Manager
|
|
resource "aws_cloudwatch_metric_alarm" "acm_certificate_expired" {
|
|
count = var.acm_certificate_arn == null ? 0 : 1
|
|
alarm_name = "acm-cert-expiry-${var.customer_prefix}"
|
|
comparison_operator = "LessThanThreshold"
|
|
evaluation_periods = "1"
|
|
period = "86400" // 1 day in seconds
|
|
threshold = 30 // days
|
|
statistic = "Average"
|
|
namespace = "AWS/CertificateManager"
|
|
metric_name = "DaysToExpiry"
|
|
actions_enabled = "true"
|
|
alarm_description = "ACM Certificate will expire soon"
|
|
alarm_actions = lookup(var.sns_topic_arns_map, "acm_certificate_expired", var.default_sns_topic_arns)
|
|
ok_actions = lookup(var.sns_topic_arns_map, "acm_certificate_expired", var.default_sns_topic_arns)
|
|
|
|
dimensions = {
|
|
CertificateArn = var.acm_certificate_arn
|
|
}
|
|
}
|
|
|
|
// Cron Monitoring
|
|
locals {
|
|
cron_lambda_binary = "${path.module}/lambda/bootstrap"
|
|
}
|
|
|
|
resource "null_resource" "cron_monitoring_build" {
|
|
count = var.cron_monitoring == null ? 0 : 1
|
|
triggers = {
|
|
main_go_changes = filesha256("${path.module}/lambda/main.go"),
|
|
go_mod_changes = filesha256("${path.module}/lambda/go.mod")
|
|
go_sum_changes = filesha256("${path.module}/lambda/go.sum")
|
|
# Make sure to always have a unique trigger if the file doesn't exist
|
|
binary_exists = fileexists(local.cron_lambda_binary) ? true : timestamp()
|
|
}
|
|
provisioner "local-exec" {
|
|
working_dir = "${path.module}/lambda"
|
|
command = <<-EOT
|
|
go get
|
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags lambda.norpc -o bootstrap main.go
|
|
EOT
|
|
}
|
|
}
|
|
|
|
data "archive_file" "cron_monitoring_lambda" {
|
|
count = var.cron_monitoring == null ? 0 : 1
|
|
depends_on = [null_resource.cron_monitoring_build[0]]
|
|
type = "zip"
|
|
output_path = "${path.module}/lambda/.lambda.zip"
|
|
source_file = local.cron_lambda_binary
|
|
}
|
|
|
|
data "aws_secretsmanager_secret" "mysql_database_password" {
|
|
count = var.cron_monitoring == null ? 0 : 1
|
|
name = var.cron_monitoring.mysql_password_secret_name
|
|
}
|
|
|
|
resource "aws_security_group" "cron_monitoring" {
|
|
count = var.cron_monitoring == null ? 0 : 1
|
|
name = "${var.customer_prefix}_cron_monitoring"
|
|
description = "Security group for cron monitoring lambda (used by RDS to allow access in)"
|
|
vpc_id = var.cron_monitoring.vpc_id
|
|
|
|
egress {
|
|
from_port = 0
|
|
to_port = 0
|
|
protocol = "-1"
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
ipv6_cidr_blocks = ["::/0"]
|
|
}
|
|
}
|
|
|
|
resource "aws_security_group_rule" "cron_monitoring_to_rds" {
|
|
count = var.cron_monitoring == null ? 0 : 1
|
|
type = "ingress"
|
|
from_port = 3306
|
|
to_port = 3306
|
|
protocol = "tcp"
|
|
source_security_group_id = aws_security_group.cron_monitoring[0].id
|
|
security_group_id = var.cron_monitoring.rds_security_group_id
|
|
}
|
|
|
|
resource "aws_lambda_function" "cron_monitoring" {
|
|
count = var.cron_monitoring == null ? 0 : 1
|
|
|
|
depends_on = [
|
|
null_resource.cron_monitoring_build[0],
|
|
data.archive_file.cron_monitoring_lambda[0]
|
|
]
|
|
|
|
function_name = "${var.customer_prefix}_cron_monitoring"
|
|
runtime = "provided.al2"
|
|
memory_size = 256
|
|
timeout = 300
|
|
package_type = "Zip"
|
|
filename = data.archive_file.cron_monitoring_lambda[0].output_path
|
|
source_code_hash = data.archive_file.cron_monitoring_lambda[0].output_base64sha256
|
|
handler = "bootstrap"
|
|
reserved_concurrent_executions = 1
|
|
description = "This function has the ability to log into a production database and validate that the Fleet crons are running properly"
|
|
tracing_config {
|
|
mode = "Active"
|
|
}
|
|
|
|
vpc_config {
|
|
subnet_ids = var.cron_monitoring.subnet_ids
|
|
security_group_ids = [aws_security_group.cron_monitoring[0].id]
|
|
}
|
|
|
|
role = aws_iam_role.cron_monitoring_lambda[0].arn
|
|
|
|
environment {
|
|
variables = {
|
|
MYSQL_HOST = var.cron_monitoring.mysql_host
|
|
MYSQL_DATABASE = var.cron_monitoring.mysql_database
|
|
MYSQL_USER = var.cron_monitoring.mysql_user
|
|
MYSQL_SECRETSMANAGER_SECRET = data.aws_secretsmanager_secret.mysql_database_password[0].name
|
|
MYSQL_TLS_CONFIG = var.cron_monitoring.mysql_tls_config
|
|
CRON_SYSTEM_MONITOR_SNS_TOPIC_ARNS = join(",", lookup(var.sns_topic_arns_map, "cron_monitoring", var.default_sns_topic_arns))
|
|
CRON_JOB_FAILURE_MONITOR_SNS_TOPIC_ARNS = join(",", lookup(var.sns_topic_arns_map, "cron_job_failure_monitoring", var.default_sns_topic_arns))
|
|
FLEET_ENV = var.customer_prefix
|
|
CRON_DELAY_TOLERANCE = var.cron_monitoring.delay_tolerance
|
|
CRON_MONITOR_RUN_INTERVAL = var.cron_monitoring.run_interval
|
|
CRON_IGNORE_LIST = join(",", var.cron_monitoring.ignore_list)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// Lambda IAM
|
|
data "aws_iam_policy_document" "cron_monitoring_lambda_assume_role" {
|
|
statement {
|
|
actions = ["sts:AssumeRole"]
|
|
principals {
|
|
type = "Service"
|
|
identifiers = ["lambda.amazonaws.com"]
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "aws_iam_role_policy_attachment" "cron_monitoring_lambda" {
|
|
count = var.cron_monitoring == null ? 0 : 1
|
|
role = aws_iam_role.cron_monitoring_lambda[0].id
|
|
policy_arn = aws_iam_policy.cron_monitoring_lambda[0].arn
|
|
}
|
|
|
|
resource "aws_iam_role_policy_attachment" "cron_monitoring_lambda_managed" {
|
|
count = var.cron_monitoring == null ? 0 : 1
|
|
role = aws_iam_role.cron_monitoring_lambda[0].id
|
|
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
|
|
}
|
|
|
|
resource "aws_iam_policy" "cron_monitoring_lambda" {
|
|
count = var.cron_monitoring == null ? 0 : 1
|
|
name = "${var.customer_prefix}-cron-monitoring"
|
|
policy = data.aws_iam_policy_document.cron_monitoring_lambda.json
|
|
}
|
|
|
|
resource "aws_iam_role" "cron_monitoring_lambda" {
|
|
count = var.cron_monitoring == null ? 0 : 1
|
|
name = "${var.customer_prefix}-cron-monitoring-lambda"
|
|
assume_role_policy = data.aws_iam_policy_document.cron_monitoring_lambda_assume_role.json
|
|
}
|
|
|
|
data "aws_region" "current" {}
|
|
data "aws_caller_identity" "current" {}
|
|
|
|
data "aws_iam_policy_document" "cron_monitoring_lambda" {
|
|
statement {
|
|
|
|
sid = "SSMGetParameterPolicy"
|
|
|
|
actions = [
|
|
"secretsmanager:GetResourcePolicy",
|
|
"secretsmanager:DescribeSecret",
|
|
"secretsmanager:GetSecretValue"
|
|
]
|
|
|
|
resources = [data.aws_secretsmanager_secret.mysql_database_password[0].arn]
|
|
|
|
effect = "Allow"
|
|
|
|
}
|
|
|
|
dynamic "statement" {
|
|
for_each = var.cron_monitoring != null && try(var.cron_monitoring.mysql_password_secret_kms_key_arn, null) != null ? [var.cron_monitoring.mysql_password_secret_kms_key_arn] : []
|
|
content {
|
|
sid = "KMSDecryptMysqlPasswordSecret"
|
|
|
|
actions = [
|
|
"kms:Decrypt",
|
|
"kms:DescribeKey"
|
|
]
|
|
|
|
resources = [statement.value]
|
|
|
|
effect = "Allow"
|
|
}
|
|
}
|
|
|
|
statement {
|
|
sid = "SNSPublish"
|
|
|
|
actions = [
|
|
"sns:Publish"
|
|
]
|
|
|
|
resources = distinct(concat(
|
|
lookup(var.sns_topic_arns_map, "cron_monitoring", var.default_sns_topic_arns),
|
|
lookup(var.sns_topic_arns_map, "cron_job_failure_monitoring", var.default_sns_topic_arns)
|
|
))
|
|
|
|
effect = "Allow"
|
|
}
|
|
|
|
}
|
|
|
|
resource "aws_cloudwatch_log_group" "cron_monitoring_lambda" {
|
|
count = var.cron_monitoring == null ? 0 : 1
|
|
name = "/aws/lambda/${var.customer_prefix}-cron-monitoring"
|
|
retention_in_days = var.cron_monitoring.log_retention_in_days
|
|
|
|
}
|
|
|
|
resource "aws_cloudwatch_event_rule" "cron_monitoring_lambda" {
|
|
count = var.cron_monitoring == null ? 0 : 1
|
|
name = "${var.customer_prefix}-cron-monitoring"
|
|
schedule_expression = "rate(${var.cron_monitoring.run_interval})"
|
|
state = "ENABLED"
|
|
}
|
|
|
|
resource "aws_cloudwatch_event_target" "cron_monitoring_lambda" {
|
|
count = var.cron_monitoring == null ? 0 : 1
|
|
rule = aws_cloudwatch_event_rule.cron_monitoring_lambda[0].name
|
|
arn = aws_lambda_function.cron_monitoring[0].arn
|
|
}
|
|
|
|
resource "aws_lambda_permission" "cron_monitoring_cloudwatch" {
|
|
count = var.cron_monitoring == null ? 0 : 1
|
|
action = "lambda:InvokeFunction"
|
|
function_name = aws_lambda_function.cron_monitoring[0].id
|
|
principal = "events.amazonaws.com"
|
|
source_arn = aws_cloudwatch_event_rule.cron_monitoring_lambda[0].arn
|
|
}
|