Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom build step for SAM CLI

Tags:

When using the AWS SAM CLI to build a serverless application, it located dependencies magically and installs them all as the "build" step. For example, using a NodeJS application:

$> sam build
Building resource 'HelloWorldFunction'
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrc
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Running NodejsNpmBuilder:CleanUpNpmrc

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided

$>

Looking at the official documentation they're happy to simply treat it like magic, saying that it:

iterates through the functions in your application, looks for a manifest file (such as requirements.txt) that contains the dependencies, and automatically creates deployment artifacts that you can deploy to Lambda

But what if I have a dependency beyond just those specified in the manifest file? What if my code depends on a compiled binary file, or a static data file?

I would like to add additional build steps so that when I run sam build it compiles these files or copies them appropriately. Is there any way to do this?

like image 656
stevendesu Avatar asked Feb 11 '20 18:02

stevendesu


1 Answers

sam build is running npm install. So if you insert your own script into a step such as preinstall in package.json, sam build will also execute that step.

package.json

{
  ...
  "preinstall": "cp -r ../../../common ./"
  ...
}

The above preinstall script is a hack that embeds the common directory in the root folder of the sam inited project in the zip of each lambda handler so that it can be referenced from each.

You should also create a symbolic link in the local lambda handler directory, like ln -s ../common ./common, so that local and lambda work with the same code.

like image 100
ajido Avatar answered Sep 28 '22 11:09

ajido