top of page

Download free e-books

Explore the world of Software and Data Engineering in a more efficient and accessible way with our eBooks!

  • Writer's pictureJP

How to create a serverless app with AWS SAM


AWS SAM



For this post, I will teach you how to create a serverless app with AWS SAM. AWS SAM (Serverless Application Model) is an extension of AWS CloudFormation, specifically designed for serverless application development and deployment, famous serverless like AWS Lambda, API Gateway, DynamoDB, among other AWS features.


Level of abstraction


AWS SAM is an application-level tool primarily focused on building and deploying serverless applications on AWS. It provides higher level abstractions to facilitate the development and deployment of serverless applications, with a focus on the AWS services needed to support this type of architecture, i.e. the whole focus is on AWS and not another cloud. AWS SAM has a whole way to generate the project's code locally and makes it possible to generate tests, Build and Deploy through SAM CLI.


How to install AWS SAM


Go to this link and follow the steps according to each operating system.


How to create a serverless project


After installing, through a terminal, manage your project locally by generating the necessary files to then deploy the application. First, go to the folder where you want to generate your serverless resource and then open the terminal.


Type the following command in the terminal to start the SAM:

sam init

After typing, a prompt will appear with some options for you to fill in your project information.

AWS SAM

Above we have 2 options to generate our initial template, let's type 1 to generate the option 1 - AWS Quick Start Templates.


After typing, a new list will be shown with some template options. Note that each option boils down to a resource such as Lambda, Dynamo table and even a simple API using API Gateway.


AWS SAM

For this scenario, let's create a Dynamo table, in this case, type the option 13 and press enter. After typing, some questions will be asked, just type y to proceed until a new screen about the project information is offered as below.

AWS SAM



Type the name of the project you want and press enter. In our case I typed the following name for the project dynamo-table-using-aws-sam as in the image below.

AWS SAM


After typing the project name, the template and files containing the base code will be available and ready for deployment.


Access the folder and see that a file called template.yaml has been created containing information about the resources that will be created. It's very similar to a CloudFormation template, but shorter.


Open the file and notice that several helper resources have been mapped into the template, such as Dynamo itself, a Lambda and an API Gateway. Were also created, some base codes related to Lambda and some unit tests that allow local invocations.


How to deploy


Now that our template and base code has been generated, it's time to create the Dynamo table in AWS, just follow the next steps.


Access the terminal again and type the following command:

sam deploy --guided

After executing this command, the following options will be shown in the terminal prompt for completion:

AWS SAM

For the Stack Name field, enter a value that will be the identifier of that stack which will be used by CloudFormation to create the necessary resources. When in doubt, follow what was typed as per the image above, in this case dynamo-stack.


After filling in all the fields, a summary of what will be created will be presented as shown in the image below:

AWS SAM

Finally, one more last question will be asked about the desire to deploy, just type y to confirm.

AWS SAM



After confirming the operation, the progress of creating the resources will be displayed in the terminal until the end of the process.

AWS SAM

Deploy finished, notice again the resources that were created.

AWS SAM

Now just access the AWS console and check the table created in Dynamo.


Deleting Resources


If necessary, you can delete the resources via SAM CLI, just run the command below:

sam delete dynamo-stack

The dynamo-stack argument refers to the identifier we typed earlier in the Stack Name field, remember? Use the same to delete the entire created stack.


After typing the command above, just confirm the next steps.


It's quite simple how to create a serverless resource with AWS SAM, there are some advantages and disadvantages and it all depends on your strategy.


Hope you enjoyed!

bottom of page