SAM build - does it also build layers?

Since SAM Cli version v0.50.0, it is building layers as part of sam build.

Design document could be a good starting point to understand how it works.

Basically you have to set a custom BuildMethod with your lambda's target runtime:

MyLayer:
Type: AWS::Serverless::LayerVersion
Properties:
  ContentUri: my_layer
  CompatibleRuntimes:
    - python3.8
Metadata:
  BuildMethod: python3.8 (or nodejs8.10 etc..)

Warning: For compiled language as Java, it has a issue which it tries to build layers before functions. It's expected to have it fixed on the next release (PR opened already).


Quick answer - No, currently SAM does not build layers you define in a SAM template.yaml file.

It will only build any functions you define.

However (curiously) it will package (upload to S3) and deploy (setup within AWS, assign ARN so it can be used etc) any layers you define.


There is a feature request on the SAM github issues to implement layer building with SAM.


This can actually be hacked right now to get SAM to build your layers as well, by creating a dummy function in your SAM template file, as well as a layer entry, and having the layer ContentUri entry point to the .aws-sam build directory that gets created for the function.

See my post here on that.

That approach actually seems to work pretty well for twisting SAM right now to build your layers for you.