Skip to content

Commit 8903869

Browse files
authored
feat: intial version (#1)
* feat: expose bootstrap files and Makefile through templates * refactor: let the user export the FUNCTION_NAME variable to user with the Makefile
1 parent 6b20a76 commit 8903869

File tree

5 files changed

+89
-14
lines changed

5 files changed

+89
-14
lines changed

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# Get function name from Terraform output
2-
FUNCTION_NAME := $(shell terraform output -raw function_name)
1+
# Function name from environment variable
2+
FUNCTION_NAME := $(or $(FUNCTION_NAME),$(error FUNCTION_NAME environment variable not set))
33

44
# Default target
55
.PHONY: help
66
help:
7+
@echo "Usage: export FUNCTION_NAME=your-function-name"
8+
@echo ""
79
@echo "Available targets:"
810
@echo " deploy - Package and deploy function code"
911
@echo " invoke - Test function invocation"

README.md

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ Terraform module that creates a Lambda function with zip packaging, IAM executio
1414

1515
## Usage
1616

17+
### Basic Usage with Template Generation
18+
1719
```hcl
1820
module "lambda_function" {
1921
source = "git::https://github.com/ql4b/terraform-aws-lambda-function.git"
2022
21-
source_dir = "./src"
23+
source_dir = "./src"
24+
create_templates = true
25+
template_dir = "./src"
2226
2327
context = {
2428
namespace = "myorg"
@@ -27,18 +31,23 @@ module "lambda_function" {
2731
}
2832
```
2933

34+
This will automatically create `bootstrap`, `handler.sh`, and `Makefile` in your `./src` directory.
35+
3036
## Advanced Usage
3137

3238
```hcl
3339
module "lambda_function" {
3440
source = "git::https://github.com/ql4b/terraform-aws-lambda-function.git"
3541
36-
source_dir = "./src"
37-
handler = "bootstrap"
38-
runtime = "provided.al2023"
39-
architecture = "arm64"
40-
memory_size = 256
41-
timeout = 30
42+
source_dir = "./src"
43+
create_templates = true
44+
template_dir = "./src"
45+
46+
handler = "bootstrap"
47+
runtime = "provided.al2023"
48+
architecture = "arm64"
49+
memory_size = 256
50+
timeout = 30
4251
4352
environment_variables = {
4453
HANDLER = "/var/task/handler.sh"
@@ -55,12 +64,21 @@ module "lambda_function" {
5564
}
5665
```
5766

58-
## Source Directory Structure
67+
## Template Generation
68+
69+
Set `create_templates = true` to automatically generate starter files:
70+
71+
- `bootstrap` - Lambda runtime bootstrap (executable)
72+
- `handler.sh` - Example function handler
73+
- `Makefile` - Deployment and testing commands
74+
75+
### Generated Directory Structure
5976

6077
```
6178
src/
6279
├── bootstrap # Lambda runtime bootstrap (executable)
63-
└── handler.sh # Your function code
80+
├── handler.sh # Your function code
81+
└── Makefile # Deployment commands
6482
```
6583

6684
## Example Bootstrap
@@ -111,11 +129,20 @@ terraform init
111129
terraform apply
112130
```
113131

114-
### 2. Use the Included Makefile
132+
### 2. Set Function Name
133+
134+
```bash
135+
export FUNCTION_NAME=$(terraform output -raw function_name)
136+
```
137+
138+
### 3. Use the Generated Makefile
115139

116-
The module includes a `Makefile` for simple deployment workflows:
140+
When `create_templates = true`, a `Makefile` is generated with deployment workflows:
117141

118142
```bash
143+
# Set function name
144+
export FUNCTION_NAME=$(terraform output -raw function_name)
145+
119146
# Deploy function code
120147
make deploy
121148

@@ -132,7 +159,7 @@ make clean
132159
make help
133160
```
134161

135-
### 3. Manual Deployment (Alternative)
162+
### 4. Manual Deployment (Alternative)
136163

137164
```bash
138165
# Package and deploy manually
@@ -159,6 +186,7 @@ cat /tmp/response.json
159186
- `function_arn` - Lambda function ARN
160187
- `execution_role_arn` - IAM execution role ARN
161188
- `log_group_name` - CloudWatch log group name
189+
- `template_files` - Paths to generated template files (when `create_templates = true`)
162190

163191
## Integration with Lambda Layers
164192

main.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@ locals {
22
source_dir = var.source_dir
33
}
44

5+
# Template files for user reference
6+
resource "local_file" "bootstrap_template" {
7+
count = var.create_templates ? 1 : 0
8+
filename = "${var.template_dir}/bootstrap"
9+
content = file("${path.module}/src/bootstrap")
10+
file_permission = "0755"
11+
}
12+
13+
resource "local_file" "handler_template" {
14+
count = var.create_templates ? 1 : 0
15+
filename = "${var.template_dir}/handler.sh"
16+
content = file("${path.module}/src/handler.sh")
17+
file_permission = "0755"
18+
}
19+
20+
resource "local_file" "makefile_template" {
21+
count = var.create_templates ? 1 : 0
22+
filename = "${var.template_dir}/Makefile"
23+
content = templatefile("${path.module}/Makefile", {
24+
function_name = module.this.id
25+
})
26+
}
27+
528
# Create zip package from source directory
629
data "archive_file" "lambda_zip" {
730
type = "zip"

outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,14 @@ output "package_path" {
7070
output "package_size" {
7171
description = "Size of the Lambda deployment package"
7272
value = data.archive_file.lambda_zip.output_size
73+
}
74+
75+
# Template outputs
76+
output "template_files" {
77+
description = "Paths to created template files"
78+
value = var.create_templates ? {
79+
bootstrap = "${var.template_dir}/bootstrap"
80+
handler = "${var.template_dir}/handler.sh"
81+
makefile = "${var.template_dir}/Makefile"
82+
} : {}
7383
}

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,16 @@ variable "log_retention_days" {
6565
], var.log_retention_days)
6666
error_message = "Log retention days must be a valid CloudWatch retention period."
6767
}
68+
}
69+
70+
variable "create_templates" {
71+
type = bool
72+
description = "Create template files (bootstrap, handler, Makefile) in template_dir"
73+
default = false
74+
}
75+
76+
variable "template_dir" {
77+
type = string
78+
description = "Directory to create template files in"
79+
default = "./src"
6880
}

0 commit comments

Comments
 (0)