Easier Serverless Infrastructure Setup with Sam Template Creator

Easier Serverless Infrastructure Setup with Sam Template Creator

On several occasions, I have written blog posts on applications that accomplish their goals with AWS Lambda and managed services. Most of those projects used Infrastructure as Code (or IaC) because even in pet projects having your entire infrastructure described in a file makes life so much easier. You get exact knowledge of your infrastructure, source control, easier recovery in case of serious issues and less error-prone manual labor.

In cloud-native apps, IaC helps you to control an infrastructure which is almost by definition more complex than that of a monolithic application. Automation is useful when you have a single app on a single server doing everything, but it keeps you sane when dealing with dozens of services, running on multiple containers. The same goes for serverless, where our applications consists of large numbers of Lambdas using additional (managed) services to accomplish their tasks. In serverless, IaC can also serve as basic documentation, helping you understand the flow of events within your application.

Still, there are obstacles to conquer. For developers coming from an old-fashioned enterprise environment structured around silos, taking care of your own infrastructure takes some getting used to. And even for developers who are familiar with IaC, setting up your application requires a bit of repetitive work. For example, with AWS you will have to write Cloudformation templates describing your ‘resources’. Several frameworks exist to help you do this (Troposphere, Terraform, Serverless,...), but these too require repetitive work and present an additional learning curve.

This is where the sam template creator steps in. It’s a small project, written in Python, that will scan your project and create your serverless infrastructure - a SAM template - for you. See this GIF for an example.

No alt text provided for this image

The sam template creator helps both beginning and advanced AWS developers. Beginners spend less time going through documentation, figuring out how to get their infrastructure up and running, while those already familiar with SAM templates save time by avoiding repetitive work. Even for complex projects, the tool can generate a starting template that you can then refine and perfect. In short: it automates the boring parts.

The template creator requires no configuration. Nor does it leave a trace in your project except for the YAML template describing your infrastructure. Still, there are a few guidelines to get the most out of it, also described in the readme file. First (though the tool should still work if you ignore this advice), it is wise to avoid putting a lambda in the root of your project, or putting two in the same directory. These seem like minor limitations, especially as both AWS examples and the Serverless framework suggest an organisation of functions in separate directories as a good way of managing your project.

Second, some conventions help the creator detect event sources and endpoints for your Lambdas. If you want your function to receive events from S3, you can add ‘s3’ to the name of the event parameter, for example ‘s3_event’. The event and its bucket will be added to your template. If you want to put your Lambda behind an API Gateway, you can add the method and path to the name of the handler. For example, in Python, ‘put_secret_location_handler()’ will create a Lambda which reacts to PUTs to ‘/secret/location’. These conventions are simple and have the additional benefit of serving as documentation, with the name of your handler or event showing its intention. The only downside is that people need to be aware of the importance of naming, though this is only the case if you intent to use the tool to ‘refresh’ the template every time you add new lambdas, instead of using it once early on in your project, to generate an outline. Other conventions depend on the language. For Python, the name of the handler function should contain the word ‘handler’. As with putting your lambda in a separate directory, this convention is endorsed by examples from AWS.

The sam template creator can only be used for serverless projects on AWS. Right now it supports Python and Go, with Node and Java on the roadmap. Other improvements are also planned, including guidance for creating better templates, perhaps by asking some (optional) questions about your infrastructure. For those interested, the project is available on Github and can be installed using pip. Once installed, just run ‘sam-template-creator’ with the absolute or relative path to your project root as the location parameter.

And, naturally, any suggestions or help are appreciated!


Sam Van Overmeire, Cloud Developer


To view or add a comment, sign in

More articles by Sam Van Overmeire

Others also viewed

Explore content categories