stevendesu
stevendesu

Reputation: 16791

Custom build step for SAM CLI

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?

Upvotes: 8

Views: 3975

Answers (2)

ajido
ajido

Reputation: 96

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.

Upvotes: 8

me2resh
me2resh

Reputation: 886

You will need to wrap this command into another custom command and add the steps you need to it.

You can create a make file with multiple targets that satisfy your requirements.

I haven't used sam build before, I usually have a make target for that purpose.

you can give it a try with this bootstrap template here https://github.com/healthbridgeltd/nodejs-sam-bootstrap which is more efficient than using sam build.

Upvotes: 0

Related Questions