|
| 1 | +data "aws_caller_identity" "current" {} |
| 2 | +data "aws_region" "current" {} |
| 3 | + |
| 4 | +data "archive_file" "zip_lambda_function" { |
| 5 | + type = "zip" |
| 6 | + source_file = "${path.module}/lambda_function.py" |
| 7 | + output_path = "${path.module}/lambda_function.zip" |
| 8 | +} |
| 9 | + |
| 10 | +resource "aws_secretsmanager_secret" "datadog_api_key" { |
| 11 | + name = "datadog_api_key" |
| 12 | + description = "Datadog API Key used for monitoring Lambda" |
| 13 | +} |
| 14 | + |
| 15 | +resource "aws_secretsmanager_secret_version" "datadog_api_key_version" { |
| 16 | + secret_id = aws_secretsmanager_secret.datadog_api_key.id |
| 17 | + secret_string = var.datadog_api_key # This should be the Datadog API key (input as a variable) |
| 18 | +} |
| 19 | + |
| 20 | +locals { |
| 21 | + function_name = "${var.deployment_name}-github-webhook-handler" |
| 22 | + role = aws_iam_role.lambda_role.arn |
| 23 | + handler = "lambda_function.lambda_handler" |
| 24 | + runtime = "python3.12" |
| 25 | + memory_size = 256 |
| 26 | + timeout = 30 |
| 27 | + publish = true |
| 28 | + |
| 29 | + filename = data.archive_file.zip_lambda_function.output_path |
| 30 | + source_code_hash = data.archive_file.zip_lambda_function.output_base64sha256 |
| 31 | + |
| 32 | + private_system_endpoint = "https://${var.private_system_endpoint}/integrations/github/v1/app_hook" |
| 33 | + |
| 34 | + subnet_ids = var.vpc_private_subnets |
| 35 | + security_group_ids = length(var.security_group_ids) > 0 ? var.security_group_ids : [aws_security_group.lambda_sg.id] |
| 36 | +} |
| 37 | + |
| 38 | +module "lambda_datadog" { |
| 39 | + count = var.monitor_lambda_datadog ? 1 : 0 |
| 40 | + |
| 41 | + source = "DataDog/lambda-datadog/aws" |
| 42 | + version = "1.4.0" |
| 43 | + |
| 44 | + function_name = local.function_name |
| 45 | + role = local.role |
| 46 | + handler = local.handler |
| 47 | + runtime = local.runtime |
| 48 | + memory_size = local.memory_size |
| 49 | + timeout = local.timeout |
| 50 | + publish = local.publish |
| 51 | + |
| 52 | + filename = local.filename |
| 53 | + source_code_hash = local.source_code_hash |
| 54 | + |
| 55 | + environment_variables = { |
| 56 | + "DD_API_KEY_SECRET_ARN" : aws_secretsmanager_secret.datadog_api_key.arn |
| 57 | + "DD_ENV" : var.environment |
| 58 | + "DD_SERVICE" : "github-webhook-service" |
| 59 | + "DD_SITE": "datadoghq.com" |
| 60 | + "DD_VERSION" : "1.0.0" |
| 61 | + "DD_EXTENSION_VERSION": "next" |
| 62 | + "DD_SERVERLESS_LOGS_ENABLED": "true" |
| 63 | + "DD_LOG_LEVEL": "INFO" |
| 64 | + "DD_TAGS": "deployment:${var.deployment_name}" |
| 65 | + "PRIVATE_SYSTEM_ENDPOINT" : local.private_system_endpoint |
| 66 | + "DATADOG_MONITORING_ENABLED": "true" |
| 67 | + } |
| 68 | + |
| 69 | + vpc_config_subnet_ids = local.subnet_ids |
| 70 | + vpc_config_security_group_ids = local.security_group_ids |
| 71 | + |
| 72 | + datadog_extension_layer_version = 63 |
| 73 | + datadog_python_layer_version = 98 |
| 74 | + |
| 75 | + # Depend on the zip operation |
| 76 | + depends_on = [data.archive_file.zip_lambda_function] |
| 77 | +} |
| 78 | + |
| 79 | +resource "aws_lambda_function" "github_webhook_handler" { |
| 80 | + count = var.monitor_lambda_datadog ? 0 : 1 |
| 81 | + |
| 82 | + function_name = local.function_name |
| 83 | + role = local.role |
| 84 | + handler = local.handler |
| 85 | + runtime = local.runtime |
| 86 | + memory_size = local.memory_size |
| 87 | + timeout = local.timeout |
| 88 | + publish = local.publish |
| 89 | + |
| 90 | + filename = local.filename |
| 91 | + source_code_hash = local.source_code_hash |
| 92 | + |
| 93 | + environment { |
| 94 | + variables = { |
| 95 | + PRIVATE_SYSTEM_ENDPOINT = local.private_system_endpoint |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + vpc_config { |
| 100 | + subnet_ids = local.subnet_ids |
| 101 | + security_group_ids = local.security_group_ids |
| 102 | + } |
| 103 | + |
| 104 | + # Depend on the zip operation |
| 105 | + depends_on = [data.archive_file.zip_lambda_function] |
| 106 | +} |
| 107 | + |
| 108 | +locals { |
| 109 | + function_version = coalesce(concat(module.lambda_datadog[*].version, aws_lambda_function.github_webhook_handler[*].version)...) |
| 110 | +} |
| 111 | + |
| 112 | +resource "aws_lambda_alias" "prod_alias" { |
| 113 | + name = "prod" |
| 114 | + function_name = local.function_name |
| 115 | + function_version = local.function_version |
| 116 | +} |
| 117 | + |
| 118 | +resource "aws_lambda_provisioned_concurrency_config" "example" { |
| 119 | + function_name = aws_lambda_alias.prod_alias.function_name |
| 120 | + provisioned_concurrent_executions = 1 |
| 121 | + qualifier = aws_lambda_alias.prod_alias.name |
| 122 | +} |
| 123 | + |
| 124 | +resource "aws_lambda_function_event_invoke_config" "concurrency_limit" { |
| 125 | + function_name = aws_lambda_alias.prod_alias.function_name |
| 126 | + qualifier = aws_lambda_alias.prod_alias.name |
| 127 | + maximum_retry_attempts = 2 |
| 128 | +} |
| 129 | + |
| 130 | +resource "aws_lambda_permission" "lambda_permission" { |
| 131 | + statement_id = "AllowExecutionFromAPIGateway" |
| 132 | + action = "lambda:InvokeFunction" |
| 133 | + function_name = aws_lambda_alias.prod_alias.function_name |
| 134 | + qualifier = aws_lambda_alias.prod_alias.name |
| 135 | + principal = "apigateway.amazonaws.com" |
| 136 | + |
| 137 | + # The /* part allows invocation from any stage, method and resource path |
| 138 | + # within API Gateway. |
| 139 | + source_arn = "${aws_api_gateway_rest_api.webhook_api.execution_arn}/*" |
| 140 | +} |
| 141 | + |
| 142 | +# API Gateway to act as a reverse proxy |
| 143 | +resource "aws_api_gateway_rest_api" "webhook_api" { |
| 144 | + name = "GitHub Webhook Reverse Proxy" |
| 145 | +} |
| 146 | + |
| 147 | +# Create resource for webhooks |
| 148 | +resource "aws_api_gateway_resource" "webhook_resource" { |
| 149 | + rest_api_id = aws_api_gateway_rest_api.webhook_api.id |
| 150 | + parent_id = aws_api_gateway_rest_api.webhook_api.root_resource_id |
| 151 | + path_part = "webhook" |
| 152 | +} |
| 153 | + |
| 154 | +# API Gateway Method |
| 155 | +resource "aws_api_gateway_method" "post_webhook" { |
| 156 | + rest_api_id = aws_api_gateway_rest_api.webhook_api.id |
| 157 | + resource_id = aws_api_gateway_resource.webhook_resource.id |
| 158 | + http_method = "POST" |
| 159 | + authorization = "NONE" |
| 160 | +} |
| 161 | + |
| 162 | +# Lambda integration for API Gateway |
| 163 | +resource "aws_api_gateway_integration" "lambda_integration" { |
| 164 | + rest_api_id = aws_api_gateway_rest_api.webhook_api.id |
| 165 | + resource_id = aws_api_gateway_resource.webhook_resource.id |
| 166 | + http_method = aws_api_gateway_method.post_webhook.http_method |
| 167 | + integration_http_method = "POST" |
| 168 | + type = "AWS_PROXY" |
| 169 | + uri = aws_lambda_alias.prod_alias.invoke_arn |
| 170 | +} |
| 171 | + |
| 172 | +# Deployment of API Gateway |
| 173 | +resource "aws_api_gateway_deployment" "api_deployment" { |
| 174 | + rest_api_id = aws_api_gateway_rest_api.webhook_api.id |
| 175 | + stage_name = "prod" |
| 176 | + |
| 177 | + depends_on = [aws_api_gateway_integration.lambda_integration] |
| 178 | +} |
| 179 | + |
| 180 | +resource "aws_api_gateway_rest_api_policy" "github_ip_restriction" { |
| 181 | + rest_api_id = aws_api_gateway_rest_api.webhook_api.id |
| 182 | + |
| 183 | + policy = jsonencode({ |
| 184 | + Version = "2012-10-17", |
| 185 | + Statement = [ |
| 186 | + { |
| 187 | + Effect: "Allow", |
| 188 | + Principal: "*", |
| 189 | + Action: "execute-api:Invoke", |
| 190 | + Resource: "arn:aws:execute-api:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${aws_api_gateway_rest_api.webhook_api.id}/*/POST/*", |
| 191 | + }, |
| 192 | + { |
| 193 | + Effect: "Deny", |
| 194 | + Principal: "*", |
| 195 | + Action: "execute-api:Invoke", |
| 196 | + Resource: "arn:aws:execute-api:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${aws_api_gateway_rest_api.webhook_api.id}/*/POST/*", |
| 197 | + Condition: { |
| 198 | + "NotIpAddress": { |
| 199 | + "aws:SourceIp": var.github_cidrs |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + ] |
| 204 | + }) |
| 205 | +} |
0 commit comments