Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit 4bb80be

Browse files
jsalcedo1987npalm
andauthored
fix(binary-syncer): Allow lambda inside VPC (#2938)
* fix: add VPCAccessExecutionRole for lambda running in VPC * fix: add aws_partition to variables.tf file * fix: update main.tf file aws_partition variable * fix: add subnets to lambda webhook * fix: adding subnets id and security groups ids to variables file * fix: update lamba to add vpc parameters and vpc policy * fix: adding partition to variables.tf * fix: adding partition input parameter * fix: remove variables * fix: remove aws variable * fix: add vpc parameters to webhook module * doc: update docs input parameters to modules * fix: adding missing variables to webhook module and runner-binaries --------- Co-authored-by: Niek Palm <[email protected]>
1 parent 8f4cc41 commit 4bb80be

File tree

9 files changed

+66
-0
lines changed

9 files changed

+66
-0
lines changed

Diff for: main.tf

+6
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ module "webhook" {
161161
role_permissions_boundary = var.role_permissions_boundary
162162
repository_white_list = var.repository_white_list
163163

164+
lambda_subnet_ids = var.lambda_subnet_ids
165+
lambda_security_group_ids = var.lambda_security_group_ids
166+
aws_partition = var.aws_partition
167+
164168
log_type = var.log_type
165169
log_level = var.log_level
166170
}
@@ -306,6 +310,8 @@ module "runner_binaries" {
306310

307311
lambda_subnet_ids = var.lambda_subnet_ids
308312
lambda_security_group_ids = var.lambda_security_group_ids
313+
aws_partition = var.aws_partition
314+
309315

310316
lambda_principals = var.lambda_principals
311317
}

Diff for: modules/multi-runner/runner-binaries.tf

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ module "runner_binaries" {
2727
log_type = var.log_type
2828
log_level = var.log_level
2929

30+
lambda_subnet_ids = var.lambda_subnet_ids
31+
lambda_security_group_ids = var.lambda_security_group_ids
32+
aws_partition = var.aws_partition
33+
3034
lambda_principals = var.lambda_principals
3135
}
3236
locals {

Diff for: modules/multi-runner/webhook.tf

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ module "webhook" {
2626
role_permissions_boundary = var.role_permissions_boundary
2727
repository_white_list = var.repository_white_list
2828

29+
lambda_subnet_ids = var.lambda_subnet_ids
30+
lambda_security_group_ids = var.lambda_security_group_ids
31+
aws_partition = var.aws_partition
32+
2933
log_type = var.log_type
3034
log_level = var.log_level
3135
}

Diff for: modules/runner-binaries-syncer/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ No modules.
6565
| [aws_iam_role_policy.lambda_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
6666
| [aws_iam_role_policy.lambda_syncer_vpc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
6767
| [aws_iam_role_policy.syncer](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
68+
| [aws_iam_role_policy_attachment.syncer_vpc_execution_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
6869
| [aws_lambda_function.syncer](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource |
6970
| [aws_lambda_permission.on_deploy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource |
7071
| [aws_lambda_permission.syncer](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource |
@@ -85,6 +86,7 @@ No modules.
8586

8687
| Name | Description | Type | Default | Required |
8788
|------|-------------|------|---------|:--------:|
89+
| <a name="input_aws_partition"></a> [aws\_partition](#input\_aws\_partition) | (optional) partition for the base arn if not 'aws' | `string` | `"aws"` | no |
8890
| <a name="input_distribution_bucket_name"></a> [distribution\_bucket\_name](#input\_distribution\_bucket\_name) | Bucket for storing the action runner distribution. | `string` | n/a | yes |
8991
| <a name="input_environment"></a> [environment](#input\_environment) | A name that identifies the environment, used as prefix and for tagging. | `string` | `null` | no |
9092
| <a name="input_lambda_architecture"></a> [lambda\_architecture](#input\_lambda\_architecture) | AWS Lambda architecture. Lambda functions using Graviton processors ('arm64') tend to have better price/performance than 'x86\_64' functions. | `string` | `"arm64"` | no |

Diff for: modules/runner-binaries-syncer/runner-binaries-syncer.tf

+8
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ resource "aws_cloudwatch_event_target" "syncer" {
128128
arn = aws_lambda_function.syncer.arn
129129
}
130130

131+
resource "aws_iam_role_policy_attachment" "syncer_vpc_execution_role" {
132+
count = length(var.lambda_subnet_ids) > 0 ? 1 : 0
133+
role = aws_iam_role.syncer_lambda.name
134+
policy_arn = "arn:${var.aws_partition}:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
135+
}
136+
131137
resource "aws_lambda_permission" "syncer" {
132138
statement_id = "AllowExecutionFromCloudWatch"
133139
action = "lambda:InvokeFunction"
@@ -173,3 +179,5 @@ resource "aws_lambda_permission" "on_deploy" {
173179
source_account = data.aws_caller_identity.current.account_id
174180
source_arn = aws_s3_bucket.action_dist.arn
175181
}
182+
183+

Diff for: modules/runner-binaries-syncer/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ variable "lambda_security_group_ids" {
163163
default = []
164164
}
165165

166+
variable "aws_partition" {
167+
description = "(optional) partition for the base arn if not 'aws'"
168+
type = string
169+
default = "aws"
170+
}
171+
166172
variable "log_type" {
167173
description = "Logging format for lambda logging. Valid values are 'json', 'pretty', 'hidden'. "
168174
type = string

Diff for: modules/webhook/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ No modules.
6767
| [aws_iam_role_policy.webhook_sqs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
6868
| [aws_iam_role_policy.webhook_ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
6969
| [aws_iam_role_policy.webhook_workflow_job_sqs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
70+
| [aws_iam_role_policy_attachment.webhook_vpc_execution_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
7071
| [aws_lambda_function.webhook](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource |
7172
| [aws_lambda_permission.webhook](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource |
7273
| [aws_iam_policy_document.lambda_assume_role_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
@@ -75,12 +76,15 @@ No modules.
7576

7677
| Name | Description | Type | Default | Required |
7778
|------|-------------|------|---------|:--------:|
79+
| <a name="input_aws_partition"></a> [aws\_partition](#input\_aws\_partition) | (optional) partition for the base arn if not 'aws' | `string` | `"aws"` | no |
7880
| <a name="input_environment"></a> [environment](#input\_environment) | A name that identifies the environment, used as prefix and for tagging. | `string` | `null` | no |
7981
| <a name="input_github_app_parameters"></a> [github\_app\_parameters](#input\_github\_app\_parameters) | Parameter Store for GitHub App Parameters. | <pre>object({<br> webhook_secret = map(string)<br> })</pre> | n/a | yes |
8082
| <a name="input_kms_key_arn"></a> [kms\_key\_arn](#input\_kms\_key\_arn) | Optional CMK Key ARN to be used for Parameter Store. | `string` | `null` | no |
8183
| <a name="input_lambda_architecture"></a> [lambda\_architecture](#input\_lambda\_architecture) | AWS Lambda architecture. Lambda functions using Graviton processors ('arm64') tend to have better price/performance than 'x86\_64' functions. | `string` | `"arm64"` | no |
8284
| <a name="input_lambda_runtime"></a> [lambda\_runtime](#input\_lambda\_runtime) | AWS Lambda runtime. | `string` | `"nodejs18.x"` | no |
8385
| <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 |
86+
| <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 |
87+
| <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 |
8488
| <a name="input_lambda_timeout"></a> [lambda\_timeout](#input\_lambda\_timeout) | Time out of the lambda in seconds. | `number` | `10` | no |
8589
| <a name="input_lambda_zip"></a> [lambda\_zip](#input\_lambda\_zip) | File location of the lambda zip file. | `string` | `null` | no |
8690
| <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 |

Diff for: modules/webhook/variables.tf

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
variable "lambda_subnet_ids" {
2+
description = "List of subnets in which the action runners will be launched, the subnets needs to be subnets in the `vpc_id`."
3+
type = list(string)
4+
default = []
5+
}
6+
7+
variable "lambda_security_group_ids" {
8+
description = "List of security group IDs associated with the Lambda function."
9+
type = list(string)
10+
default = []
11+
}
12+
113
variable "environment" {
214
description = "A name that identifies the environment, used as prefix and for tagging."
315
type = string
@@ -154,6 +166,12 @@ variable "lambda_runtime" {
154166
default = "nodejs18.x"
155167
}
156168

169+
variable "aws_partition" {
170+
description = "(optional) partition for the base arn if not 'aws'"
171+
type = string
172+
default = "aws"
173+
}
174+
157175
variable "lambda_architecture" {
158176
description = "AWS Lambda architecture. Lambda functions using Graviton processors ('arm64') tend to have better price/performance than 'x86_64' functions. "
159177
type = string

Diff for: modules/webhook/webhook.tf

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ resource "aws_lambda_function" "webhook" {
2323
}
2424
}
2525

26+
dynamic "vpc_config" {
27+
for_each = var.lambda_subnet_ids != null && var.lambda_security_group_ids != null ? [true] : []
28+
content {
29+
security_group_ids = var.lambda_security_group_ids
30+
subnet_ids = var.lambda_subnet_ids
31+
}
32+
}
33+
2634
tags = var.tags
2735
}
2836

@@ -68,6 +76,12 @@ resource "aws_iam_role_policy" "webhook_logging" {
6876
})
6977
}
7078

79+
resource "aws_iam_role_policy_attachment" "webhook_vpc_execution_role" {
80+
count = length(var.lambda_subnet_ids) > 0 ? 1 : 0
81+
role = aws_iam_role.webhook_lambda.name
82+
policy_arn = "arn:${var.aws_partition}:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
83+
}
84+
7185
resource "aws_iam_role_policy" "webhook_sqs" {
7286
name = "${var.prefix}-lambda-webhook-publish-sqs-policy"
7387
role = aws_iam_role.webhook_lambda.name

0 commit comments

Comments
 (0)