AWS Serverless: minify/uglify the sam build lambda output - node.js

I have a few AWS Lambda functions with APIGateway. I'm using the serverless approach, packing and deploying the app using SAM CLI. It outputs the separate function built inside the .aws-sam directory in the project root. I'd like to minify & uglify that source code before the package actually uploads the S3 bucket for deployment. I'm referring to the SAM CLI Docs, but nothing related to customized packaging or using bundlers has been mentioned. Is there a workaround to bundle the sourcecode with minification/uglification?

You can create a custom build runtime described here
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/building-custom-runtimes.html
or (if you only want to use minification) you can use esbuild
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-build-typescript.html

Related

Package or zip archive creation with serverless but without lambda

It is needed to create zip file before deployment and there is no lambda functions. Just some folder to zip and upload. Without functions: section serverless skip packaging. Any ideas? Any work arounds?
The Serverless Framework generates two things, a zip file containing the code of your Lambda function(s) which is zipped and uploaded to AWS, and the CloudFormation JSON template which is sent to the CloudFormation API.
If you don't have any functions, there are no artifacts/code to zip - so packaging is not necessary.
As your stack only contains CloudFormation - you can simply run serverless deploy to apply those changes.

AWS CloudFormation with node.js 10.x Update Error "ZipFile can only be used when Runtime is set to <older node.js versions>"

We are using CloudFormation template to deploy some intermediate code on Lambda function.
We are using ZipFile function to add inline code through CloudFormation.
Current runtime for lambda function is node.js 8.10.
We need to update node version to 10.x.
While updating Lambda using cloudformation we are getting below error:
ZipFile can only be used when Runtime is set to either of nodejs,
nodejs4.3, nodejs6.10, nodejs8.10, python2.7, python3.6, python3.7.
I believe that this is a known issue. https://forums.aws.amazon.com/thread.jspa?threadID=303166&tstart=0
As of this writing, it is still an issue. My suggestion is to have super basic code in an S3 bucket and reference that instead of using a zip file and deploy your actually code after the lambda function is created. Alternatively, you can just upload your zip artifact to an S3 bucket. If your code is proprietary be careful about S3.

Deploying lambda applications on AWS

I am writing AWS Lambda functions for my project (node) and now trying to figure out the best way to deploy these. Ideally I would want to
Convert the ES7 code into a version lambda can understand possibly using webpack.
Deploy using a command line tool or some npm package.
What is the best way to get this done?
You can use serverless and serverless-webpack to do both of those.
serverless is a deployment and configuration tool.
serverless-webpack is a plugin for serverless that will compile your ES7 code upon deployment.
Either you canzip it and upload it or you can use automate the deployment with AWS CodePipelne. Please see here more details about automated deployment

How to work with bucklescript requires and google functions

I'm trying to deploy functions created with bucklescript to google functions but the deploy won't run without this error :
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'bs-platform/lib/js/js_json.js'
I'm using the gcloud beta functions deploy utility. My code is using the Js.Json module, which produce var Js_json = require("bs-platform/lib/js/js_json.js"); in the outputed js code. My package.json contains the bs-platform package.
Is there a way to setup bucklescript or the gcloud utility to make my code acceptable?
BuckleScript's requires are just standard CommonJS requires, and can be bundled up into a single file using a bundler like webpack. You can also configure bsb to emit es6 modules (see the package-specs property of the bsconfig.json schema) and bundle them up using rollup.

Deploy Lambda functions without binaries

I have some problems with serverless deploy, because when I deploy my Lambda function, the Serverless Framework start packing my node_modules, but it takes a lot of time.
I mean why to upload node_modules again if it have not been updated. Maybe somebody know, how deploy only a Lambda function code without packing binaries?
You need to add packaging configuration.
In your serverless.yml file, add:
package:
exclude:
- node_modules/**
It is useful to remove the AWS-SDK modules (because if you don't upload them, Lambda will use what AWS offers - which is better) and to remove dev modules (like testing frameworks). However, all other modules are dependencies and will be needed to be uploaded for your function to work properly. So, configure the package settings to include/exclude exactly what you need.
Regarding your other question
why to upload node_modules again if it have not been updated
It is not a limitation of the Serverless Framework. It is a limitation of the AWS Lambda service. You can't make a partial upload of a Lambda function. Lambda always requires that the uploaded zip package contains the updated code and all required modules dependencies.
If your deploy is taking too long, maybe you should consider breaking this Lambda function into smaller units.

Resources