Skip to content

Commit de5c2b6

Browse files
authored
refactor: handle function code update when templates are used (#3)
1 parent e3af841 commit de5c2b6

1 file changed

Lines changed: 54 additions & 64 deletions

File tree

main.tf

Lines changed: 54 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,74 @@ locals {
33
use_prebuilt_zip = var.filename != null
44
}
55

6-
# Template files for user reference
7-
resource "local_file" "bootstrap_template" {
8-
count = var.create_templates ? 1 : 0
9-
filename = "${var.template_dir}/bootstrap"
10-
content = file("${path.module}/src/bootstrap")
11-
file_permission = "0755"
6+
# Template files for user reference - only created if they don't exist
7+
resource "null_resource" "bootstrap_template" {
8+
count = var.create_templates ? 1 : 0
9+
10+
triggers = {
11+
template_dir = var.template_dir
12+
}
13+
14+
provisioner "local-exec" {
15+
command = <<-EOT
16+
if [ ! -f "${var.template_dir}/bootstrap" ]; then
17+
mkdir -p "${var.template_dir}"
18+
cat > "${var.template_dir}/bootstrap" << 'EOF'
19+
${file("${path.module}/src/bootstrap")}
20+
EOF
21+
chmod 0755 "${var.template_dir}/bootstrap"
22+
fi
23+
EOT
24+
}
1225
}
1326

14-
resource "local_file" "handler_template" {
15-
count = var.create_templates ? 1 : 0
16-
filename = "${var.template_dir}/handler.sh"
17-
content = file("${path.module}/src/handler.sh")
18-
file_permission = "0755"
27+
resource "null_resource" "handler_template" {
28+
count = var.create_templates ? 1 : 0
29+
30+
triggers = {
31+
template_dir = var.template_dir
32+
}
33+
34+
provisioner "local-exec" {
35+
command = <<-EOT
36+
if [ ! -f "${var.template_dir}/handler.sh" ]; then
37+
mkdir -p "${var.template_dir}"
38+
cat > "${var.template_dir}/handler.sh" << 'EOF'
39+
${file("${path.module}/src/handler.sh")}
40+
EOF
41+
chmod 0755 "${var.template_dir}/handler.sh"
42+
fi
43+
EOT
44+
}
1945
}
2046

21-
resource "local_file" "makefile_template" {
22-
count = var.create_templates ? 1 : 0
23-
filename = "${var.template_dir}/Makefile"
24-
content = templatefile("${path.module}/Makefile", {
47+
resource "null_resource" "makefile_template" {
48+
count = var.create_templates ? 1 : 0
49+
50+
triggers = {
51+
template_dir = var.template_dir
2552
function_name = module.this.id
26-
})
53+
}
54+
55+
provisioner "local-exec" {
56+
command = <<-EOT
57+
if [ ! -f "${var.template_dir}/Makefile" ]; then
58+
mkdir -p "${var.template_dir}"
59+
cat > "${var.template_dir}/Makefile" << 'EOF'
60+
${templatefile("${path.module}/Makefile", { function_name = module.this.id })}
61+
EOF
62+
fi
63+
EOT
64+
}
2765
}
2866

29-
# Create zip package from source directory
30-
# data "archive_file" "lambda_zip" {
31-
# type = "zip"
32-
# source_dir = local.source_dir
33-
# output_path = "${path.module}/.terraform/tmp/${module.this.id}.zip"
34-
35-
# depends_on = [
36-
# local_file.bootstrap_template,
37-
# local_file.handler_template,
38-
# local_file.makefile_template
39-
# ]
40-
# }
41-
4267
# Create zip package from source directory (only for Zip package type and when not using prebuilt)
4368
data "archive_file" "lambda_zip" {
4469
count = var.package_type == "Zip" && !local.use_prebuilt_zip ? 1 : 0
4570

4671
type = "zip"
4772
source_dir = local.source_dir
4873
output_path = "${path.module}/.terraform/tmp/${module.this.id}.zip"
49-
50-
depends_on = [
51-
local_file.bootstrap_template,
52-
local_file.handler_template,
53-
local_file.makefile_template
54-
]
5574
}
5675

5776

@@ -89,35 +108,6 @@ resource "aws_cloudwatch_log_group" "lambda_logs" {
89108
tags = module.this.tags
90109
}
91110

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

0 commit comments

Comments
 (0)