Skip to content

Commit 5a7e5a5

Browse files
committed
feat: Make Lambda DataDog monitoring optional
1 parent 89c4d04 commit 5a7e5a5

File tree

5 files changed

+96
-29
lines changed

5 files changed

+96
-29
lines changed

modules/github_reverse_proxy/lambda_function.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import os
44
import urllib3
55

6-
from datadog_lambda.logger import initialize_logging
76

8-
9-
initialize_logging(__name__)
10-
logger = logging.getLogger(__name__)
11-
# logger.setLevel(logging.INFO)
7+
if os.environ.get('DATADOG_MONITORING_ENABLED', None):
8+
from datadog_lambda.logger import initialize_logging
9+
initialize_logging(__name__)
10+
logger = logging.getLogger(__name__)
11+
else:
12+
logger = logging.getLogger()
13+
logger.setLevel(logging.INFO)
1214

1315

1416
def forward_to_private_system(data, private_endpoint, headers):
48 Bytes
Binary file not shown.

modules/github_reverse_proxy/main.tf

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
data "aws_caller_identity" "current" {}
22
data "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-
144
data "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-
2110
resource "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+
70112
resource "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

76118
resource "aws_lambda_provisioned_concurrency_config" "example" {

modules/github_reverse_proxy/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,22 @@ variable "private_system_endpoint" {
5151
type = string
5252
}
5353

54+
variable "monitor_lambda_datadog" {
55+
description = "Whether to monitor the Lambda with Datadog"
56+
type = bool
57+
default = true
58+
}
59+
5460
variable "datadog_api_key" {
5561
description = "The API key for Datadog"
5662
type = string
63+
default = ""
5764
sensitive = true
65+
66+
validation {
67+
condition = var.monitor_lambda_datadog ? length(var.datadog_api_key) > 0 : true
68+
error_message = "Datadog API key must be provided when monitoring Lambda with Datadog"
69+
}
5870
}
5971

6072
variable "use_private_egress" {

variables.tf

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,9 +763,20 @@ variable "github_cidrs" {
763763
description = "List of CIDRs that are allowed to connect to the github reverse proxy"
764764
}
765765

766+
variable "monitor_lambda_datadog" {
767+
description = "Whether to monitor the Lambda with Datadog"
768+
type = bool
769+
default = true
770+
}
771+
766772
variable "datadog_api_key" {
767-
type = string
768-
default = "not_set"
769773
description = "The API key for Datadog"
774+
type = string
775+
default = ""
770776
sensitive = true
777+
778+
validation {
779+
condition = var.monitor_lambda_datadog ? length(var.datadog_api_key) > 0 : true
780+
error_message = "Datadog API key must be provided when monitoring Lambda with Datadog"
781+
}
771782
}

0 commit comments

Comments
 (0)