Skip to content

Commit 3be71a8

Browse files
authored
Merge pull request #11 from nikola-acuris/master
Unify Zip and Image lambda deployments
2 parents 94f5dd4 + 6795a76 commit 3be71a8

File tree

5 files changed

+109
-64
lines changed

5 files changed

+109
-64
lines changed

README.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,39 @@
22

33
[![Test](https://github.com/mergermarket/terraform-acuris-aws-lambda/actions/workflows/test.yml/badge.svg)](https://github.com/mergermarket/terraform-acuris-aws-lambda/actions/workflows/test.yml)
44

5-
This module will deploy a Lambda function.
5+
This module will deploy a Lambda function. It supports both Zip and Image deployments.
66

7-
## Module Input Variables
7+
> NOTE 1: if image_uri is set then ECR Image will be deployed regardless of what Zip deployment properties are set to.
8+
9+
> NOTE 2: if both security_group_ids and subnet_ids are empty then the Lambda will not have access to resources within a VPC.
10+
11+
## Module input variables (shared)
812

913
- `function_name` - (string) - **REQUIRED** - The name of the Lambda function.
10-
- `handler` - (map) - **REQUIRED** - The function within your code that Lambda calls to begin execution.
11-
- `lambda_env` - (map) - Environment parameters passed to the Lambda function
14+
- `lambda_env` - (map) - Environment parameters passed to the Lambda function.
1215
- `lambda_role_policy` (string) - The Lambda IAM Role Policy.
13-
- `log_subscription_filter` - (string) - Subscription filter to filter logs sent to datadog
14-
- `memory_size` (number) - Amount of memory in MB your Lambda Function can use at runtime
15-
- `runtime` - (string) - **REQUIRED** The runtime environment for the Lambda function you are uploading.
16-
- `s3_bucket` - (string) - **REQUIRED** - The name of the bucket containing your uploaded Lambda deployment package.
17-
- `s3_key` - (string) - **REQUIRED** - The s3 key for your Lambda deployment package.
16+
- `log_subscription_filter` - (string) - Subscription filter to filter logs sent to datadog.
17+
- `memory_size` (number) - Amount of memory in MB your Lambda Function can use at runtime.
1818
- `security_group_ids` - (list) - The VPC security groups assigned to the Lambda.
1919
- `subnet_ids` - (list) - The VPC subnets in which the Lambda runs.
20-
- `timeout` (number) - The maximum time in seconds that the Lambda can run for
20+
- `timeout` (number) - The maximum time in seconds that the Lambda can run for.
2121
- `reserved_concurrent_executions` (number) - The amount of reserved concurrent executions for this lambda function.
2222
- `tags` (map) - A mapping of tags to assign to this lambda function.
23-
- `datadog_log_subscription_arn` - (string) - Log subscription arn for shipping logs to datadog
23+
- `datadog_log_subscription_arn` - (string) - Log subscription arn for shipping logs to datadog.
24+
25+
### Zip deployment variables
26+
- `runtime` - (string) - **REQUIRED** - The runtime environment for the Lambda function you are uploading.
27+
- `handler` - (map) - **REQUIRED** - The function within your code that Lambda calls to begin execution.
28+
- `s3_bucket` - (string) - **REQUIRED** - The name of the bucket containing your uploaded Lambda deployment package.
29+
- `s3_key` - (string) - **REQUIRED** - The s3 key for your Lambda deployment package.
30+
- `layers` - (list) - ARNs of the layers to attach to the lambda function in order.
31+
32+
### Image deployment variables
33+
- `image_uri` - (string) - **REQUIRED** - Uri to the image in ECR repo.
34+
- `image_config_command` - (list) - List of values with which to override CMD entry in the image.
35+
- `image_config_entry_point` - (list) - List of values with which to override ENTRYPOINT entry in the image.
36+
- `image_config_working_directory` - (string) - Value with which to override WORKDIR entry in the image.
2437

25-
> NOTE: if both security_group_ids and subnet_ids are empty then the Lambda will not have access to resources within a VPC.
2638

2739
## Usage
2840

main.tf

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,28 @@ terraform {
33
}
44

55
resource "aws_lambda_function" "lambda_function" {
6-
image_uri = var.image_uri != "" ? var.image_uri : null
7-
s3_bucket = var.s3_bucket != "" ? var.s3_bucket : null
8-
s3_key = var.s3_key != "" ? var.s3_key : null
9-
function_name = var.function_name
10-
role = aws_iam_role.iam_for_lambda.arn
11-
handler = var.handler
12-
runtime = var.runtime
13-
timeout = var.timeout
14-
memory_size = var.memory_size
15-
reserved_concurrent_executions = var.reserved_concurrent_executions
16-
tags = var.tags
17-
package_type = var.image_uri != "" ? "Image" : "Zip"
18-
layers = var.layers
6+
image_uri = var.image_uri != "" ? var.image_uri : null
7+
s3_bucket = var.s3_bucket
8+
s3_key = var.s3_key
9+
function_name = var.function_name
10+
role = aws_iam_role.iam_for_lambda.arn
11+
handler = var.handler
12+
runtime = var.runtime
13+
timeout = var.timeout
14+
memory_size = var.memory_size
15+
reserved_concurrent_executions = var.reserved_concurrent_executions
16+
tags = var.tags
17+
package_type = var.image_uri != "" ? "Image" : "Zip"
18+
layers = var.layers
19+
20+
dynamic "image_config" {
21+
for_each = var.image_uri != "" ? [1] : []
22+
content {
23+
command = var.image_config_command
24+
entry_point = var.image_config_entry_point
25+
working_directory = var.image_config_working_directory
26+
}
27+
}
1928

2029
vpc_config {
2130
subnet_ids = var.subnet_ids

test/files/create_lambda_container.json

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
{
2-
"resource_changes": [
3-
{
2+
"resource_changes": [{
43
"address": "module.lambda.aws_cloudwatch_log_group.lambda_loggroup",
54
"module_address": "module.lambda",
65
"mode": "managed",
76
"type": "aws_cloudwatch_log_group",
87
"name": "lambda_loggroup",
98
"provider_name": "aws",
109
"change": {
11-
"actions": [
12-
"create"
13-
],
10+
"actions": ["create"],
1411
"before": null,
1512
"after": {
1613
"kms_key_id": null,
@@ -24,18 +21,15 @@
2421
"id": true
2522
}
2623
}
27-
},
28-
{
24+
}, {
2925
"address": "module.lambda.aws_iam_role.iam_for_lambda",
3026
"module_address": "module.lambda",
3127
"mode": "managed",
3228
"type": "aws_iam_role",
3329
"name": "iam_for_lambda",
3430
"provider_name": "aws",
3531
"change": {
36-
"actions": [
37-
"create"
38-
],
32+
"actions": ["create"],
3933
"before": null,
4034
"after": {
4135
"assume_role_policy": "{\n \"Version\": \"2012-10-17\",\n \"Statement\": [\n {\n \"Action\": \"sts:AssumeRole\",\n \"Principal\": {\n \"Service\": \"lambda.amazonaws.com\"\n },\n \"Effect\": \"Allow\"\n }\n ]\n}\n",
@@ -57,18 +51,15 @@
5751
"unique_id": true
5852
}
5953
}
60-
},
61-
{
54+
}, {
6255
"address": "module.lambda.aws_iam_role_policy.lambda_policy",
6356
"module_address": "module.lambda",
6457
"mode": "managed",
6558
"type": "aws_iam_role_policy",
6659
"name": "lambda_policy",
6760
"provider_name": "aws",
6861
"change": {
69-
"actions": [
70-
"create"
71-
],
62+
"actions": ["create"],
7263
"before": null,
7364
"after": {
7465
"name": "policy",
@@ -80,49 +71,49 @@
8071
"role": true
8172
}
8273
}
83-
},
84-
{
74+
}, {
8575
"address": "module.lambda.aws_lambda_function.lambda_function",
8676
"module_address": "module.lambda",
8777
"mode": "managed",
8878
"type": "aws_lambda_function",
8979
"name": "lambda_function",
9080
"provider_name": "aws",
9181
"change": {
92-
"actions": [
93-
"create"
94-
],
82+
"actions": ["create"],
9583
"before": null,
9684
"after": {
9785
"code_signing_config_arn": null,
9886
"dead_letter_config": [],
9987
"description": null,
100-
"environment": [
101-
{
88+
"environment": [{
10289
"variables": null
10390
}
10491
],
10592
"file_system_config": [],
10693
"filename": null,
10794
"function_name": "check_lambda_function",
108-
"handler": "unused",
109-
"image_config": [],
95+
"handler": null,
96+
"image_config": [{
97+
"command": ["some_cmd"],
98+
"entry_point": ["some_entrypoint"],
99+
"working_directory": null
100+
}
101+
],
110102
"image_uri": "image",
111103
"kms_key_arn": null,
112-
"layers": [],
104+
"layers": null,
113105
"memory_size": 128,
114106
"package_type": "Image",
115107
"publish": false,
116108
"reserved_concurrent_executions": -1,
117-
"runtime": "provided",
109+
"runtime": null,
118110
"s3_bucket": null,
119111
"s3_key": null,
120112
"s3_object_version": null,
121113
"tags": null,
122114
"timeout": 3,
123115
"timeouts": null,
124-
"vpc_config": [
125-
{
116+
"vpc_config": [{
126117
"security_group_ids": null,
127118
"subnet_ids": null
128119
}
@@ -131,15 +122,17 @@
131122
"after_unknown": {
132123
"arn": true,
133124
"dead_letter_config": [],
134-
"environment": [
135-
{}
125+
"environment": [{}
136126
],
137127
"file_system_config": [],
138128
"id": true,
139-
"image_config": [],
129+
"image_config": [{
130+
"command": [false],
131+
"entry_point": [false]
132+
}
133+
],
140134
"invoke_arn": true,
141135
"last_modified": true,
142-
"layers": [],
143136
"qualified_arn": true,
144137
"role": true,
145138
"signing_job_arn": true,
@@ -148,8 +141,7 @@
148141
"source_code_size": true,
149142
"tracing_config": true,
150143
"version": true,
151-
"vpc_config": [
152-
{
144+
"vpc_config": [{
153145
"vpc_id": true
154146
}
155147
]

test/infra_container/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ provider "aws" {
1818
module "lambda" {
1919
source = "../.."
2020
image_uri = "image"
21+
image_config_command = ["some_cmd"]
22+
image_config_entry_point = ["some_entrypoint"]
2123
function_name = "check_lambda_function"
22-
handler = "unused"
23-
runtime = "provided"
2424
}
2525

2626
output "lambda_function_arn" {

variables.tf

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,54 @@
11
variable "image_uri" {
2+
type = string
23
description = "Optional ECR image (for image based lambda)"
34
default = ""
45
}
56

7+
variable "image_config_command" {
8+
type = list(string)
9+
description = "Optional override of image's CMD"
10+
default = null
11+
}
12+
13+
variable "image_config_entry_point" {
14+
type = list(string)
15+
description = "Optional override of image's ENTRYPOINT"
16+
default = null
17+
}
18+
19+
variable "image_config_working_directory" {
20+
type = string
21+
description = "Optional override of image's WORKDIR"
22+
default = null
23+
}
24+
625
variable "s3_bucket" {
26+
type = string
727
description = "The name of the bucket containing your uploaded Lambda deployment package."
8-
default = ""
28+
default = null
929
}
1030

1131
variable "s3_key" {
32+
type = string
1233
description = "The s3 key for your Lambda deployment package."
13-
default = ""
34+
default = null
1435
}
1536

1637
variable "function_name" {
38+
type = string
1739
description = "The name of the Lambda function."
1840
}
1941

2042
variable "handler" {
43+
type = string
2144
description = "The function within your code that Lambda calls to begin execution."
45+
default = null
2246
}
2347

2448
variable "runtime" {
49+
type = string
2550
description = "The runtime environment for the Lambda function you are uploading."
51+
default = null
2652
}
2753

2854
variable "subnet_ids" {
@@ -38,11 +64,13 @@ variable "security_group_ids" {
3864
}
3965

4066
variable "datadog_log_subscription_arn" {
67+
type = string
4168
description = "Log subscription arn for shipping logs to datadog"
4269
default = ""
4370
}
4471

4572
variable "lambda_role_policy" {
73+
type = string
4674
description = "The Lambda IAM Role Policy."
4775
default = <<END
4876
{
@@ -63,11 +91,13 @@ END
6391
}
6492

6593
variable "timeout" {
94+
type = number
6695
description = "The maximum time in seconds that the Lambda can run for."
6796
default = 3
6897
}
6998

7099
variable "memory_size" {
100+
type = number
71101
description = "Amount of memory in MB your Lambda Function can use at runtime."
72102
default = 128
73103
}
@@ -79,11 +109,13 @@ variable "lambda_env" {
79109
}
80110

81111
variable "log_subscription_filter" {
112+
type = string
82113
description = "Subscription filter to filter logs sent to datadog"
83114
default = ""
84115
}
85116

86117
variable "reserved_concurrent_executions" {
118+
type = number
87119
description = "Reserved concurrent executions for this Lambda"
88120
default = -1
89121
}
@@ -97,5 +129,5 @@ variable "tags" {
97129
variable "layers" {
98130
type = list(string)
99131
description = "ARNs of the layers to attach to the lambda function in order"
100-
default = []
132+
default = null
101133
}

0 commit comments

Comments
 (0)