The idea behind AWS Lambda is great — no servers or software to manage sounds very attractive to me as a developer. But it turned out that managing Lambda functions is not trivial as well. Apart from many new concepts and tools provided by AWS, there are several 3rd-party tools built to help you deal with AWS Lambda. Those tools require you to learn many new concepts and interfaces which may be tough for a beginner. Therefore, I created a starter kit project which will help you to start using NodeJS Lambda functions and learning the AWS Lambda stack.

The starter kit uses the official AWS CLI and CloudFormation to manage Lambda resources. No other dependencies are required. The starter kit glues various AWS CLI commands via several very basic shell scripts so it is easy to learn and change. Additionally, the starter kit unlike many other projects out there is using YAML for writing CloudFormation templates so that templates become much easier to read.

By default, the kit contains one Lambda function called FooFunction. The function outputs foo when invoked.

The source code of Lambda functions is stored in S3 so you need to create an S3 bucket first. The bucket name (S3_BUCKET_NAME) is configured in config.env.
Feel free to change it as well as the STACK_NAME.

Deploying the project

To deploy the project functions, use the following command:

npm run create-stack

This command creates zip archives with your functions and uploads them to S3. The command generates two files for every functions FunctionName-latest.zip and FunctionName-Version.zip, where Version is the version number of the function. It can be an md5 hash of the zip file or anything else. There is another convention: the version number for a function is provided by a build.sh that is function-specific and that is supposed to output only the version number.

After zip files are uploaded to S3, the command creates a new CloudFormation stack which, in turn, creates the actual Lambda resources (or other resources) on AWS. Here is how the stack appears in the CloudFormation console:

Updating the project

Once you modified the source code of your project and want to deploy the new version:

npm run update-stack

This will upload new versions of the functions to S3 and update the same CloudFormation stack so that changes are actually deployed.

Deleting the project

You can always delete all resources belonging to the project by running

npm run delete-stack

Internals

Every function is supposed to have a build.sh file which builds zip files and uploads them to S3 and outputs the version of the function. Here is how it may look like:

Cloudformation

AWS resources are defined in cloudformation.yaml. This file describes all functions of the project and this implies that all functions and all resources are updated at the same time. Here is how the default cloudformation.yaml looks like:

Here, you can tweak FooFunction to change the parameters of the Lambda function and change FooFunctionRole to grant additional permissions (such as DynamoDB or SNS access) to your function.

If you don’t want to create a new role, you can re-use existing one and delete FooFunctionRole. In this case, you don’t need to specify the --capabilities CAPABILITY_IAM parameter in the create and update operations.

Adding New Functions

To add a new function you need to create a new folder where the function will be located and add references to the new function wherever you find a TODO comment in the shell scripts. Additionally, you need to create a build.sh which will build your function and output its version. Also the function resource and its role need to be added to the cloudformation.yaml.

I hope this starter kit will be helpful to anyone who starts working with AWS Lambda and that it is a good introduction to more complicated technologies like terraform, apex or node-lambda. Thanks for reading!