Skip to content

sam watch with Separate Lambda Layer and Function Stacks -- how to use newest layers in lambdas during development

0

I'm looking for the recommended local development workflow for a project that uses multiple, independent AWS SAM applications within a single monorepo.

Setup:

  1. Layers Stack: one SAM application dedicated entirely to shared Lambda Layers. This stack defines and deploys a CommonLayer, SpecificLayer, etc.
  2. Function Stacks: multiple other SAM applications for microservices. These applications define Lambda functions.
  3. Dependency: The function stacks are dependent on the layers stack. I link them during deployment by passing the full, version-specific ARN of a layer as a CloudFormation parameter (e.g., sam deploy --parameter-overrides CommonLayerVersionArn=...). This process works perfectly in our CI/CD pipeline.

What I Am Trying to Achieve (Local Development):

I want to establish a fast, iterative local development feedback loop using sam watch. My goal is to run two sam watch processes concurrently:

  • One sam watch for the layers application.
  • Another sam watch for a function application.

My expectation is that when I save a change to a file inside the CommonLayer, the layer's sam watch would sync it, and the function's sam watch would immediately pick up this new layer version, allowing me to test the change instantly.

The Question:

The observed behavior is that while the layer is updated (creating a new layer version), the function application is unaware of this change and continues to use the old layer version it was originally deployed with. I understand this is likely because updating a layer ARN is a configuration change, which sam sync is designed to skip for speed.

What is the recommended best practice for achieving a fast development feedback loop when working on Lambda Layers and the functions that consume them in this multi-stack SAM setup?

Am I supposed to use custom scripts to automate the deploy layer -> fetch new ARN -> deploy function cycle locally, or is there a more integrated sam feature or workflow I am missing?

Thank you for any insights or solutions you can share.

1 Answer
0

The challenge you're facing is common when working with Lambda layers across multiple SAM applications. The sam watch command doesn't automatically update function configurations to use new layer versions, as you've correctly observed.

Here are some recommended approaches for your local development workflow:

Option 1: Separate Stack for Layers with Manual Updates

Continue with your current architecture but implement a workflow to update the function configurations:

  1. Run sam watch for your layers stack
  2. When a layer changes, use AWS CLI to update your functions to use the new layer version:
aws lambda update-function-configuration \
--function-name your-function-name \
--layers arn:aws:lambda:region:account:layer:layer-name:new-version

You could automate this with a script that:

  • Watches for layer deployments
  • Retrieves the new layer ARN
  • Updates the function configuration

Option 2: Include Layers in Function Stacks During Development

For local development only, you could temporarily include the layer code directly in your function stacks:

  1. Define the layer in both your layers stack (for production) and function stacks (for development)
  2. In the function stack, reference the local layer definition instead of using a parameter
  3. When ready for production, switch back to using the parameter-based approach

Option 3: Custom Resource in CloudFormation

Implement a custom resource in your function stack that checks for the latest layer version and updates the function configuration accordingly. This would allow your functions to automatically use the latest layer version.

Option 4: Use Parameters with Manual Triggers

Continue using parameters but manually trigger redeployments:

  1. Run sam watch for your layers stack
  2. When a layer changes, manually run sam deploy for your function stack with the updated layer ARN parameter

Important considerations:

  • Keep your local development dependencies in sync with the Lambda Layer version
  • Ensure your build process excludes packages from your deployment bundle that are provided by the layer
  • Consider the trade-offs between development speed and maintaining production-like environments

While AWS SAM doesn't have a built-in feature to automatically update function configurations when layers change across stacks, these approaches can help you establish an efficient development workflow.
Sources
Is there a way to only update lambda layers? | AWS re:Post
Using AWS SAM with layers - AWS Lambda
Lambda layers - Powertools for AWS Lambda (TypeScript)

answered a year ago

AWS
SUPPORT ENGINEER

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.