

Using Serverless Framework to build and deploy Docker images for AWS Lambdas
source link: https://www.kevinhooke.com/2021/11/20/using-serverless-framework-to-build-and-deploy-docker-images-for-aws-lambdas/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Using Serverless Framework to build and deploy Docker images for AWS Lambdas
AWS Lambdas can be packaged and deployed using a Docker image, described in the docs here.
Serverless Framework makes building and deploying a Docker based Lambda incredibly simple. If you have a simplest Dockerfile like this (from the docs here):
FROM public.ecr.aws/lambda/nodejs:14 # Assumes your function is named "app.js", and there is a package.json file in the app directory COPY app.js package.json ${LAMBDA_TASK_ROOT} # Install NPM dependencies for function RUN npm install # Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile) CMD [ "app.handler" ]
The handler to be packaged in this image is this simplest hello world function:
exports.handler = async function(event, context) { console.log("EVENT: \n" + JSON.stringify(event, null, 2)) return "hello!" }
To define a Lambda using this image, with Serverless define an ECR section like this to define your image, which will get built using the above Dockerfile in the same folder:
service: lambda-container-1 provider: name: aws ecr: images: lambda-container-example: path: ./ functions: hello: image: name: lambda-container-example
Run ‘serverless deploy’ and it builds the image, uploads to ECR, and deploys the Lambda all for you.
After deploying, on first test I got this error:
"errorMessage": "RequestId: 58fe500f-26ee-44ba-b6a9-6079b6ff2896 Error: fork/exec /lambda-entrypoint.sh: exec format error", "errorType": "Runtime.InvalidEntrypoint"
The key part of this error is this “exec format error”. I’m building and deploying this from my M1 MacBook Pro, which is Apple’s arm64, not x64.
If we look in the AWS Console for this lambda, on the first page under Image you’ll see the Lambda runtime architecture:
Updating the serverless.yml to include ‘architecture: arm64’, redeploy and now the architecture is arm64:
Invoking with ‘serverless invoke –function hello’ and now it successfully runs!
Recommend
-
80
In this post, we look at how we enable Pulumi to build and deploy serverless functions in AWS, Azure, or GCP using lambda functions in JavaScript and TypeScript.
-
9
AWS Lambda errors with Java Lambdas and Serverless framework Using a basic Servlerless framework event config like this: functions: hello: handler: kh.javalambda.HelloLambda::myHandler events: - http: ...
-
4
-
5
Serverless framework deploy for Java AWS Lambdas The Serverless framework ‘serverless deploy’ deploys whatever .jar you have packaged and ready to go within your source project. If you make local code changes but d...
-
6
Serverless Framework: AWS Lambdas with scheduled events and parameters To configure an AWS Lambda to get triggered by a CloudWatch event, you can use the ‘schedule’ event in your config: functions: your-functio...
-
7
‘serverless invoke local’ with Java Lambdas If you create a Java Lambda with the provided template, you’re probably returning a response using the provided ApiGatewayResponseClass. If you run your Lambda locally with ‘server...
-
7
Deploy OpenFaaS Serverless Framework on KubernetesWelcome to this guide on how to deploy OpenFaaS Serverless Framework on Kubernetes. But before we commence on the crux of this tool, I would like to explain a few concepts.Serverless is...
-
5
Deploying changes to individual Lambdas using Serverless Framework I have a serverless project that deploys 2 Lambdas in the same stack: service: example-apis2 provider: name: aws memorySize: 512 region: u...
-
12
.NET 6 Lambdas on ARM64 - Part 2, ServerlessDownload full source code.This is...
-
7
Announcement I will be speaking at Percona Live 2023 about serverless PostgreSQL. Join us at this event if you are interested! Introduction Recently, P...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK