Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda): Add support for X-Ray tracing #3142

Merged
merged 15 commits into from
Apr 20, 2023
Merged
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ We welcome any improvement to the standard module to make the default as secure
| <a name="input_lambda_s3_bucket"></a> [lambda\_s3\_bucket](#input\_lambda\_s3\_bucket) | S3 bucket from which to specify lambda functions. This is an alternative to providing local files directly. | `string` | `null` | no |
| <a name="input_lambda_security_group_ids"></a> [lambda\_security\_group\_ids](#input\_lambda\_security\_group\_ids) | List of security group IDs associated with the Lambda function. | `list(string)` | `[]` | no |
| <a name="input_lambda_subnet_ids"></a> [lambda\_subnet\_ids](#input\_lambda\_subnet\_ids) | List of subnets in which the action runners will be launched, the subnets needs to be subnets in the `vpc_id`. | `list(string)` | `[]` | no |
| <a name="input_lambda_tracing_mode"></a> [lambda\_tracing\_mode](#input\_lambda\_tracing\_mode) | Enable X-Ray tracing for the lambda functions. | `string` | `null` | no |
| <a name="input_log_level"></a> [log\_level](#input\_log\_level) | Logging level for lambda logging. Valid values are 'silly', 'trace', 'debug', 'info', 'warn', 'error', 'fatal'. | `string` | `"info"` | no |
| <a name="input_log_type"></a> [log\_type](#input\_log\_type) | Logging format for lambda logging. Valid values are 'json', 'pretty', 'hidden'. | `string` | `null` | no |
| <a name="input_logging_kms_key_id"></a> [logging\_kms\_key\_id](#input\_logging\_kms\_key\_id) | Specifies the kms key id to encrypt the logs with | `string` | `null` | no |
Expand Down
3 changes: 3 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ module "webhook" {
lambda_architecture = var.lambda_architecture
lambda_zip = var.webhook_lambda_zip
lambda_timeout = var.webhook_lambda_timeout
lambda_tracing_mode = var.lambda_tracing_mode
logging_retention_in_days = var.logging_retention_in_days
logging_kms_key_id = var.logging_kms_key_id

Expand Down Expand Up @@ -235,6 +236,7 @@ module "runners" {
lambda_timeout_scale_down = var.runners_scale_down_lambda_timeout
lambda_subnet_ids = var.lambda_subnet_ids
lambda_security_group_ids = var.lambda_security_group_ids
lambda_tracing_mode = var.lambda_tracing_mode
logging_retention_in_days = var.logging_retention_in_days
logging_kms_key_id = var.logging_kms_key_id
enable_cloudwatch_agent = var.enable_cloudwatch_agent
Expand Down Expand Up @@ -297,6 +299,7 @@ module "runner_binaries" {
lambda_architecture = var.lambda_architecture
lambda_zip = var.runner_binaries_syncer_lambda_zip
lambda_timeout = var.runner_binaries_syncer_lambda_timeout
lambda_tracing_mode = var.lambda_tracing_mode
logging_retention_in_days = var.logging_retention_in_days
logging_kms_key_id = var.logging_kms_key_id

Expand Down
3 changes: 3 additions & 0 deletions modules/runner-binaries-syncer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ No modules.
| [aws_iam_role_policy.lambda_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.lambda_syncer_vpc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.syncer](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.syncer_lambda_xray](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy_attachment.syncer_vpc_execution_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_lambda_function.syncer](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource |
| [aws_lambda_permission.on_deploy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource |
Expand All @@ -81,6 +82,7 @@ No modules.
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_iam_policy_document.action_dist_sse_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.lambda_assume_role_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.lambda_xray](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |

## Inputs

Expand All @@ -98,6 +100,7 @@ No modules.
| <a name="input_lambda_security_group_ids"></a> [lambda\_security\_group\_ids](#input\_lambda\_security\_group\_ids) | List of security group IDs associated with the Lambda function. | `list(string)` | `[]` | no |
| <a name="input_lambda_subnet_ids"></a> [lambda\_subnet\_ids](#input\_lambda\_subnet\_ids) | List of subnets in which the action runners will be launched, the subnets needs to be subnets in the `vpc_id`. | `list(string)` | `[]` | no |
| <a name="input_lambda_timeout"></a> [lambda\_timeout](#input\_lambda\_timeout) | Time out of the lambda in seconds. | `number` | `300` | no |
| <a name="input_lambda_tracing_mode"></a> [lambda\_tracing\_mode](#input\_lambda\_tracing\_mode) | Enable X-Ray tracing for the lambda functions. | `string` | `null` | no |
| <a name="input_lambda_zip"></a> [lambda\_zip](#input\_lambda\_zip) | File location of the lambda zip file. | `string` | `null` | no |
| <a name="input_log_level"></a> [log\_level](#input\_log\_level) | Logging level for lambda logging. Valid values are 'silly', 'trace', 'debug', 'info', 'warn', 'error', 'fatal'. | `string` | `"info"` | no |
| <a name="input_log_type"></a> [log\_type](#input\_log\_type) | Logging format for lambda logging. Valid values are 'json', 'pretty', 'hidden'. | `string` | `null` | no |
Expand Down
16 changes: 16 additions & 0 deletions modules/runner-binaries-syncer/iam.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
data "aws_iam_policy_document" "lambda_xray" {
count = var.lambda_tracing_mode != null ? 1 : 0
statement {
actions = [
"xray:BatchGetTraces",
"xray:GetTraceSummaries",
"xray:PutTelemetryRecords",
"xray:PutTraceSegments"
]
effect = "Allow"
resources = [
"*"
]
sid = "AllowXRay"
}
}
13 changes: 12 additions & 1 deletion modules/runner-binaries-syncer/runner-binaries-syncer.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ resource "aws_lambda_function" "syncer" {
}

tags = var.tags

dynamic "tracing_config" {
for_each = var.lambda_tracing_mode != null ? [true] : []
content {
mode = var.lambda_tracing_mode
}
}
}

resource "aws_iam_role_policy" "lambda_kms" {
Expand Down Expand Up @@ -182,4 +189,8 @@ resource "aws_lambda_permission" "on_deploy" {
source_arn = aws_s3_bucket.action_dist.arn
}


resource "aws_iam_role_policy" "syncer_lambda_xray" {
count = var.lambda_tracing_mode != null ? 1 : 0
policy = data.aws_iam_policy_document.lambda_xray[0].json
role = aws_iam_role.syncer_lambda.name
}
6 changes: 6 additions & 0 deletions modules/runner-binaries-syncer/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,9 @@ variable "lambda_architecture" {
error_message = "`lambda_architecture` value is not valid, valid values are: `arm64` and `x86_64`."
}
}

variable "lambda_tracing_mode" {
description = "Enable X-Ray tracing for the lambda functions."
type = string
default = null
}
4 changes: 4 additions & 0 deletions modules/runners/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ yarn run dist
| [aws_iam_role_policy.runner_session_manager_aws_managed](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.scale_down](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.scale_down_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.scale_down_xray](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.scale_up](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.scale_up_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.scale_up_xray](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.service_linked_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.ssm_parameters](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy_attachment.ami_id_ssm_parameter_read](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
Expand All @@ -115,6 +117,7 @@ yarn run dist
| [aws_ami.runner](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_iam_policy_document.lambda_assume_role_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.lambda_xray](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |

## Inputs

Expand Down Expand Up @@ -161,6 +164,7 @@ yarn run dist
| <a name="input_lambda_subnet_ids"></a> [lambda\_subnet\_ids](#input\_lambda\_subnet\_ids) | List of subnets in which the lambda will be launched, the subnets needs to be subnets in the `vpc_id`. | `list(string)` | `[]` | no |
| <a name="input_lambda_timeout_scale_down"></a> [lambda\_timeout\_scale\_down](#input\_lambda\_timeout\_scale\_down) | Time out for the scale down lambda in seconds. | `number` | `60` | no |
| <a name="input_lambda_timeout_scale_up"></a> [lambda\_timeout\_scale\_up](#input\_lambda\_timeout\_scale\_up) | Time out for the scale up lambda in seconds. | `number` | `60` | no |
| <a name="input_lambda_tracing_mode"></a> [lambda\_tracing\_mode](#input\_lambda\_tracing\_mode) | Enable X-Ray tracing for the lambda functions. | `string` | `null` | no |
| <a name="input_lambda_zip"></a> [lambda\_zip](#input\_lambda\_zip) | File location of the lambda zip file. | `string` | `null` | no |
| <a name="input_log_level"></a> [log\_level](#input\_log\_level) | Logging level for lambda logging. Valid values are 'silly', 'trace', 'debug', 'info', 'warn', 'error', 'fatal'. | `string` | `"info"` | no |
| <a name="input_log_type"></a> [log\_type](#input\_log\_type) | Logging format for lambda logging. Valid values are 'json', 'pretty', 'hidden'. | `string` | `null` | no |
Expand Down
17 changes: 17 additions & 0 deletions modules/runners/policies-lambda-common.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,20 @@ resource "aws_iam_policy" "ami_id_ssm_parameter_read" {
}
JSON
}

data "aws_iam_policy_document" "lambda_xray" {
count = var.lambda_tracing_mode != null ? 1 : 0
statement {
actions = [
"xray:BatchGetTraces",
"xray:GetTraceSummaries",
"xray:PutTelemetryRecords",
"xray:PutTraceSegments"
]
effect = "Allow"
resources = [
"*"
]
sid = "AllowXRay"
}
}
13 changes: 13 additions & 0 deletions modules/runners/scale-down.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ resource "aws_lambda_function" "scale_down" {
subnet_ids = var.lambda_subnet_ids
}
}

dynamic "tracing_config" {
for_each = var.lambda_tracing_mode != null ? [true] : []
content {
mode = var.lambda_tracing_mode
}
}
}

resource "aws_cloudwatch_log_group" "scale_down" {
Expand Down Expand Up @@ -110,3 +117,9 @@ resource "aws_iam_role_policy_attachment" "scale_down_vpc_execution_role" {
role = aws_iam_role.scale_down.name
policy_arn = "arn:${var.aws_partition}:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
}

resource "aws_iam_role_policy" "scale_down_xray" {
count = var.lambda_tracing_mode != null ? 1 : 0
policy = data.aws_iam_policy_document.lambda_xray[0].json
role = aws_iam_role.scale_down.name
}
13 changes: 13 additions & 0 deletions modules/runners/scale-up.tf
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ resource "aws_lambda_function" "scale_up" {
subnet_ids = var.lambda_subnet_ids
}
}

dynamic "tracing_config" {
for_each = var.lambda_tracing_mode != null ? [true] : []
content {
mode = var.lambda_tracing_mode
}
}
}

resource "aws_cloudwatch_log_group" "scale_up" {
Expand Down Expand Up @@ -130,3 +137,9 @@ resource "aws_iam_role_policy_attachment" "ami_id_ssm_parameter_read" {
role = aws_iam_role.scale_up.name
policy_arn = aws_iam_policy.ami_id_ssm_parameter_read[0].arn
}

resource "aws_iam_role_policy" "scale_up_xray" {
count = var.lambda_tracing_mode != null ? 1 : 0
policy = data.aws_iam_policy_document.lambda_xray[0].json
role = aws_iam_role.scale_up.name
}
6 changes: 6 additions & 0 deletions modules/runners/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,9 @@ variable "runner_name_prefix" {
error_message = "The prefix used for the GitHub runner name must be less than 32 characters. AWS instances id are 17 chars, https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/resource-ids.html"
}
}

variable "lambda_tracing_mode" {
description = "Enable X-Ray tracing for the lambda functions."
type = string
default = null
}
3 changes: 3 additions & 0 deletions modules/webhook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ No modules.
| [aws_iam_role_policy.webhook_sqs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.webhook_ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.webhook_workflow_job_sqs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.xray](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy_attachment.webhook_vpc_execution_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_lambda_function.webhook](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource |
| [aws_lambda_permission.webhook](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource |
| [aws_iam_policy_document.lambda_assume_role_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.lambda_xray](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |

## Inputs

Expand All @@ -86,6 +88,7 @@ No modules.
| <a name="input_lambda_security_group_ids"></a> [lambda\_security\_group\_ids](#input\_lambda\_security\_group\_ids) | List of security group IDs associated with the Lambda function. | `list(string)` | `[]` | no |
| <a name="input_lambda_subnet_ids"></a> [lambda\_subnet\_ids](#input\_lambda\_subnet\_ids) | List of subnets in which the action runners will be launched, the subnets needs to be subnets in the `vpc_id`. | `list(string)` | `[]` | no |
| <a name="input_lambda_timeout"></a> [lambda\_timeout](#input\_lambda\_timeout) | Time out of the lambda in seconds. | `number` | `10` | no |
| <a name="input_lambda_tracing_mode"></a> [lambda\_tracing\_mode](#input\_lambda\_tracing\_mode) | Enable X-Ray tracing for the lambda functions. | `string` | `null` | no |
| <a name="input_lambda_zip"></a> [lambda\_zip](#input\_lambda\_zip) | File location of the lambda zip file. | `string` | `null` | no |
| <a name="input_log_level"></a> [log\_level](#input\_log\_level) | Logging level for lambda logging. Valid values are 'silly', 'trace', 'debug', 'info', 'warn', 'error', 'fatal'. | `string` | `"info"` | no |
| <a name="input_log_type"></a> [log\_type](#input\_log\_type) | Logging format for lambda logging. Valid values are 'json', 'pretty', 'hidden'. | `string` | `null` | no |
Expand Down
16 changes: 16 additions & 0 deletions modules/webhook/policies.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
data "aws_iam_policy_document" "lambda_xray" {
count = var.lambda_tracing_mode != null ? 1 : 0
statement {
actions = [
"xray:BatchGetTraces",
"xray:GetTraceSummaries",
"xray:PutTelemetryRecords",
"xray:PutTraceSegments"
]
effect = "Allow"
resources = [
"*"
]
sid = "AllowXRay"
}
}
6 changes: 6 additions & 0 deletions modules/webhook/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,9 @@ variable "github_app_parameters" {
webhook_secret = map(string)
})
}

variable "lambda_tracing_mode" {
description = "Enable X-Ray tracing for the lambda functions."
type = string
default = null
}
13 changes: 13 additions & 0 deletions modules/webhook/webhook.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ resource "aws_lambda_function" "webhook" {
}

tags = var.tags

dynamic "tracing_config" {
for_each = var.lambda_tracing_mode != null ? [true] : []
content {
mode = var.lambda_tracing_mode
}
}
}

resource "aws_cloudwatch_log_group" "webhook" {
Expand Down Expand Up @@ -111,3 +118,9 @@ resource "aws_iam_role_policy" "webhook_ssm" {
github_app_webhook_secret_arn = var.github_app_parameters.webhook_secret.arn,
})
}

resource "aws_iam_role_policy" "xray" {
count = var.lambda_tracing_mode != null ? 1 : 0
policy = data.aws_iam_policy_document.lambda_xray[0].json
role = aws_iam_role.webhook_lambda.name
}
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,9 @@ variable "runner_name_prefix" {
error_message = "The prefix used for the GitHub runner name must be less than 32 characters. AWS instances id are 17 chars, https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/resource-ids.html"
}
}

variable "lambda_tracing_mode" {
description = "Enable X-Ray tracing for the lambda functions."
type = string
default = null
}