Skip to content

Instantly share code, notes, and snippets.

@mdlavin
Last active March 18, 2024 14:38
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mdlavin/1b8fcc1b05932f6c105fb7c4ad34c204 to your computer and use it in GitHub Desktop.
Save mdlavin/1b8fcc1b05932f6c105fb7c4ad34c204 to your computer and use it in GitHub Desktop.
Terraform configuration to enable X-Ray for a Lambda function
resource "aws_lambda_function" "service" {
# Your usual aws_lambda_function configuration settings here
tracing_config {
mode = "Active"
}
}
@sburns
Copy link

sburns commented Jul 26, 2019

Note that you might have to add this policy block into the IAM role that this lambda runs with:

        {
            "Effect": "Allow",
            "Action": [
                "xray:PutTraceSegments",
                "xray:PutTelemetryRecords",
                "xray:GetSamplingRules",
                "xray:GetSamplingTargets",
                "xray:GetSamplingStatisticSummaries"
            ],
            "Resource": [
                "*"
            ]
        }

This is coming from this documentation page

@tiny-dancer
Copy link

Also an IAM option for the above

data "aws_iam_policy" "aws_xray_write_only_access" {
  arn = "arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess"
}

@BarakBD-Globality
Copy link

This was super useful, thanks everyone!
Just to add to the party:

data "aws_iam_policy" "aws_xray_write_only_access" {
  arn = "arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess"
}
resource "aws_iam_role_policy_attachment" "aws_xray_write_only_access" {
  role       = "${aws_iam_role.lambda.name}"
  policy_arn = "${data.aws_iam_policy.aws_xray_write_only_access.arn}"
}

@illia-sh
Copy link

or even shorter way

resource "aws_iam_role_policy_attachment" "aws_xray_write_only_access" {
  role       = aws_iam_role.lambda.name
  policy_arn = "arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess"
}

@YuanH
Copy link

YuanH commented Oct 23, 2021

Hi, not sure if anyone's run into this situation where if you remove that tracing_config block, tracing is still turned on?

@j4y
Copy link

j4y commented Sep 8, 2022

This is the newer version of the managed policy:

data "aws_iam_policy" "lambda_xray" {
  name = "AWSXRayDaemonWriteAccess"
}

@jzaoueli
Copy link

jzaoueli commented Mar 18, 2024

with the suggegsted code:

resource "aws_lambda_function" "service" {
  tracing_config {
    mode = "Active"
  }
}

I got only the last Log containing the X-Ray-data but not all Logs, my wish is that all logs of the lambda contains the xray-trace-id did someone have a solution?

@jzaoueli
Copy link

jzaoueli commented Mar 18, 2024

Hi, not sure if anyone's run into this situation where if you remove that tracing_config block, tracing is still turned on?

yes me also. I had to change it to "PassThrough" to turn it of. I'm not sure if this is the right workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment