|
| 1 | +resource aws_cloudwatch_event_rule slowlog_check { |
| 2 | + name_prefix = "slowlog_check_every_minute" |
| 3 | + description = "Check for slowlogs every five minutes" |
| 4 | + schedule_expression = "cron(0/5 * * * ? *)" # every 5 minute |
| 5 | + tags = var.tags |
| 6 | +} |
| 7 | + |
| 8 | +resource aws_cloudwatch_event_target slowlog_check { |
| 9 | + rule = aws_cloudwatch_event_rule.slowlog_check.name |
| 10 | + arn = aws_lambda_function.slowlog_check.arn |
| 11 | +} |
| 12 | + |
| 13 | +resource aws_lambda_permission slowlog_check { |
| 14 | + statement_id = "AllowExecutionFromCloudWatch" |
| 15 | + action = "lambda:InvokeFunction" |
| 16 | + function_name = aws_lambda_function.slowlog_check.function_name |
| 17 | + principal = "events.amazonaws.com" |
| 18 | + source_arn = aws_cloudwatch_event_rule.slowlog_check.arn |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +resource aws_iam_role slowlog_check { |
| 23 | + name = "slowlog_check" |
| 24 | + |
| 25 | + assume_role_policy = <<EOF |
| 26 | +{ |
| 27 | + "Version": "2012-10-17", |
| 28 | + "Statement": [ |
| 29 | + { |
| 30 | + "Action": "sts:AssumeRole", |
| 31 | + "Principal": { |
| 32 | + "Service": "lambda.amazonaws.com" |
| 33 | + }, |
| 34 | + "Effect": "Allow", |
| 35 | + "Sid": "" |
| 36 | + } |
| 37 | + ] |
| 38 | +} |
| 39 | +EOF |
| 40 | +} |
| 41 | + |
| 42 | +resource aws_iam_policy slowlog_check { |
| 43 | + name = "slowlog_check" |
| 44 | + path = "/" |
| 45 | + description = "This IAM policy allows the slowlog_check to run" |
| 46 | + |
| 47 | + policy = <<EOF |
| 48 | +{ |
| 49 | + "Version": "2012-10-17", |
| 50 | + "Statement": [ |
| 51 | + { |
| 52 | + "Action": [ |
| 53 | + "ssm:GetParameter", |
| 54 | + "ssm:GetParametersByPath" |
| 55 | + ], |
| 56 | + "Resource": [ |
| 57 | + "arn:aws:ssm:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:parameter/${var.ssm_path}", |
| 58 | + "arn:aws:ssm:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:parameter/${var.ssm_path}/*" |
| 59 | + ], |
| 60 | + "Effect": "Allow" |
| 61 | + } |
| 62 | + ] |
| 63 | +} |
| 64 | +EOF |
| 65 | +} |
| 66 | + |
| 67 | +resource aws_iam_role_policy_attachment "lambda_vpc_0" { |
| 68 | + role = aws_iam_role.slowlog_check.name |
| 69 | + policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole" |
| 70 | +} |
| 71 | + |
| 72 | +resource aws_iam_role_policy_attachment "lambda_vpc_1" { |
| 73 | + role = aws_iam_role.slowlog_check.name |
| 74 | + policy_arn = aws_iam_policy.slowlog_check.arn |
| 75 | +} |
| 76 | + |
| 77 | +resource aws_security_group egress { |
| 78 | + name_prefix = "egress-https" |
| 79 | + description = "Allow outbound https calls" |
| 80 | + vpc_id = var.vpc_id |
| 81 | + |
| 82 | + # https://github.com/hashicorp/terraform/issues/8617#issuecomment-343973544-permalink |
| 83 | + lifecycle { |
| 84 | + create_before_destroy = true |
| 85 | + } |
| 86 | + |
| 87 | + egress { |
| 88 | + description = "outbound https" |
| 89 | + from_port = 443 |
| 90 | + to_port = 443 |
| 91 | + protocol = "tcp" |
| 92 | + cidr_blocks = ["0.0.0.0/0"] |
| 93 | + } |
| 94 | + tags = var.tags |
| 95 | +} |
| 96 | + |
| 97 | +resource null_resource get_slowlog_archive { |
| 98 | + provisioner local-exec { |
| 99 | + command = "wget https://github.com/scribd/elasticache-slowlog-to-datadog/releases/download/v1.0.0/slowlog_check.1.0.0.zip" |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +resource aws_ssm_parameter datadog_api_key { |
| 104 | + name = "/${var.ssm_path}/DATADOG_API_KEY" |
| 105 | + description = "Datadog API Key" |
| 106 | + tags = var.tags |
| 107 | + type = "SecureString" |
| 108 | + value = var.datadog_api_key |
| 109 | +} |
| 110 | + |
| 111 | +resource aws_ssm_parameter datadog_app_key { |
| 112 | + name = "/${var.ssm_path}/DATADOG_APP_KEY" |
| 113 | + description = "Datadog App Key" |
| 114 | + tags = var.tags |
| 115 | + type = "SecureString" |
| 116 | + value = var.datadog_app_key |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +resource "aws_lambda_function" "slowlog_check" { |
| 121 | + function_name = "slowlog_check" |
| 122 | + filename = "${path.module}/slowlog_check.1.0.0.zip" |
| 123 | + source_code_hash = "MDgxYjVkZmMyNDkzODg1ZDJiMzBiY2FmYWI5NWNkMTQ1MjQ0Y2ViNDkzZTFhM2I3OGFhMmU3MzZiOWFhZTJiMw==" |
| 124 | + role = aws_iam_role.slowlog_check.arn |
| 125 | + handler = "lambda_function.lambda_handler" |
| 126 | + runtime = "ruby2.5" |
| 127 | + vpc_config { |
| 128 | + subnet_ids = var.subnet_ids |
| 129 | + security_group_ids = concat([aws_security_group.egress.id], var.elasticache_security_groups) |
| 130 | + } |
| 131 | + timeout = 600 |
| 132 | + |
| 133 | + environment { |
| 134 | + variables = { |
| 135 | + REDIS_HOST = var.elasticache_endpoint |
| 136 | + SSM_PATH = "/${var.ssm_path}/" |
| 137 | + NAMESPACE = var.namespace |
| 138 | + ENV = var.env |
| 139 | + METRICNAME = var.metric_name |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + tags = var.tags |
| 144 | + depends_on = [null_resource.get_slowlog_archive] |
| 145 | +} |
| 146 | + |
| 147 | +resource aws_lambda_function_event_invoke_config slowlog_check { |
| 148 | + function_name = aws_lambda_function.slowlog_check.function_name |
| 149 | + maximum_retry_attempts = 0 |
| 150 | +} |
0 commit comments