Do you feel slowed down by the time and effort required to manage servers and infrastructure for your application? Are you searching for a simpler way to develop and deploy your applications faster? AWS Serverless Application Model is here for you. In this article, we’ll introduce SAM and show you how to deploy a simple serverless app.
Serverless computing is quickly becoming a popular choice for building and deploying applications in the cloud. With serverless computing, developers can focus on writing code and building applications without having to worry about managing servers, infrastructure, or scaling. AWS Lambda, the leading serverless computing service, has made it easy for developers to build and deploy serverless applications on AWS. However, managing the infrastructure for these applications, and ensuring everything align with best practices can still be complex and time-consuming. And this is where the AWS SAM (Serverless Application Model) fits in.
What is AWS SAM ?
AWS SAM (Serverless Application Model) is a framework that simplifies the process of building serverless applications on Amazon Web Services (AWS).
It makes it easier for developers to develop, test, and deploy serverless applications on AWS. By providing a standardized way to define and manage the resources required by an application , AWS SAM streamlines the development process and helps ensure that the application is scalable, reliable, secure and follow best practices.
This framework allows developers to define the various resources their application needs, such as APIs, functions, and tables, using a YAML or JSON file. For example it can provide a simplified way to define the AWS API Gateway APIs, AWS Lambda functions, and Amazon DynamoDB tables needed by your serverless application.
Some cool things about AWS SAM
- With AWS SAM, you can define the resources your application needs using a YAML or JSON file, making it easy to share and version control with other developers
- It supports popular programming languages such as Python, Node.js, Go and Java , and has many templates that will hep you to quickly get started.
- Integration with AWS Services : AWS SAM integrates with other AWS services, such as Amazon S3, Amazon Kinesis, and Amazon SNS. This allows developers to build complex serverless applications that leverage multiple AWS services, without having to manually configure each service.
- Local Development and Testing : AWS SAM supports local development and testing, allowing developers to test their serverless applications locally before deploying them to the cloud. This helps ensure that the application is running as expected, and helps catch any issues before they are deployed to production.
- Single, reliable and automated deployment: AWS SAM organizes and deploy related resources together as a single, versioned entity. It is an extension of AWS CloudFormation , which means that developers can leverage CloudFormation’s reliable deployment capabilities. With the SAM CLI , developers can easily deploy their applications to AWS, or create secure CI/CD pipelines that follow best practices and integrate with AWS’ native and third-party CI/CD systems.
Now Let’s practice a bit: Deploying a simple severless app on AWS
Now that you know what SAM is, let’s put it into practice by deploying a simple serverless application. We’ll walk through the steps of deploying a serverless app that displays a poem about AWS SAM 😂.
To get started, we’ll use AWS Lambda to execute our code and Amazon API Gateway to handle incoming requests. By following the steps outlined in this tutorial, you’ll see just how easy it can be to build and deploy serverless applications with SAM. So, let’s dive in!
- Installing SAM CLI
To get started, we need to install the AWS SAM CLI.
- Windows: Download and install AWS SAM CLI for windows here.
- MacOS: If you’re running on MacOS, you can get the package installer for Intel(x86_64) or forApple (arm64)
After installing, run this command to make sure it works well
sam --version
It should display SAM CLI, version
- Configuring the serverless application
Let’s create our serverless app and get it ready for deployment
- Create a new directory for the project
mkdir serverless-app
cd serverless-app
- Create the Lambda handler
Create a new file called poem.py and add the following code
import json
def handler(event, context):
poem = '''
Roses are red,
Violets are blue,
AWS Serverless,
Is the way to go, too.
No more servers to manage,
No more scaling to do,
AWS takes care of it all,
So you can focus on code.
Lambda functions and API Gateway,
DynamoDB and more,
AWS Serverless has it all,
For your app to truly soar.
So if you want to build,
An app that's fast and lean,
Choose AWS Serverless,
And you'll be a development machine.
'''
json_poem = json.dumps({"statusCode": 200, "poem": poem})
return {
"isBase64Encoded": False,
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json_poem
}
- Create the SAM template file
Resources:
PoemFunction:
Type: "AWS::Serverless::Function"
Properties:
Handler: poem.handler
Runtime: python3.8
CodeUri: .
Description: A Lambda function that returns a poem
MemorySize: 128
Timeout: 10
Events:
PoemApi:
Type: Api
Properties:
Path: /poem
Method: GET
This configuration defines an AWS Lambda function called PoemFunction and an API Gateway endpoint called PoemApi that triggers the function when accessed with a GET request to _/poem._
- Deploying the Serverless app to AWS
The moment has arrived to deploy our serverless application! Let’s get it up and running 🤹♂️
Package the app 📦
sam package --template-file template.yaml --output-template-file packaged.yaml --s3-bucket serverless-app-bucket --region us-east-1
This command packages your AWS SAM application and uploads it to an auto-created S3 bucket. It creates a new CloudFormation templat e that includes the S3 location of the packaged application.
Deploy 🚀
sam deploy --template-file packaged.yaml --stack-name serverless-app --capabilities CAPABILITY_IAM
This command deploys your AWS SAM application to AWS CloudFormation. It creates a new CloudFormation stack and deploys the packaged application to the stack.
Test 🧪
- After the deployment is complete, open the AWS Console.
- Navigate to the AWS API Gateway service and select the API that was created by AWS SAM.
- Click on the “Stages” tab and find the URL for the deployed API.
- Open a web browser and navigate to the URL. You should see the poem displayed in the browser.
Congratulations 🎉! You’ve successfully deployed a simple serverless app with AWS SAM.
I hope this tutorial has been helpful in discovering and guiding you through the process of deploying a serverless application with AWS SAM. If you encounter any errors or issues that you cannot troubleshoot on your own, don’t hesitate to leave a comment.
Top comments (0)