Deploy Lambda Functions from CLI with Layers
Package and deploy AWS Lambda functions using the CLI, including dependency management through Lambda layers for smaller deployment bundles.
Prerequisites
- -AWS CLI v2 installed and configured
- -IAM role with Lambda execution permissions
- -Node.js or Python runtime available locally
Steps
Package the function code
Create a ZIP archive containing your Lambda function handler and any application-specific modules.
Exclude test files and node_modules from the function ZIP. Dependencies should go in a layer instead.
Create or update the Lambda function
Deploy the function code ZIP to Lambda. Use create-function for the first deploy, or update-function-code for subsequent deploys.
The fileb:// prefix is required for binary file uploads. Using file:// will corrupt the ZIP.
Package the layer dependencies
Create a layer ZIP with the correct directory structure for your runtime. Node.js expects a nodejs/node_modules path.
Lambda layers must follow a specific directory structure: nodejs/node_modules/ for Node.js, python/ for Python.
Publish the Lambda layer
Upload the dependency layer to Lambda. Each publish creates a new immutable version that can be referenced by functions.
Attach the layer to the function
Update the function configuration to reference the new layer version, making the packaged dependencies available at runtime.
You can attach up to 5 layers to a single function. The total unzipped size of function code plus all layers must be under 250 MB.
Test the deployed function
Invoke the Lambda function with a test payload and view the response to confirm the deployment works correctly.
Use --cli-binary-format raw-in-base64-out when passing JSON payloads directly, otherwise the CLI will base64-encode your payload.
Full Script
FAQ
Discussion
Loading comments...