diff --git a/Makefile b/Makefile index 8420bdc..fcb1e5d 100644 --- a/Makefile +++ b/Makefile @@ -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" diff --git a/README.md b/README.md index d5948e9..523b2cb 100644 --- a/README.md +++ b/README.md @@ -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" @@ -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" @@ -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 @@ -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 @@ -132,7 +159,7 @@ make clean make help ``` -### 3. Manual Deployment (Alternative) +### 4. Manual Deployment (Alternative) ```bash # Package and deploy manually @@ -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 diff --git a/main.tf b/main.tf index 042346f..9f0acde 100644 --- a/main.tf +++ b/main.tf @@ -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" diff --git a/outputs.tf b/outputs.tf index 6b5e71f..336c787 100644 --- a/outputs.tf +++ b/outputs.tf @@ -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" + } : {} } \ No newline at end of file diff --git a/variables.tf b/variables.tf index 90163fa..edcde59 100644 --- a/variables.tf +++ b/variables.tf @@ -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" } \ No newline at end of file