Amazon Web Services (AWS) recently open sourced its Serverless Application Model (SAM) implementation and added a new event source for CloudWatch Logs subscription filters. The preceding release featured a comprehensive expansion of options to configure and deploy REST APIs via Amazon API Gateway, including support for CORS headers, regional endpoints, and binary media types.
The Serverless Application Model (SAM) is an open source specification that extends AWS's infrastructure as code service CloudFormation with specialized resource types to "define serverless applications in simple and clean syntax". SAM is implemented as one of currently two available CloudFormation transforms, which aim to "simplify template authoring by condensing the expression of AWS infrastructure as code". When a template specifies an AWS::Serverless transform, CloudFormation expands the more concise SAM syntax into regular CloudFormation resource types before it uses the processed template to create or update a stack.
The authoring of SAM applications is assisted by the SAM Local CLI, which allows to "test functions locally, start a local API Gateway from a SAM template, validate a SAM template, and generate sample payloads for various event sources" (previous coverage). Developers can then use AWS CodePipeline, AWS CodeBuild, CloudFormation, and AWS CodeDeploy to continuously build and gradually deploy SAM applications via various canary or linear traffic shifting configurations. Compatible solutions can also be shared via the Serverless Application Repository, AWS's packaging, discovery and provisioning platform for SAM applications (previous coverage).
While the SAM specification has been open source from the get-go, the AWS team had to implement each accepted feature request behind closed doors. AWS has now lowered the barrier for community involvement by also open sourcing the code that transforms SAM templates into regular CloudFormation templates so that anyone can implement new features and collaborate with the AWS team via pull requests to accelerate acceptance. The Apache 2.0 licensed samtranslator source code and the readily available aws-sam-translator PyPI package will also make it easier for the serverless community to "integrate it with other frameworks and deployment providers".
Besides open sourcing the SAM implementation, SAM release 1.5 also added an event source for Amazon CloudWatch Logs that can trigger a Lambda function when log statements are matching the specified filter pattern:
# [...]
Resources:
LogsProcessor:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: lambda_function.lambda_handler
Runtime: python3.6
Events:
HelloInfoQSubscription:
Type: CloudWatchLogs
Properties:
LogGroupName: !Ref CloudWatchLambdaLogsGroup
FilterPattern: Hello InfoQ
The preceding SAM release 1.4 delivered the much requested Cross-Origin Resource Sharing (CORS) support for Amazon API Gateway, which can now be enabled by simply specifying a domain (a more granular Cors Configuration is also available). It also integrated the new API Gateway regional endpoints, binary media types, logging, metrics, and cache TTL settings, as well as AWS Lambda function concurrency, and various smaller improvements and bug fixes. The following template excerpt illustrates the conciseness of SAM's API Gateway support via the Globals section:
---
Transform: AWS::Serverless-2016-10-31
Globals:
Api:
# Allows www.infoq.com to call this API - SAM will also automatically
# add AllowMethods with a list of HTTP methods used within this API
Cors: "'https://www.infoq.com'"
EndpointConfiguration: REGIONAL
BinaryMediaTypes:
# These are equivalent to image/gif and image/png when deployed
- image~1gif
- image~1png
# [...]
The Serverless Application Model source code includes a development guide, a brief design overview, and the contributing guidelines. Documentation on the generated CloudFormation resources and related CloudFormation compatibility is also available. End user support for SAM has so far been provided via the AWS Lambda forum and will shift to the new AWS Serverless Application Repository forum going forward. Developers interested in collaboration with the SAM community and team are encouraged to join the AWS SAM Developers channel (#samdev).