Getting Started with AWS SAM by deploying a simple Serverless Application
Tags: serverless, awsapigateway, awslambda, aws
Published: 2023-07-29 10:58:37

---
title: Getting Started with AWS SAM by deploying a simple Serverless Application
published: true
date: 2023-07-10 17:32:10 UTC
tags: serverless,awsapigateway,awslambda,aws
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/960/1*zR1Im5EdKu9XVF0l5KP8Tg.jpeg)


_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

![AWS SAM mascotte](https://cdn-images-1.medium.com/max/571/0*c-17cBkxcrHzQtbm.png)

- 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!

1. **Installing SAM CLI**

To get started, we need to install the **AWS SAM CLI.**

- **Windows:** Download and install AWS SAM CLI for windows [here](https://github.com/aws/aws-sam-cli/releases/latest/download/AWS_SAM_CLI_64_PY3.msi).
- **MacOS:** If you’re running on MacOS, you can get the package installer for [Intel(x86\_64)](https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-macos-x86_64.pkg) or for[Apple (arm64)](https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-macos-arm64.pkg)

After installing, run this command to make sure it works well

```
sam --version
```

It should display SAM CLI, version <version of sam cli>

2. **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._

3. **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 🧪**

1. After the deployment is complete, open the AWS Console.
2. Navigate to the **AWS API Gateway** service and select the API that was created by AWS SAM.
3. Click on the “Stages” tab and find the URL for the deployed API.
4. 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._

* * *