<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Guido Luz Percú</title>
    <description>The latest articles on DEV Community by Guido Luz Percú (@oumguido).</description>
    <link>https://dev.to/oumguido</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F462505%2F9538fc6c-287a-46ec-be11-5fb35fa39ed7.jpg</url>
      <title>DEV Community: Guido Luz Percú</title>
      <link>https://dev.to/oumguido</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oumguido"/>
    <language>en</language>
    <item>
      <title>Build a Serverless Python API using AWS SAM</title>
      <dc:creator>Guido Luz Percú</dc:creator>
      <pubDate>Wed, 02 Sep 2020 23:21:31 +0000</pubDate>
      <link>https://dev.to/oumguido/build-a-serverless-python-api-using-aws-sam-177b</link>
      <guid>https://dev.to/oumguido/build-a-serverless-python-api-using-aws-sam-177b</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;As it’s used today, &lt;strong&gt;serverless computing&lt;/strong&gt; means that the service provider manages the computing resources (e.g. keeps the firmware up to date)  and the resources scale up and down automagically (horizontally and/or  vertically to meet demand). No more; no less.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://moduscreate.com/blog/serverless-allthethings-1/"&gt;Modus Create - Serverless All the things&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Serverless architecture is becoming more and more popular: Amazon launched &lt;a href="https://aws.amazon.com/lambda/"&gt;AWS Lambda&lt;/a&gt;, Microsoft has &lt;br&gt;
&lt;a href="https://azure.microsoft.com/en-us/services/functions/"&gt;Azure Functions&lt;/a&gt; and Google has &lt;a href="https://cloud.google.com/functions/"&gt;Cloud Functions&lt;/a&gt;. Now not only can you stop worrying about manually &lt;br&gt;
provisioning, managing and upgrading servers, but also pay only for what you use.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why you don't need to worry about managing a server
&lt;/h2&gt;

&lt;p&gt;You may be already familiar with traditional cloud usage model: provisioning servers, deploying code to the cloud and manage resources usage and scaling. This is still the right thing to do in most scenarios, but there's a new way to do it.&lt;/p&gt;

&lt;p&gt;For example, if you have an application that allow users to upload profile pictures and you need to resize those images and store them on Amazon S3, you can handle this by using a AWS Lambda.&lt;/p&gt;

&lt;p&gt;Using a serverless architecture can also help reducing costs, since with Lambda you only pay for execution costs.&lt;/p&gt;

&lt;p&gt;The AWS Lambda free usage tier includes 1M free request per month and 400.000 GB-seconds of compute time per month.&lt;/p&gt;
&lt;h2&gt;
  
  
  SAM - Serverless Application Model
&lt;/h2&gt;

&lt;p&gt;SAM is an open-source framework for building serverless applications. It provides syntax to express APIs, databases, functions,  and event source mappings. Writing a few lines of YAML, you can create IAM roles, define resources and create your application. &lt;/p&gt;
&lt;h3&gt;
  
  
  Instalation
&lt;/h3&gt;

&lt;p&gt;To learn how to install &lt;code&gt;sam cli&lt;/code&gt;, check the &lt;a href="https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html"&gt;official docs&lt;/a&gt; (there are different processes for Windows and Linux systems).&lt;/p&gt;

&lt;p&gt;And you should also have &lt;code&gt;aws cli&lt;/code&gt; installed to configure your credentials so &lt;code&gt;sam cli&lt;/code&gt; knows about it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install awscli --upgrade --user&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws configure&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To get your AWS (Access/Secret) Keys go to your &lt;a href="https://console.aws.amazon.com/iam/home?#/security_credentials"&gt;AWS account &amp;gt; My Security Credentials&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Using SAM command line interface
&lt;/h3&gt;

&lt;p&gt;You can create a new project using the command &lt;code&gt;sam init&lt;/code&gt; and following its instructions. Here I'm creating a Python 3.8 Lambda function.&lt;/p&gt;
&lt;h4&gt;
  
  
  SAM Template
&lt;/h4&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;AWSTemplateFormatVersion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;2010-09-09'&lt;/span&gt;
&lt;span class="na"&gt;Transform&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AWS::Serverless-2016-10-31&lt;/span&gt;
&lt;span class="na"&gt;Description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="s"&gt;aws-sam-opencv&lt;/span&gt;

  &lt;span class="s"&gt;Sample SAM Template for aws-sam-opencv&lt;/span&gt;

&lt;span class="na"&gt;Globals&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;Function&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;Timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;

&lt;span class="na"&gt;Resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;HelloWorldFunction&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AWS::Serverless::Function&lt;/span&gt;
    &lt;span class="na"&gt;Properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;CodeUri&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;hello_world/&lt;/span&gt;
      &lt;span class="na"&gt;Handler&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;app.lambda_handler&lt;/span&gt;
      &lt;span class="na"&gt;Runtime&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python3.8&lt;/span&gt;
      &lt;span class="na"&gt;Events&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;HelloWorld&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;Properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="na"&gt;Path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/hello&lt;/span&gt;
            &lt;span class="na"&gt;Method&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;get&lt;/span&gt;

&lt;span class="na"&gt;Outputs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;HelloWorldApi&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;Description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;API&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;Gateway&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;endpoint&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;URL&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;for&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;Prod&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;stage&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;for&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;World&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;function"&lt;/span&gt;
    &lt;span class="na"&gt;Value&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;!Sub&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"&lt;/span&gt;
  &lt;span class="na"&gt;HelloWorldFunction&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;Description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;World&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;Lambda&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;Function&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;ARN"&lt;/span&gt;
    &lt;span class="na"&gt;Value&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;!GetAtt&lt;/span&gt; &lt;span class="s"&gt;HelloWorldFunction.Arn&lt;/span&gt;
  &lt;span class="na"&gt;HelloWorldFunctionIamRole&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;Description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Implicit&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;IAM&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;Role&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;created&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;for&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;World&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;function"&lt;/span&gt;
    &lt;span class="na"&gt;Value&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;!GetAtt&lt;/span&gt; &lt;span class="s"&gt;HelloWorldFunctionRole.Arn&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This should be your SAM &lt;code&gt;template.yaml&lt;/code&gt; file if you're following the &lt;code&gt;sam init&lt;/code&gt; process. It's a bit confusing and there's a lot of room for error (extra attention to indentation).&lt;/p&gt;

&lt;p&gt;So it will look in your  &lt;code&gt;hello_world&lt;/code&gt; folder for &lt;code&gt;app.py&lt;/code&gt;file with the &lt;code&gt;lambda_handler&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lambda_handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="s"&gt;"""Example Function and Handler"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;'statusCode'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;'body'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Hello from Lambda!'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Validate the template file
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;sam validate&lt;/code&gt; will take a look at your &lt;code&gt;template.yaml&lt;/code&gt; file and see if it looks correct. This command is very useful because it checks for removed references and wrong indentation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Build and deploy
&lt;/h3&gt;

&lt;p&gt;To deploy, let's first build it using &lt;code&gt;sam build --use-container&lt;/code&gt; This will build the source of your application and prepare it to run locally (with &lt;code&gt;sam local invoke&lt;/code&gt;) or to ship it to AWS. &lt;/p&gt;

&lt;p&gt;Then it's time to package and deploy your application to AWS, with a series of prompts:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sam deploy --guided&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Stack Name&lt;/strong&gt;: The name of the stack to deploy to CloudFormation. This should be your project name.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AWS Region&lt;/strong&gt;: The AWS region you want to deploy your app to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Confirm changes before deploy&lt;/strong&gt;: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Allow SAM CLI IAM role creation&lt;/strong&gt;: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Save arguments to samconfig.toml&lt;/strong&gt;: In the future you can just re-run &lt;code&gt;sam deploy&lt;/code&gt; without parameters to deploy changes to your application, because your choices will be saved in a &lt;code&gt;samconfig.toml&lt;/code&gt; file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At the end of your process, you should see a URL with the production environment of your API. If all went well, you can just access it to get your Hello World using SAM.&lt;/p&gt;

&lt;p&gt;Have fun!&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Articles:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://medium.com/@edjgeek/meet-aws-sam-cli-sam-init-bab68b4cc0d4"&gt;Meet AWS SAM CLI: sam init&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackify.com/aws-lambda-with-python-a-complete-getting-started-guide/"&gt;AWS Lambda with Python: A complete getting started guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/getting-started/projects/build-serverless-web-app-lambda-apigateway-s3-dynamodb-cognito/"&gt;Build a Serverless Web Application
with AWS Lambda, Amazon API Gateway, Amazon S3, Amazon DynamoDB, and Amazon Cognito&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/blogs/aws/aws-serverless-application-model-sam-command-line-interface-build-test-and-debug-serverless-apps-locally/"&gt;AWS Serverless Application Model SAM Command Line interface build, test and debug&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.guidopercu.dev/blog/serverless-on-amazon-aws/"&gt;Serverless on Amazon AWS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.guidopercu.dev/blog/dynamodb-on-aws-sam/"&gt;DynamoDB on AWS SAM&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Videos:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=CIdUU6rNdk4"&gt;Deep Dive into AWS SAM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://egghead.io/playlists/learn-aws-lambda-from-scratch-d29d"&gt;Learn AWS Lambda from scratch&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=DA0MtJkUVaY"&gt;Start Right Using AWS SAM: Treat Your Infrastructure the Way You Treat Your Code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pages.awscloud.com/Managing-Serverless-Applications-with-SAM-Templates_2019_1020-SRV_OD.html"&gt;Managing Serverless Applications with SAM Templates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://egghead.io/playlists/learn-aws-serverless-application-model-aws-sam-framework-from-scratch-baf9"&gt;Build serverless applications with AWS Serverless Application Model (AWS SAM)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Code example:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/aws-samples/cookiecutter-aws-sam-s3-rekognition-dynamodb-python"&gt;Cookiecutter AWS SAM S3 Rekognition DynamoDB Python&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/aws-samples/serverless-app-examples/tree/master/python/hello-world-python3"&gt;Hello World Python 3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/marekq/serverless-cognito"&gt;Serverless Cognito&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/awslabs/serverless-application-model/tree/master"&gt;SAM Serverless Application Framework&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
