diff --git a/non-prod/us-west-2/example-lambda/src/app.py b/non-prod/us-west-2/example-lambda/src/app.py new file mode 100644 index 0000000..7bd03e6 --- /dev/null +++ b/non-prod/us-west-2/example-lambda/src/app.py @@ -0,0 +1,69 @@ +""" +Lambda function handler for the example-lambda-non-prod function. +""" + +import json +import logging +import os + +# Configure logging +logger = logging.getLogger() +logger.setLevel(os.environ.get("LOG_LEVEL", "INFO")) + + +def handler(event, context): + """ + Main Lambda handler function. + + Args: + event: The event data passed to the Lambda function. + This could be from API Gateway, S3, SNS, CloudWatch Events, etc. + context: The Lambda runtime context object containing metadata + about the invocation, function, and execution environment. + + Returns: + dict: A response object with statusCode and body for API Gateway, + or any JSON-serializable object for other triggers. + """ + logger.info("Received event: %s", json.dumps(event)) + + # Extract request details if coming from API Gateway + http_method = event.get("httpMethod", "N/A") + path = event.get("path", "/") + query_params = event.get("queryStringParameters") or {} + body = event.get("body") + + # Parse body if it's JSON + if body: + try: + body = json.loads(body) + except json.JSONDecodeError: + pass # Keep body as-is if not valid JSON + + # Log context information + logger.info( + "Function: %s, Request ID: %s, Remaining time: %dms", + context.function_name, + context.aws_request_id, + context.get_remaining_time_in_millis(), + ) + + # Build response + response_body = { + "message": "Hello from example-lambda-non-prod!", + "function_name": context.function_name, + "request_id": context.aws_request_id, + "environment": "non-prod", + "event": event, + } + + # Return API Gateway compatible response + return { + "statusCode": 200, + "headers": { + "Content-Type": "application/json", + "X-Request-Id": context.aws_request_id, + }, + "body": json.dumps(response_body), + } + diff --git a/non-prod/us-west-2/example-lambda/terragrunt.hcl b/non-prod/us-west-2/example-lambda/terragrunt.hcl new file mode 100644 index 0000000..32e2021 --- /dev/null +++ b/non-prod/us-west-2/example-lambda/terragrunt.hcl @@ -0,0 +1,58 @@ +# --------------------------------------------------------------------------------------------------------------------- +# LAMBDA SERVICE +# This Terragrunt configuration deploys an AWS Lambda function using the lambda-service module +# from the Gruntwork Infrastructure Catalog Example. +# --------------------------------------------------------------------------------------------------------------------- + +terraform { + # You'll typically want to pin this to a particular version of your catalog repo. + # e.g. + # source = "git::git@github.com:gruntwork-io/terragrunt-infrastructure-catalog-example.git//modules/lambda-service?ref=v0.1.0" + source = "git::git@github.com:gruntwork-io/terragrunt-infrastructure-catalog-example.git//modules/lambda-service?ref=v1.0.0" + + # Zip the Lambda source code before running Terraform commands + before_hook "zip_lambda_source" { + commands = ["apply", "plan"] + execute = ["bash", "-c", "cd ${get_terragrunt_dir()}/src && zip -r ../lambda.zip ."] + } +} + +# Include the root `root.hcl` configuration. The root configuration contains settings that are common across all +# components and environments, such as how to configure remote state. +include "root" { + path = find_in_parent_folders("root.hcl") + expose = true +} + +# --------------------------------------------------------------------------------------------------------------------- +# MODULE INPUTS +# These are the variables we pass to the lambda-service module. +# See https://github.com/gruntwork-io/terragrunt-infrastructure-catalog-example/tree/main/modules/lambda-service +# for all available options. +# --------------------------------------------------------------------------------------------------------------------- +inputs = { + # Required: The name of the Lambda function + name = "example-lambda-non-prod" + + # Required: The runtime environment for the Lambda function + runtime = "python3.13" + + # Required: The function entrypoint in your code (file.function_name format) + handler = "app.handler" + + # Required: Path to the zipped Lambda function code (created by before_hook) + zip_file = "${get_terragrunt_dir()}/lambda.zip" + + # Required: Memory size in MB (128-10240) + memory_size = 128 + + # Required: Timeout in seconds (1-900) + timeout = 30 + + # Tags to apply to the Lambda function + tags = { + Environment = "non-prod" + ManagedBy = "terragrunt" + } +} +