In my previous post, we looked at the basic concept of AWS SAM and the high-level problem it solves. If that wasn't enough for you, keep reading!
AWS Serverless Application Model (SAM) templates are the building blocks of serverless applications on Amazon Web Services (AWS). Understanding the basic anatomy of an AWS SAM template is essential for harnessing the full power of serverless development. In this blog post, we'll delve into the fundamental elements that make up an AWS SAM template, unlocking the secrets to crafting scalable and efficient serverless applications.
YAML at Your Service - An AWS SAM template is written in YAML (YAML Ain't Markup Language), a human-readable and expressive data serialisation format. YAML allows for a concise and structured representation of your serverless application's resources and configurations. (Just watch those tabs/spaces, IYKYK!)
Transforming the Magic - At the beginning of your AWS SAM template, you'll find the Transform section. This is where the transformation magic happens.
Transform: AWS::Serverless-2016-10-31
The above declaration instructs AWS CloudFormation to use the AWS SAM syntax and capabilities to deploy and manage your serverless application. Why? Because SAM offers a lot of flexibility by providing special resource definitions that can save an engineer a lot of time NOT writing CloudFormation! Don't panic though, you can still use CloudFormation resources too!
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/format-version-structure.html
Parameters: Flexible Configurations - In the Parameters section, you can define customizable inputs to your AWS SAM template. Parameters allow for flexible configuration options, enabling you to modify aspects of your serverless application during deployment. You can specify parameter types, and default values, and even apply constraints to ensure the correct inputs are provided.
Parameters:
Stage:
Type: String
Default: Prod
Description: Deployment stage of the application
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html
Resources. The Building Blocks of Your Application - The heart of your AWS SAM template lies within the Resources section. Here, you define the AWS resources required for your serverless application, such as AWS Lambda functions, API Gateway endpoints, DynamoDB tables, and more. Each resource is represented by a unique logical ID and its corresponding properties.
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html
Functions: The Powerhouses of Your Application - Within the Resources section, AWS Lambda functions are defined under the AWS::Serverless::Function resource type. These functions encapsulate your code logic and can be triggered by various events, such as API Gateway requests or scheduled events. Specify the function's runtime, handler, memory allocation, and other properties to unleash its full potential.
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.8
Handler: lambda_function.handler
CodeUri: ./lambda_function
Event Sources: Triggers for Your Functions - To invoke your AWS Lambda functions, you'll need event sources. Event sources define the triggers that initiate the execution of your functions. They can be API Gateway endpoints, S3 bucket notifications, or even custom event sources. Connect your functions to the relevant event sources in the Resources section to enable seamless and event-driven serverless architecture.
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.8
Handler: lambda_function.handler
CodeUri: ./lambda_function
Events:
HelloWorldApi:
Type: Api
Properties:
Path: /hello
Method: get
Outputs: Communicating the Magic - Outputs allow you to communicate important information from your AWS SAM template to other resources or external systems. You can define outputs to export values such as API endpoint URLs or resource ARNs. These outputs can be referenced and utilized by other AWS CloudFormation stacks or external applications.
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html
Parameters:
Stage:
Type: String
Default: Prod
Description: Deployment stage of the application
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.8
Handler: lambda_function.handler
CodeUri: ./lambda_function
Events:
HelloWorldApi:
Type: Api
Properties:
Path: /hello
Method: get
Outputs:
HelloWorldApiUrl:
Description: URL of the Hello World API endpoint
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/${Stage}/hello"
Et Voila!!! You've now unlocked the basic anatomy of an AWS SAM template. By understanding the different sections and elements, you have the power to craft sophisticated and scalable serverless applications. With YAML as your trusty tool and the knowledge of transforms, resources, functions, event sources, outputs, and parameters, you can architect magical serverless experiences. Stay tuned for more exciting adventures in AWS SAM, where we'll explore advanced features and techniques to take your serverless applications to new heights. Happy serverless coding!
Source: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy.html
Top comments (0)