Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Get function name from Terraform output
FUNCTION_NAME := $(shell terraform output -raw function_name)
# Function name from environment variable
FUNCTION_NAME := $(or $(FUNCTION_NAME),$(error FUNCTION_NAME environment variable not set))

# Default target
.PHONY: help
help:
@echo "Usage: export FUNCTION_NAME=your-function-name"
@echo ""
@echo "Available targets:"
@echo " deploy - Package and deploy function code"
@echo " invoke - Test function invocation"
Expand Down
52 changes: 40 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ Terraform module that creates a Lambda function with zip packaging, IAM executio

## Usage

### Basic Usage with Template Generation

```hcl
module "lambda_function" {
source = "git::https://github.com/ql4b/terraform-aws-lambda-function.git"

source_dir = "./src"
source_dir = "./src"
create_templates = true
template_dir = "./src"

context = {
namespace = "myorg"
Expand All @@ -27,18 +31,23 @@ module "lambda_function" {
}
```

This will automatically create `bootstrap`, `handler.sh`, and `Makefile` in your `./src` directory.

## Advanced Usage

```hcl
module "lambda_function" {
source = "git::https://github.com/ql4b/terraform-aws-lambda-function.git"

source_dir = "./src"
handler = "bootstrap"
runtime = "provided.al2023"
architecture = "arm64"
memory_size = 256
timeout = 30
source_dir = "./src"
create_templates = true
template_dir = "./src"

handler = "bootstrap"
runtime = "provided.al2023"
architecture = "arm64"
memory_size = 256
timeout = 30

environment_variables = {
HANDLER = "/var/task/handler.sh"
Expand All @@ -55,12 +64,21 @@ module "lambda_function" {
}
```

## Source Directory Structure
## Template Generation

Set `create_templates = true` to automatically generate starter files:

- `bootstrap` - Lambda runtime bootstrap (executable)
- `handler.sh` - Example function handler
- `Makefile` - Deployment and testing commands

### Generated Directory Structure

```
src/
├── bootstrap # Lambda runtime bootstrap (executable)
└── handler.sh # Your function code
├── handler.sh # Your function code
└── Makefile # Deployment commands
```

## Example Bootstrap
Expand Down Expand Up @@ -111,11 +129,20 @@ terraform init
terraform apply
```

### 2. Use the Included Makefile
### 2. Set Function Name

```bash
export FUNCTION_NAME=$(terraform output -raw function_name)
```

### 3. Use the Generated Makefile

The module includes a `Makefile` for simple deployment workflows:
When `create_templates = true`, a `Makefile` is generated with deployment workflows:

```bash
# Set function name
export FUNCTION_NAME=$(terraform output -raw function_name)

# Deploy function code
make deploy

Expand All @@ -132,7 +159,7 @@ make clean
make help
```

### 3. Manual Deployment (Alternative)
### 4. Manual Deployment (Alternative)

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

## Integration with Lambda Layers

Expand Down
23 changes: 23 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@ locals {
source_dir = var.source_dir
}

# Template files for user reference
resource "local_file" "bootstrap_template" {
count = var.create_templates ? 1 : 0
filename = "${var.template_dir}/bootstrap"
content = file("${path.module}/src/bootstrap")
file_permission = "0755"
}

resource "local_file" "handler_template" {
count = var.create_templates ? 1 : 0
filename = "${var.template_dir}/handler.sh"
content = file("${path.module}/src/handler.sh")
file_permission = "0755"
}

resource "local_file" "makefile_template" {
count = var.create_templates ? 1 : 0
filename = "${var.template_dir}/Makefile"
content = templatefile("${path.module}/Makefile", {
function_name = module.this.id
})
}

# Create zip package from source directory
data "archive_file" "lambda_zip" {
type = "zip"
Expand Down
10 changes: 10 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@ output "package_path" {
output "package_size" {
description = "Size of the Lambda deployment package"
value = data.archive_file.lambda_zip.output_size
}

# Template outputs
output "template_files" {
description = "Paths to created template files"
value = var.create_templates ? {
bootstrap = "${var.template_dir}/bootstrap"
handler = "${var.template_dir}/handler.sh"
makefile = "${var.template_dir}/Makefile"
} : {}
}
12 changes: 12 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,16 @@ variable "log_retention_days" {
], var.log_retention_days)
error_message = "Log retention days must be a valid CloudWatch retention period."
}
}

variable "create_templates" {
type = bool
description = "Create template files (bootstrap, handler, Makefile) in template_dir"
default = false
}

variable "template_dir" {
type = string
description = "Directory to create template files in"
default = "./src"
}