For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/terraform-aws-lambda-functions-without-x-ray-tracing.md.
A documentation index is available at /llms.txt.
AWS Lambda functions should have the TracingConfig property set with mode = "Active" to enable active tracing, which provides detailed request and performance monitoring through AWS X-Ray. When tracing_config is either omitted or configured as mode = "PassThrough", as shown below, tracing data isn’t automatically captured for Lambda invocations:
tracing_config {
mode = "PassThrough"
}
Without active tracing, teams lose critical visibility into Lambda execution, making it harder to detect and troubleshoot performance issues or security incidents in serverless environments.
Compliant Code Examples
resource"aws_iam_role""iam_for_lambda"{name="iam_for_lambda"assume_role_policy=<<EOF{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF}resource"aws_lambda_function""test_lambda"{filename="lambda_function_payload.zip"function_name="lambda_function_name"role=aws_iam_role.iam_for_lambda.arnhandler="exports.test"tracing_config{mode="Active"} # The filebase64sha256() function is available in Terraform 0.11.12 and later
# For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
# source_code_hash = "${base64sha256(file("lambda_function_payload.zip"))}"
source_code_hash=filebase64sha256("lambda_function_payload.zip")runtime="nodejs12.x"environment{variables={foo="bar"}}}
Non-Compliant Code Examples
resource"aws_iam_role""iam_for_lambda2"{name="iam_for_lambda"assume_role_policy=<<EOF{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF}resource"aws_lambda_function""test_lambda2"{filename="lambda_function_payload.zip"function_name="lambda_function_name"role=aws_iam_role.iam_for_lambda.arnhandler="exports.test"tracing_config{mode="PassThrough"} # The filebase64sha256() function is available in Terraform 0.11.12 and later
# For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
# source_code_hash = "${base64sha256(file("lambda_function_payload.zip"))}"
source_code_hash=filebase64sha256("lambda_function_payload.zip")runtime="nodejs12.x"environment{variables={foo="bar"}}}resource"aws_lambda_function""test_lambda3"{filename="lambda_function_payload.zip"function_name="lambda_function_name"role=aws_iam_role.iam_for_lambda.arnhandler="exports.test" # The filebase64sha256() function is available in Terraform 0.11.12 and later
# For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
# source_code_hash = "${base64sha256(file("lambda_function_payload.zip"))}"
source_code_hash=filebase64sha256("lambda_function_payload.zip")runtime="nodejs12.x"environment{variables={foo="bar"}}}
1
2
rulesets:- Terraform / AWS # Rules to enforce / AWS.
Request a personalized demo
Get Started with Datadog
Ask AI
AI-generated responses may be inaccurate. Verify important info.