Skip to content

Commit 0376526

Browse files
committed
feat: support both zip and container image deployment
1 parent 873fb75 commit 0376526

File tree

2 files changed

+95
-8
lines changed

2 files changed

+95
-8
lines changed

main.tf

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,22 @@ resource "local_file" "makefile_template" {
2626
}
2727

2828
# Create zip package from source directory
29+
# data "archive_file" "lambda_zip" {
30+
# type = "zip"
31+
# source_dir = local.source_dir
32+
# output_path = "${path.module}/.terraform/tmp/${module.this.id}.zip"
33+
34+
# depends_on = [
35+
# local_file.bootstrap_template,
36+
# local_file.handler_template,
37+
# local_file.makefile_template
38+
# ]
39+
# }
40+
41+
# Create zip package from source directory (only for Zip package type)
2942
data "archive_file" "lambda_zip" {
43+
count = var.package_type == "Zip" ? 1 : 0
44+
3045
type = "zip"
3146
source_dir = local.source_dir
3247
output_path = "${path.module}/.terraform/tmp/${module.this.id}.zip"
@@ -38,6 +53,7 @@ data "archive_file" "lambda_zip" {
3853
]
3954
}
4055

56+
4157
# IAM role for Lambda execution
4258
resource "aws_iam_role" "lambda_execution" {
4359
name = "${module.this.id}-execution"
@@ -72,19 +88,63 @@ resource "aws_cloudwatch_log_group" "lambda_logs" {
7288
tags = module.this.tags
7389
}
7490

91+
# Lambda function
92+
# resource "aws_lambda_function" "this" {
93+
# function_name = module.this.id
94+
# role = aws_iam_role.lambda_execution.arn
95+
96+
# filename = data.archive_file.lambda_zip.output_path
97+
# source_code_hash = data.archive_file.lambda_zip.output_base64sha256
98+
99+
# handler = var.handler
100+
# runtime = var.runtime
101+
# architectures = [var.architecture]
102+
# memory_size = var.memory_size
103+
# timeout = var.timeout
104+
105+
# dynamic "environment" {
106+
# for_each = length(var.environment_variables) > 0 ? [1] : []
107+
# content {
108+
# variables = var.environment_variables
109+
# }
110+
# }
111+
112+
# depends_on = [
113+
# aws_iam_role_policy_attachment.lambda_basic_execution,
114+
# aws_cloudwatch_log_group.lambda_logs,
115+
# ]
116+
117+
# tags = module.this.tags
118+
# }
119+
75120
# Lambda function
76121
resource "aws_lambda_function" "this" {
77122
function_name = module.this.id
78123
role = aws_iam_role.lambda_execution.arn
79124

80-
filename = data.archive_file.lambda_zip.output_path
81-
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
125+
package_type = var.package_type
126+
127+
# Zip package configuration
128+
filename = var.package_type == "Zip" ? data.archive_file.lambda_zip[0].output_path : null
129+
source_code_hash = var.package_type == "Zip" ? data.archive_file.lambda_zip[0].output_base64sha256 : null
130+
handler = var.package_type == "Zip" ? var.handler : null
131+
runtime = var.package_type == "Zip" ? var.runtime : null
132+
133+
# Container image configuration
134+
image_uri = var.package_type == "Image" ? var.image_uri : null
135+
136+
dynamic "image_config" {
137+
for_each = var.package_type == "Image" && var.image_config != null ? [var.image_config] : []
138+
content {
139+
entry_point = image_config.value.entry_point
140+
command = image_config.value.command
141+
working_directory = image_config.value.working_directory
142+
}
143+
}
82144

83-
handler = var.handler
84-
runtime = var.runtime
85-
architectures = [var.architecture]
86-
memory_size = var.memory_size
87-
timeout = var.timeout
145+
architectures = [var.architecture]
146+
memory_size = var.memory_size
147+
timeout = var.timeout
88148

89149
dynamic "environment" {
90150
for_each = length(var.environment_variables) > 0 ? [1] : []

variables.tf

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,31 @@ variable "template_dir" {
7777
type = string
7878
description = "Directory to create template files in"
7979
default = "./src"
80-
}
80+
}
81+
82+
variable "package_type" {
83+
type = string
84+
description = "Lambda deployment package type"
85+
default = "Zip"
86+
87+
validation {
88+
condition = contains(["Zip", "Image"], var.package_type)
89+
error_message = "Package type must be either Zip or Image."
90+
}
91+
}
92+
93+
variable "image_uri" {
94+
type = string
95+
description = "ECR image URI for container image deployment"
96+
default = null
97+
}
98+
99+
variable "image_config" {
100+
type = object({
101+
entry_point = optional(list(string))
102+
command = optional(list(string))
103+
working_directory = optional(string)
104+
})
105+
description = "Container image configuration"
106+
default = null
107+
}

0 commit comments

Comments
 (0)