11data "aws_caller_identity" "current" {}
22data "aws_region" "current" {}
33
4- # resource "null_resource" "zip_lambda_function" {
5- # provisioner "local-exec" {
6- # command = "zip -j ${path.module}/lambda_function.zip ${path.module}/lambda_function.py"
7- # }
8-
9- # triggers = {
10- # py_source = filemd5("${path.module}/lambda_function.py")
11- # }
12- # }
13-
144data "archive_file" "zip_lambda_function" {
155 type = " zip"
166 source_file = " ${ path . module } /lambda_function.py"
177 output_path = " ${ path . module } /lambda_function.zip"
188}
199
20-
2110resource "aws_secretsmanager_secret" "datadog_api_key" {
2211 name = " datadog_api_key"
2312 description = " Datadog API Key used for monitoring Lambda"
@@ -28,22 +17,41 @@ resource "aws_secretsmanager_secret_version" "datadog_api_key_version" {
2817 secret_string = var. datadog_api_key # This should be the Datadog API key (input as a variable)
2918}
3019
31- module "lambda_datadog" {
32- source = " DataDog/lambda-datadog/aws"
33- version = " 1.4.0"
34-
20+ locals {
3521 function_name = " ${ var . deployment_name } -github-webhook-handler"
3622 role = aws_iam_role. lambda_role . arn
3723 handler = " lambda_function.lambda_handler"
3824 runtime = " python3.12"
3925 memory_size = 256
4026 timeout = 30
41-
42- publish = true
27+ publish = true
4328
4429 filename = data. archive_file . zip_lambda_function . output_path
4530 source_code_hash = data. archive_file . zip_lambda_function . output_base64sha256
4631
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+
4755 environment_variables = {
4856 " DD_API_KEY_SECRET_ARN" : aws_secretsmanager_secret.datadog_api_key.arn
4957 " DD_ENV" : var.environment
@@ -54,11 +62,12 @@ module "lambda_datadog" {
5462 " DD_SERVERLESS_LOGS_ENABLED" : " true"
5563 " DD_LOG_LEVEL" : " INFO"
5664 " DD_TAGS" : " deployment:${ var . deployment_name } "
57- " PRIVATE_SYSTEM_ENDPOINT" : " https://${ var . private_system_endpoint } /integrations/github/v1/app_hook"
65+ " PRIVATE_SYSTEM_ENDPOINT" : local.private_system_endpoint
66+ " DATADOG_MONITORING_ENABLED" : " true"
5867 }
5968
60- vpc_config_subnet_ids = var . vpc_private_subnets
61- vpc_config_security_group_ids = length (var . security_group_ids ) > 0 ? var . security_group_ids : [ aws_security_group . lambda_sg . id ]
69+ vpc_config_subnet_ids = local . subnet_ids
70+ vpc_config_security_group_ids = local . security_group_ids
6271
6372 datadog_extension_layer_version = 63
6473 datadog_python_layer_version = 98
@@ -67,10 +76,43 @@ module "lambda_datadog" {
6776 depends_on = [data . archive_file . zip_lambda_function ]
6877}
6978
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+
70112resource "aws_lambda_alias" "prod_alias" {
71113 name = " prod"
72- function_name = module . lambda_datadog . function_name
73- function_version = module . lambda_datadog . version
114+ function_name = local . function_name
115+ function_version = local . function_version
74116}
75117
76118resource "aws_lambda_provisioned_concurrency_config" "example" {
0 commit comments