This example borrows from several existing online repositories, or tutorials. They will be referenced below.
In particular, I've forked two repositories and modified that will be used. In particular the bootstrap script has been refactored to include a context object, and use error apis.
- Docker
- AWS Account and CLI
- AWS SAM CLI
git clone https://github.com/dheffx/aws-lambda-perl5-layer.git layers
git clone https://github.com/dheffx/aws-lambda-perl5-layer-example.git functions
make -C layers build-docker-image PERL_VERSION=5.28.1 CONTAINER_TAG=5.28
make -C layers build CONTAINER_TAG=5.28
Note: This can take a few minutes
This will build a docker image called lambda-perl-layer-foundation
, then will extract its contents as two zip files.
ls layers/*.zip
lambda-layer-perl-5.28.zip lambda-layer-perl-5.28-paws.zip
The first one is the base Perl runtime. The other is the lib for Paws.
Paws is installed because
- To demonstrate multiple layers for a single function
- When running functions in AWS, you probably want to access other AWS services
- It is a large amount of files that would increase the artifact size for each function that would otherwise vendor it
The base layer image is also used for running Carton to vendor the additional libraries defined in cpanfile for each function.
See the project README.md for more.
Each sample function consists of at least a handler script, such as handler.pl
. Most of them also contain a cpanfile
.
The Makefile in functions/
is set up to use the image made for the layers to build their artifact. Any modules defined in cpanfile
and bundled.
make -C functions build CONTAINER_TAG=5.28 FUNC_NAME=simple-yaml
make -C functions build CONTAINER_TAG=5.28 FUNC_NAME=plack
Some are just a single file and do not need the build, you can just zip them
zip -j functions/hello-world/hello-world.zip functions/hello-world/* -x *.zip
zip -j functions/s3-upload/s3-upload.zip functions/s3-upload/* -x *.zip
zip -j functions/python-system-perl/python-system-perl.zip functions/python-system-perl/* -x *.zip
The s3-upload
function utilizes the 2nd layer for Paws
aws s3 mb your-perl-faas-project-artifacts
export ARTIFACT_BUCKET=your-perl-faas-project-artifacts
use sam local
commands to execute functions locally
In short, SAM is not properly extracting layers from zip files for sam local
.
To get around this, unzip the layers to a temporary directory, and update template.yaml
to reference them
ContentUri: layers/tmp
Without doing this, sam local
will show an error of Error: Couldn't find valid bootstrap(s)
Direct Invoke
sam local invoke HelloWorld --event test/hello.event
sam local invoke SimpleYaml --event test/yaml.event
sam local invoke PythonSystemPerl --event test/hello.event
AWS Events
sam local generate-event s3 put | sam local invoke S3Upload
sam package \
--region $AWS_REGION \
--template-file template.yml \
--s3-bucket $ARTIFACT_BUCKET \
--output-template-file ./out.yml
aws cloudformation deploy \
--template-file ./out.yml \
--stack-name perl-faas \
--capabilities CAPABILITY_IAM
perl test/generate-files.pl outdir
aws s3 sync outdir/ s3://$UPLOAD_BUCKET/landing
aws s3 ls s3://$UPLOAD_BUCKET/processed
The two repositories that was forked for this example:
- https://github.com/moznion/aws-lambda-perl5-layer
- https://github.com/moznion/aws-lambda-perl5-layer-example
Here are a few other articles out there on this topic
I would not recommend using the bootstrap file found in these articles, but otherwise there is some good information in them.
AWS Docs