The recently launched Serverless Patterns Collection is a repository of serverless examples that demonstrate integrating two or more AWS services. Each pattern uses either the AWS Serverless Application Model (AWS SAM) or AWS Cloud Development Kit (AWS CDK). These simplify the creation and configuration of the services referenced.
The Serverless Patterns Collection is both an educational resource to help developers understand how to join different services, and an aid for developers that are getting started with building serverless applications.
By using these patterns, you can experience first-hand what is possible in the realm of serverless, experiment with the many use cases that the patterns provide, and easily deploy your applications to the AWS Cloud.
The entire collection is also available in GitHub so that you can clone it locally and build upon it in your organization. This post explains the benefits of patterns and walks through an example that you can deploy to your AWS account.
Getting started with the Serverless Patterns Collection
To get started with the Serverless Patterns Collection, go to the Serverless Patterns home page.
The filters on the left-hand side allow you to select from one of the common services used in serverless applications or from the desired deployment type. By filtering, you see all of the patterns that a service integrates with, on both the upstream or downstream side. Once you find the pattern you want to use, choose the View Pattern button below it. Here you see instructions on downloading, deploying, testing, and cleaning up the resources from your deployed patterns applications.
The Serverless Patterns Collection uses both AWS SAM templates and CDK templates. Patterns show directly which deployment model they support, either AWS SAM or CDK, as seen in the following pattern card. Both are translated into AWS CloudFormation in the deploy process.
Each template provides instructions on how to configure the resources and any security roles or policies required. Additionally, there is a GitHub link at the bottom of the page that redirects you to the code for the pattern.
Structure of an AWS SAM template
The AWS Serverless Application Model (AWS SAM) is an open-source framework for building serverless applications. It provides shorthand syntax that makes it easier to build and deploy serverless applications. With only a few lines, you can define each resource using YAML.
An AWS SAM template can have serverless-specific resources or standard AWS CloudFormation resources. When you run
sam deploy, the serverless resources get transformed into CloudFormation syntax.
Structure of an AWS CDK template
The AWS Cloud Development Kit (CDK) is another way to define your cloud application resources using common programming languages. The CDK is an open source framework that you can use to model your applications.
As with AWS SAM, when you run
npx cdk deploy --app 'ts-node .', the CDK transforms the template into AWS CloudFormation syntax and creates the specified resources for you.
Benefits of infrastructure as code
A few of the many benefits of this approach of using infrastructure as code as opposed to building applications in the console are: flexibility, repeatability, and reusability. You have the flexibility to manage all of the resources in one place. You can consistently deploy the same application in multiple Regions with one command. You have the flexibility to deploy a single template to any environment. These characteristics avoid the environment drift problem. By using this infrastructure as code tool, you can manage your applications more easily in one place.
For this tutorial, you need:
Setting up the AWS Lambda to Amazon S3 pattern
In this tutorial, you create a Lambda function using the following AWS SAM template, and then add an object to an S3 bucket every time it is invoked. You create two resources from the AWS SAM template: an Amazon S3 bucket that stores objects, and an AWS Lambda function that puts the object in the S3 bucket when it is invoked.
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Creates a Lambda function that writes to an S3 bucket Parameters: DestinationBucketName: Type: String Resources: ## S3 bucket DestinationBucket: Type: AWS::S3::Bucket Properties: BucketName: !Ref DestinationBucketName ## Lambda function PutObjectFunction: Type: AWS::Serverless::Function Properties: CodeUri: src/ Handler: app.lambda_handler Runtime: python3.8 MemorySize: 128 Environment: Variables: DestinationBucketName: !Ref DestinationBucketName Policies: - S3CrudPolicy: BucketName: !Ref DestinationBucketName
Note: When you create the S3 bucket below, keep in mind that bucket names are unique, not just for your AWS account, but across all AWS accounts. Choose a distinct name that is globally unique. If you get an error from CloudFormation saying the resource already exists, this means that it’s not unique.
To deploy this pattern:
- Navigate to the Serverless Patterns Collection website.
- Clone the repository and cd into the pattern’s directory.
Before deploying this pattern, take a look at the Lambda function code. From the lambda-s3 directory, open the app.py file in the src subdirectory.
def lambda_handler(event, context): # Upload the file data = b'Hello World' client = boto3.client('s3') response = client.put_object(Body=data, Bucket=bucket, Key='filename.txt') return True
The Lambda function puts the
filename.txt object into the destination bucket that you specify with the data
Hello World in it.
- Run sam
deploy --guided, and when prompted, enter a name for the Destination Bucket. This is the S3 bucket that stores the
- You see this confirmation message once your app is deployed:
- Navigate to the Lambda Console and in the upper right-hand corner make sure you are in the Region that was listed in the CLI previously. Find the Lambda function, and choose Test.
- Enter a name for the test event and choose Create. There is no need to change the event payload.
- Choose test again and notice in the Execution Results tab, you see a succeeded status.
- Navigate to the S3 console and choose the bucket you created from the AWS SAM CLI. You see the new object that was created from invoking the Lambda function.
When you are building your applications, this specific pattern is useful when you need to store data from your Lambda function.
Sometimes, when you run sam deploy, you see an error from CloudFormation that says
UPDATE_ROLLBACK_FAILED. The stack contains an error message that can help explain what went wrong.
If your stack is stuck in the
UPDATE_ROLLBACK_FAILED state, navigate to the CloudFormation console, select your stack and then choose Delete. This deletes your stack and you can deploy the application again.
Once you have completed this tutorial, be sure to remove the stack from CloudFormation and delete the S3 bucket. From the CloudFormation console, select your application, and then choose Delete. From the S3 console, select your bucket, empty it, and delete it.
Cleanup instructions for each pattern can be found on each pattern’s homepage.
Cost of services
S3 charges for data storage and access but not for creating a bucket. You can review the costs for Amazon S3 on its pricing page. In the AWS Free Tier, you get 1 million invocations per month for free. You can find the Lambda costs on the Lambda pricing page.
Submit a pattern to the Serverless Land Patterns Collection
While there are many patterns available to use from the Serverless Land website, there is also the option to create your own pattern and submit it. From the Serverless Patterns Collection main page, choose Submit a Pattern.
There you see guidance on how to submit. We have added many patterns from the community and we are excited to see what you build!
In this post, I show you how to use serverless patterns, deploy your application with the AWS SAM CLI, and how to troubleshoot errors. With the serverless patterns, you can learn about what types of integrations are possible with AWS serverless technologies. You can also learn how to use infrastructure as code tools to manage and deploy applications. Lastly you get a full example of how to test an application’s functionality.
For more serverless learning resources, visit Serverless Land.