<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by hpal on Medium]]></title>
        <description><![CDATA[Stories by hpal on Medium]]></description>
        <link>https://medium.com/@hpal?source=rss-8e8ee79b3dd0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by hpal on Medium</title>
            <link>https://medium.com/@hpal?source=rss-8e8ee79b3dd0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 09:40:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hpal/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Wade into serverless with AWS Chalice and Lambda, Part 1]]></title>
            <link>https://hpal.medium.com/wade-into-serverless-with-aws-chalice-and-lambda-part-1-9cda06864c7f?source=rss-8e8ee79b3dd0------2</link>
            <guid isPermaLink="false">https://medium.com/p/9cda06864c7f</guid>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[aws-chalice]]></category>
            <category><![CDATA[serverless-framework]]></category>
            <category><![CDATA[aws-sqs]]></category>
            <category><![CDATA[aws-sns]]></category>
            <dc:creator><![CDATA[hpal]]></dc:creator>
            <pubDate>Wed, 14 Oct 2020 16:39:29 GMT</pubDate>
            <atom:updated>2020-10-14T16:39:29.617Z</atom:updated>
            <content:encoded><![CDATA[<p>Build an email sending API with background processing using AWS SNS</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UcXP2Eo8QpBLtGQjVK1Mmw.png" /></figure><p>Being a scripting ninja, DevOps king, a puppet expert or a Kubernetes GDE is all good and has its place. However, sometimes you want to quickly get an app running without having to think of a server, a container orchestrator, or infrastructure provisioning.</p><p>Enter serverless.</p><blockquote>A way to build and run applications and services without having to manage infrastructure. <a href="https://aws.amazon.com/lambda/serverless-architectures-learn-more/">AWS</a></blockquote><p><strong>By the end of this tutorial, we’ll have done the following</strong>:</p><ul><li>Describe a serverless architecture</li><li>Delve into Chalice, a Python serverless framework</li><li>Build an email sending serverless app using SNS for background processing</li><li>Deploy to Amazon Web Services (AWS) Lambda</li></ul><blockquote>Note: This is not an article on architecture or what is the best way to skin this cat; rather a quick dive into serverless. Maybe I will use SQS instead of SNS and write one function with both the API and trigger processor</blockquote><h3>Prerequisites</h3><ol><li>Python</li></ol><p>Since this is a python framework, I will assume that you, my reader have installed python and set up a virtual environment. Then do pip install chalice to get ready for the next step or quickly do so:</p><pre>$ python3.7 -m venv venv<br>$ source env/bin/activate<br>(venv)$ pip install chalice</pre><p><a href="https://chalice.readthedocs.io/en/latest/">2. AWS Chalice</a></p><p>AWS Chalice allows you to quickly(<em>really quickly</em>) create and deploy applications that use Amazon API Gateway and AWS Lambda. It can be used to:</p><ul><li>Create rest APIs</li><li>Periodic tasks</li><li>Respond to triggers from AWS resources like SQS, S3 and others e.g. invoke this function whenever a message is uploaded to ‘myqueue’</li></ul><p>Before we begin, you will need to have an AWS account. If you do, then you should proceed to create an <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-service.html">AWS IAM role</a> that Chalice will use to autogenerate all the resources it needs while deploying your app.</p><p>Chalice expects to pick the AWS credentials from <strong><em>~/.aws/config </em></strong>so once you’ve created the IAM role, add the access key and secret in that file.</p><p>If the file doesn&#39;t already exist, create it</p><p>mkdir ~/.aws<br>touch ~/.aws/config</p><p>Then add the contents of the config file</p><p>[default]<br>aws_access_key_id = YOUR_ACCESS_KEY_HERE<br>aws_secret_access_key = YOUR_SECRET_ACCESS_KEY<br>region = YOUR_REGION</p><p>So then we are ready to proceed.</p><p>3. AWS SNS (Amazon Simple Notification Service)</p><p>Fully managed pub/sub messaging for microservices, distributed systems, and serverless applications</p><p>4. AWS Lambda</p><h3>Project Structure</h3><p>Our project will consist of two apps:</p><ul><li>An API written in chalice to receive POST requests for new emails and logging them in an AWS SNS topic.</li><li>A python lambda function for listening to the SNS topic trigger and processing all logged emails.</li></ul><h4>Build the API</h4><p>Now that we have chalice installed, let’s create a new app</p><pre>(env)$ chalice new-project</pre><p>Build and run the app locally</p><pre>(env)$ chalice local<br>Serving on 127.0.0.1:8000</pre><p>Using curl, we can test the API</p><pre>curl -X GET http://localhost:8000/<br>{&quot;hello&quot;: &quot;world&quot;}</pre><p>Good. since we’ve seen how the basic app runs, let’s proceed to add a function along with a route in the app.pyto create emails and add them to an SNS topic.</p><pre>import json<br>import boto3</pre><pre>@app.route(&#39;/emails&#39;, methods=[&#39;POST&#39;])<br>def create_email():<br>    email_as_json = app.current_request.json_body<br>       <br>    sns = boto3.client(&#39;sns&#39;)            <br>    result = sns.publish(<br>        TopicArn=os.environ.get(&#39;SNS_TOPIC&#39;),<br>        Subject=email_as_json[&#39;subject&#39;],<br>        Message=json.dumps(email_as_json))</pre><pre>    return {&#39;status&#39;: 200,&#39;msg&#39;: &#39;Message queued&#39;}</pre><p>Then run the app locally and test with a request</p><pre>$ curl -H &quot;Content-Type: application/json&quot; -X POST -d &#39;{&quot;sender&quot;: &quot;sender@mail.com&quot;,&quot;receiver&quot;:&quot;yourmail@gmail.com&quot;,&quot;subject&quot;:&quot;test&quot;,&quot;message&quot;:&quot;serverless with lambda and chalice&quot;}&#39; <a href="http://localhost:8000/emails/">http://localhost:8000/emails/</a></pre><h4>Deploy the API</h4><p>Using the deploy command, we can simply use Chalice to deploy and create a Lambda function that can be accessible via a REST API.</p><pre>(env)$ chalice deploy<br>Creating deployment package.<br>Creating IAM role: mailer-dev<br>Creating lambda function: mailer-dev<br>Creating Rest API<br>Resources deployed:<br>  - Lambda ARN: arn:aws:lambda:us-west-2:995516939866:function:mailer-dev<br>  - Rest API URL: <a href="https://pcscihtza3.execute-api.us-west-2.amazonaws.com/api/">https://pcscihtza3.execute-api.us-west-2.amazonaws.com/api/</a></pre><blockquote><strong>Note</strong>: The generated ARN and API URL in the above snippet will vary from user to user.</blockquote><p>If you try to POST a request on <a href="https://pcscihtza3.execute-api.us-west-2.amazonaws.com/api/"><em>https://pcscihtza3.execute-api.us-west-2.amazonaws.com/api/emails</em></a>, it will fail with 500 Internal Server Error since we have not yet added the SNS topic. So let’s proceed to create a one using the AWS console.</p><p>When the form is brought up, add a topic name and create it, copy the ARN of the topic</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ExYYDMAimPwG7PiNK55Wjg.png" /></figure><p>Replace the SNS_TOPIC word in the code with the <a href="https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html">ARN</a> which is a unique identifier of the topic on all of AWS</p><pre>TopicArn=&#39;arn:aws:sns:us-east-1:90551693986:testapp&#39;,</pre><blockquote>Note: Your ARN will be different from the one above but will be in the format</blockquote><blockquote>arn:<em>partition</em>:<em>service</em>:<em>region</em>:<em>account-id</em>:<em>resource</em></blockquote><p>After adding the topic name, make a POST request again and it should be successful this time.</p><h4>Part 2: Build the trigger processor</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9cda06864c7f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>