Skip to content

Commit 0e052e9

Browse files
lambda support vpc (#20)
* lambda support vpc * fmt * add doc * fix rejects * fix rejects * update CI spectral scan action version --------- Co-authored-by: Gavriel Hagag <[email protected]>
1 parent 08d1342 commit 0e052e9

File tree

10 files changed

+81
-1
lines changed

10 files changed

+81
-1
lines changed

.github/workflows/scan.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
steps:
1414
- uses: actions/checkout@v2
1515
- name: Install and run Spectral CI
16-
uses: spectralops/spectral-github-action@v3
16+
uses: spectralops/spectral-github-action@v4
1717
with:
1818
spectral-dsn: ${{ env.SPECTRAL_DSN }}
1919
spectral-args: scan --unpack --ok --engines secrets,iac

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Change Log
22

33
All notable changes to this project will be documented in this file.
4+
5+
## [4.2.0] - 2024-12-01
6+
### Added
7+
- Added VPC support to Lambda function
8+
49
## [4.1.0] - 2024-11-13
510
### Added
611
- Support for self hosted github

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Terraform configuration used to create the required AWS resources for integratin
5252
| <a name="input_secrets_names"></a> [secrets\_names](#input\_secrets\_names) | Names of secrets to create | `list(string)` | `null` | no |
5353
| <a name="input_store_secret_in_secrets_manager"></a> [store\_secret\_in\_secrets\_manager](#input\_store\_secret\_in\_secrets\_manager) | Whether to store your secrets in secrets manager, default is false | `bool` | `false` | no |
5454
| <a name="input_tags"></a> [tags](#input\_tags) | A collection of tags grouped by key representing it's target resource. | `map(map(string))` | <pre>{<br> "api_gateway": {},<br> "iam": {},<br> "lambda": {}<br>}</pre> | no |
55+
| <a name="input_vpc_config"></a> [vpc\_config](#input\_vpc\_config) | Configuration block for VPC settings for the Lambda function, including subnet IDs and security group IDs. | <pre>object({<br> subnet_ids = list(string)<br> security_group_ids = list(string)<br>})</pre> | `null` | no |
5556

5657
### env_vars
5758

examples/vpc-in-lambda.tf

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module "spectral_lambda_integration" {
2+
source = "github.com/SpectralOps/spectral-terraform-lambda-integration"
3+
4+
integration_type = "gitlab"
5+
6+
env_vars = {
7+
# Required environment variables for GitLab integration
8+
}
9+
10+
# With VPC configuration
11+
vpc_config = {
12+
subnet_ids = ["subnet-12345678", "subnet-87654321"]
13+
security_group_ids = ["sg-12345678"]
14+
}
15+
}

modules/lambda/lambda.tf

+26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
data "aws_partition" "current" {}
2+
13
locals {
24
runtime = "nodejs20.x"
35
lambda_source_code_zip_path = coalesce(var.lambda_source_code_path, "${path.module}/source_code/${var.integration_type}/${var.lambda_source_code_filename}")
@@ -21,6 +23,18 @@ resource "aws_lambda_function" "spectral_scanner_lambda" {
2123
environment {
2224
variables = var.env_vars
2325
}
26+
27+
dynamic "vpc_config" {
28+
for_each = var.vpc_config != null ? [var.vpc_config] : []
29+
content {
30+
subnet_ids = vpc_config.value.subnet_ids
31+
security_group_ids = vpc_config.value.security_group_ids
32+
}
33+
}
34+
35+
depends_on = [
36+
aws_iam_role_policy.lambda_vpc_policy,
37+
]
2438
}
2539

2640
resource "aws_cloudwatch_log_group" "lambda_log_group" {
@@ -32,4 +46,16 @@ resource "aws_cloudwatch_log_group" "lambda_log_group" {
3246
var.global_tags,
3347
lookup(var.tags, "lambda", {}),
3448
)
49+
}
50+
51+
data "aws_iam_policy" "lambda_vpc_policy" {
52+
count = var.vpc_config != null ? 1 : 0
53+
arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSLambdaENIManagementAccess"
54+
}
55+
56+
resource "aws_iam_role_policy" "lambda_vpc_policy" {
57+
count = var.vpc_config != null ? 1 : 0
58+
name = "lambda-vpc-policy"
59+
role = var.lambda_role_id
60+
policy = data.aws_iam_policy.lambda_vpc_policy[0].policy
3561
}

modules/lambda/variables.tf

+14
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,22 @@ variable "role_arn" {
8686
description = "The lambda source code filename"
8787
}
8888

89+
variable "lambda_role_id" {
90+
type = string
91+
description = "The lambda role id"
92+
}
93+
8994
variable "lambda_handler" {
9095
type = string
9196
description = "The handler of the handler"
9297
default = "handler.app"
98+
}
99+
100+
variable "vpc_config" {
101+
type = object({
102+
subnet_ids = list(string)
103+
security_group_ids = list(string)
104+
})
105+
description = "The VPC configuration for the lambda"
106+
default = null
93107
}

modules/role/outputs.tf

+4
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ output "lambda_role_name" {
44

55
output "lambda_role_arn" {
66
value = aws_iam_role.lambda_execution_role.arn
7+
}
8+
9+
output "lambda_role_id" {
10+
value = aws_iam_role.lambda_execution_role.id
711
}

multiple-lambdas-integration.tf

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module "frontend_lambda_function" {
1818
lambda_source_code_filename = "frontend.zip"
1919
lambda_source_code_path = var.frontend_lambda_source_code_path
2020
role_arn = module.lambda_role.lambda_role_arn
21+
vpc_config = var.vpc_config
22+
lambda_role_id = module.lambda_role.lambda_role_id
2123
}
2224

2325
module "backend_lambda_function" {
@@ -40,6 +42,8 @@ module "backend_lambda_function" {
4042
lambda_source_code_filename = "backend.zip"
4143
lambda_source_code_path = var.backend_lambda_source_code_path
4244
role_arn = module.lambda_role.lambda_role_arn
45+
vpc_config = var.vpc_config
46+
lambda_role_id = module.lambda_role.lambda_role_id
4347
}
4448

4549
data "aws_iam_policy_document" "lambda_invoke_policy_document" {

single-lambda-integration.tf

+2
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ module "lambda_function" {
1616
lambda_source_code_filename = "app.zip"
1717
lambda_source_code_path = var.lambda_source_code_path
1818
role_arn = module.lambda_role.lambda_role_arn
19+
vpc_config = var.vpc_config
20+
lambda_role_id = module.lambda_role.lambda_role_id
1921
}

variables.tf

+9
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,13 @@ variable "gateway_api_integration_timeout_milliseconds" {
116116
description = "Timeout for the API Gateway to wait for lambda response"
117117
type = number
118118
default = 29000
119+
}
120+
121+
variable "vpc_config" {
122+
description = "VPC configuration for the Lambda function"
123+
type = object({
124+
subnet_ids = list(string)
125+
security_group_ids = list(string)
126+
})
127+
default = null
119128
}

0 commit comments

Comments
 (0)