diff --git a/infrastructure/terraform/modules/lambda/README.md b/infrastructure/terraform/modules/lambda/README.md
index d09e623..05b4a2e 100644
--- a/infrastructure/terraform/modules/lambda/README.md
+++ b/infrastructure/terraform/modules/lambda/README.md
@@ -18,6 +18,7 @@
| [description](#input\_description) | Description of the Lambda | `string` | n/a | yes |
| [enable\_dlq\_and\_notifications](#input\_enable\_dlq\_and\_notifications) | Create an SQS Queue and on-failure destination to be used as the Lambda's Dead Letter Queue and notifications | `bool` | `false` | no |
| [enable\_lambda\_insights](#input\_enable\_lambda\_insights) | Enable the lambda insights layer, this must be disabled for lambda@edge usage | `bool` | `true` | no |
+| [enable\_xray\_tracing](#input\_enable\_xray\_tracing) | Enable AWS X-Ray active tracing for the Lambda function. | `bool` | `false` | no |
| [environment](#input\_environment) | The name of the tfscaffold environment | `string` | n/a | yes |
| [filter\_pattern](#input\_filter\_pattern) | Filter pattern to use for the log subscription filter | `string` | `""` | no |
| [force\_lambda\_code\_deploy](#input\_force\_lambda\_code\_deploy) | If the lambda package in s3 has the same commit id tag as the terraform build branch, the lambda will not update automatically. Set to True if making changes to Lambda code from on the same commit for example during development | `bool` | `false` | no |
diff --git a/infrastructure/terraform/modules/lambda/iam_role_policy_attachment_xray.tf b/infrastructure/terraform/modules/lambda/iam_role_policy_attachment_xray.tf
new file mode 100644
index 0000000..964edd6
--- /dev/null
+++ b/infrastructure/terraform/modules/lambda/iam_role_policy_attachment_xray.tf
@@ -0,0 +1,6 @@
+resource "aws_iam_role_policy_attachment" "xray" {
+ count = var.enable_xray_tracing ? 1 : 0
+
+ role = aws_iam_role.main.name
+ policy_arn = "arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess"
+}
diff --git a/infrastructure/terraform/modules/lambda/lambda_function.tf b/infrastructure/terraform/modules/lambda/lambda_function.tf
index f67849a..7b5662e 100644
--- a/infrastructure/terraform/modules/lambda/lambda_function.tf
+++ b/infrastructure/terraform/modules/lambda/lambda_function.tf
@@ -49,6 +49,13 @@ resource "aws_lambda_function" "main" {
}
}
+ dynamic "tracing_config" {
+ for_each = var.enable_xray_tracing ? [1] : []
+ content {
+ mode = "Active"
+ }
+ }
+
dynamic "vpc_config" {
for_each = var.vpc_config != null ? [""] : []
diff --git a/infrastructure/terraform/modules/lambda/variables.tf b/infrastructure/terraform/modules/lambda/variables.tf
index ed335f9..fe079e2 100644
--- a/infrastructure/terraform/modules/lambda/variables.tf
+++ b/infrastructure/terraform/modules/lambda/variables.tf
@@ -268,6 +268,12 @@ variable "enable_lambda_insights" {
default = true
}
+variable "enable_xray_tracing" {
+ type = bool
+ description = "Enable AWS X-Ray active tracing for the Lambda function."
+ default = false
+}
+
variable "lambda_at_edge" {
type = bool
description = "Whether this Lambda is a Lambda@Edge function"