<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lambda Archives - Blog IT</title>
	<atom:link href="https://blogit.create.pt/tag/lambda/feed/" rel="self" type="application/rss+xml" />
	<link>https://blogit.create.pt/tag/lambda/</link>
	<description>Create IT blogger community</description>
	<lastBuildDate>Wed, 30 Oct 2019 11:24:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>Send Email In AWS Using SQS, SNS and Lambda Function</title>
		<link>https://blogit.create.pt/guilhermeperuzzi/2019/10/21/send-email-in-aws-using-sqs-sns-and-lambda-function/</link>
					<comments>https://blogit.create.pt/guilhermeperuzzi/2019/10/21/send-email-in-aws-using-sqs-sns-and-lambda-function/#respond</comments>
		
		<dc:creator><![CDATA[Guilherme Peruzzi]]></dc:creator>
		<pubDate>Mon, 21 Oct 2019 16:08:29 +0000</pubDate>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[SNS]]></category>
		<category><![CDATA[SQS]]></category>
		<guid isPermaLink="false">https://blogit.create.pt/?p=11611</guid>

					<description><![CDATA[<p>This post explains how to build an AWS infrastructure so you can send an email from AWS to a subscribed email account. We will use SQS, SNS and Lambda Function for this example. Introduction In AWS the main ways that we have to send an email are: Using Amazon Simple Email Service (SES) Using Amazon [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/guilhermeperuzzi/2019/10/21/send-email-in-aws-using-sqs-sns-and-lambda-function/">Send Email In AWS Using SQS, SNS and Lambda Function</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>This post explains how to build an AWS infrastructure so you can send an email from AWS to a subscribed email account. We will use SQS, SNS and Lambda Function for this example.</p>



<h2 class="wp-block-heading">Introduction</h2>



<p>In AWS the main ways that we have to send an email are:</p>



<ul class="wp-block-list"><li>Using Amazon Simple Email Service (SES)</li><li>Using Amazon Simple Notification Service (SNS)</li></ul>



<p>For this example we are going to use Amazon Simple Notification Service.</p>



<p>You can check the main differences between SES and SNS <a href="https://chaosgears.com/aws-ses-and-sns-your-first-notification-service/">here</a></p>



<h2 class="wp-block-heading">The components</h2>



<p>Now i&#8217;m going to explain the components that we are going to use.</p>



<h3 class="wp-block-heading">Amazon Simple Queue Service (SQS)</h3>



<p style="text-align:justify">Amazon Simple Queue Service (SQS) is a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. SQS eliminates the complexity and overhead associated with managing and operating message oriented middleware, and empowers developers to focus on differentiating work. Using SQS, you can send, store, and receive messages between software components at any volume, without losing messages or requiring other services to be available. </p>



<p>More Info: <a href="https://aws.amazon.com/sqs/">SQS</a></p>



<h3 class="wp-block-heading">Amazon Simple Notification Service (SNS)</h3>



<p style="text-align:justify">Amazon Simple Notification Service (SNS) is a highly available, durable, secure, fully managed pub/sub messaging service that enables you to decouple microservices, distributed systems, and serverless applications. Amazon SNS provides topics for high-throughput, push-based, many-to-many messaging. Using Amazon SNS topics, your publisher systems can fan out messages to a large number of subscriber endpoints for parallel processing, including Amazon SQS queues, AWS Lambda functions, and HTTP/S webhooks. Additionally, SNS can be used to fan out notifications to end users using mobile push, SMS, and email.</p>



<p>More Info: <a href="https://aws.amazon.com/sns/">SNS</a></p>



<h3 class="wp-block-heading">AWS Lambda</h3>



<p style="text-align:justify">AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume &#8211; there is no charge when your code is not running.</p>



<p style="text-align:justify">With Lambda, you can run code for virtually any type of application or backend service &#8211; all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.</p>



<p>More Info:  <a href="https://aws.amazon.com/lambda/">Lambda</a></p>



<h2 class="wp-block-heading">Objective</h2>



<p style="text-align:justify">We are going to build a system using a SQS queue where we are going to send the body of the email. The lambda that long pools the queue will be triggered when the message is sent to the queue and will publish the message to the SNS topic so we can send an email with the message body. </p>



<p>More Info:<a href="https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-short-and-long-polling.html#sqs-long-polling"> Long Pooling</a></p>



<h2 class="wp-block-heading">Lets Go</h2>



<p style="text-align:justify">Create a SQS Queue where we are going to send the email body message. This queue will be a Standard Queue. This example will not have a deadletter queue configurated, but is always good to have one.</p>



<figure class="wp-block-image"><img fetchpriority="high" decoding="async" width="1892" height="690" src="https://i1.wp.com/blogit.create.pt/wp-content/uploads/2019/10/image.png?fit=696%2C254&amp;ssl=1" alt="queue" class="wp-image-11623" srcset="https://blogit.create.pt/wp-content/uploads/2019/10/image.png 1892w, https://blogit.create.pt/wp-content/uploads/2019/10/image-300x109.png 300w, https://blogit.create.pt/wp-content/uploads/2019/10/image-768x280.png 768w, https://blogit.create.pt/wp-content/uploads/2019/10/image-1024x373.png 1024w, https://blogit.create.pt/wp-content/uploads/2019/10/image-696x254.png 696w, https://blogit.create.pt/wp-content/uploads/2019/10/image-1068x389.png 1068w, https://blogit.create.pt/wp-content/uploads/2019/10/image-1152x420.png 1152w" sizes="(max-width: 1892px) 100vw, 1892px" /></figure>



<p>We need to create a SNS Topic. An Amazon SNS topic is a logical access point which acts as a communication channel.</p>



<figure class="wp-block-image"><img decoding="async" width="1853" height="676" src="https://i0.wp.com/blogit.create.pt/wp-content/uploads/2019/10/image-1.png?fit=696%2C254&amp;ssl=1" alt="topic" class="wp-image-11624" srcset="https://blogit.create.pt/wp-content/uploads/2019/10/image-1.png 1853w, https://blogit.create.pt/wp-content/uploads/2019/10/image-1-300x109.png 300w, https://blogit.create.pt/wp-content/uploads/2019/10/image-1-768x280.png 768w, https://blogit.create.pt/wp-content/uploads/2019/10/image-1-1024x374.png 1024w, https://blogit.create.pt/wp-content/uploads/2019/10/image-1-696x254.png 696w, https://blogit.create.pt/wp-content/uploads/2019/10/image-1-1068x390.png 1068w, https://blogit.create.pt/wp-content/uploads/2019/10/image-1-1151x420.png 1151w" sizes="(max-width: 1853px) 100vw, 1853px" /></figure>



<figure class="wp-block-image"><img decoding="async" width="1842" height="665" src="https://i2.wp.com/blogit.create.pt/wp-content/uploads/2019/10/image-2.png?fit=696%2C251&amp;ssl=1" alt="topic" class="wp-image-11625" srcset="https://blogit.create.pt/wp-content/uploads/2019/10/image-2.png 1842w, https://blogit.create.pt/wp-content/uploads/2019/10/image-2-300x108.png 300w, https://blogit.create.pt/wp-content/uploads/2019/10/image-2-768x277.png 768w, https://blogit.create.pt/wp-content/uploads/2019/10/image-2-1024x370.png 1024w, https://blogit.create.pt/wp-content/uploads/2019/10/image-2-696x251.png 696w, https://blogit.create.pt/wp-content/uploads/2019/10/image-2-1068x386.png 1068w, https://blogit.create.pt/wp-content/uploads/2019/10/image-2-1163x420.png 1163w" sizes="(max-width: 1842px) 100vw, 1842px" /></figure>



<p style="text-align:justify">Create a subscription to that topic. The subscription resource subscribes an endpoint to an Amazon Simple Notification Service (Amazon SNS) topic. For a subscription to be created, the owner of the endpoint must confirm the subscription. The protocol of the subscription will be Email. The endpoint will be the email address that will receive the email. For this example i used a temporary mail so that i didn&#8217;t fill my personal inbox with emails. </p>



<p>More Info: <a href="https://temp-mail.org/en/">Temp-Mail</a></p>



<figure class="wp-block-image"><img decoding="async" width="1834" height="747" src="https://i0.wp.com/blogit.create.pt/wp-content/uploads/2019/10/image-3.png?fit=696%2C283&amp;ssl=1" alt="subscription" class="wp-image-11626" srcset="https://blogit.create.pt/wp-content/uploads/2019/10/image-3.png 1834w, https://blogit.create.pt/wp-content/uploads/2019/10/image-3-300x122.png 300w, https://blogit.create.pt/wp-content/uploads/2019/10/image-3-768x313.png 768w, https://blogit.create.pt/wp-content/uploads/2019/10/image-3-1024x417.png 1024w, https://blogit.create.pt/wp-content/uploads/2019/10/image-3-696x283.png 696w, https://blogit.create.pt/wp-content/uploads/2019/10/image-3-1068x435.png 1068w, https://blogit.create.pt/wp-content/uploads/2019/10/image-3-1031x420.png 1031w" sizes="(max-width: 1834px) 100vw, 1834px" /></figure>



<p style="text-align:justify">After you configure the subscription you will receive an email with a link to confirm the subscription.</p>



<figure class="wp-block-image"><img decoding="async" width="1127" height="734" src="https://i1.wp.com/blogit.create.pt/wp-content/uploads/2019/10/image-4.png?fit=696%2C453&amp;ssl=1" alt="email" class="wp-image-11627" srcset="https://blogit.create.pt/wp-content/uploads/2019/10/image-4.png 1127w, https://blogit.create.pt/wp-content/uploads/2019/10/image-4-300x195.png 300w, https://blogit.create.pt/wp-content/uploads/2019/10/image-4-768x500.png 768w, https://blogit.create.pt/wp-content/uploads/2019/10/image-4-1024x667.png 1024w, https://blogit.create.pt/wp-content/uploads/2019/10/image-4-696x453.png 696w, https://blogit.create.pt/wp-content/uploads/2019/10/image-4-1068x696.png 1068w, https://blogit.create.pt/wp-content/uploads/2019/10/image-4-645x420.png 645w" sizes="(max-width: 1127px) 100vw, 1127px" /></figure>



<p>When you click on the link you will be redirected to the subscription confirmation page</p>



<figure class="wp-block-image"><img decoding="async" width="844" height="442" src="https://blogit.create.pt////wp-content/uploads/2019/10/image-5.png" alt="confirmed" class="wp-image-11628" srcset="https://blogit.create.pt/wp-content/uploads/2019/10/image-5.png 844w, https://blogit.create.pt/wp-content/uploads/2019/10/image-5-300x157.png 300w, https://blogit.create.pt/wp-content/uploads/2019/10/image-5-768x402.png 768w, https://blogit.create.pt/wp-content/uploads/2019/10/image-5-696x364.png 696w, https://blogit.create.pt/wp-content/uploads/2019/10/image-5-802x420.png 802w" sizes="(max-width: 844px) 100vw, 844px" /></figure>



<p style="text-align:justify">Create the lambda that will use long pooling to check if there are new messages in the queue and publish the messages to the SNS Topic.</p>



<p style="text-align:justify">In this example i used Python to develop the lambda and called it &#8220;EmailLambda&#8221;. Note: you need to set the role of the lambda so that the lambda has permissions to long pool the sqs queue and publish a message to the SNS topic.</p>



<p>More Info: <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/list_amazonsns.html">SNS IAM Roles</a>  <a href="https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-using-identity-based-policies.html">SQS IAM Roles</a> </p>



<figure class="wp-block-image"><img decoding="async" width="1849" height="870" src="https://i1.wp.com/blogit.create.pt/wp-content/uploads/2019/10/image-6.png?fit=696%2C328&amp;ssl=1" alt="lambda" class="wp-image-11629" srcset="https://blogit.create.pt/wp-content/uploads/2019/10/image-6.png 1849w, https://blogit.create.pt/wp-content/uploads/2019/10/image-6-300x141.png 300w, https://blogit.create.pt/wp-content/uploads/2019/10/image-6-768x361.png 768w, https://blogit.create.pt/wp-content/uploads/2019/10/image-6-1024x482.png 1024w, https://blogit.create.pt/wp-content/uploads/2019/10/image-6-696x327.png 696w, https://blogit.create.pt/wp-content/uploads/2019/10/image-6-1068x503.png 1068w, https://blogit.create.pt/wp-content/uploads/2019/10/image-6-893x420.png 893w" sizes="(max-width: 1849px) 100vw, 1849px" /></figure>



<p>Here is the lambda code that i used.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
#!/usr/bin/python3
import json
import boto3
import os

def lambda_handler(event, context):
    batch_processes=&#x5B;]
    for record in event&#x5B;&#039;Records&#039;]:
        send_request(record&#x5B;&quot;body&quot;])
		

def send_request(body):
    # Create an SNS client
    sns = boto3.client(&#039;sns&#039;)

    # Publish a simple message to the specified SNS topic
    response = sns.publish(
        TopicArn=os.environ&#x5B;&#039;email_topic&#039;],    
        Message=body,    
    )

    # Print out the response
    print(response)
 
</pre></div>


<p>Set the lambda enviroment variable email_topic to the ARN of the SNS Topic we created earlier.</p>



<figure class="wp-block-image"><img decoding="async" width="1687" height="409" src="https://i1.wp.com/blogit.create.pt/wp-content/uploads/2019/10/image-7.png?fit=696%2C169&amp;ssl=1" alt="topic" class="wp-image-11631" srcset="https://blogit.create.pt/wp-content/uploads/2019/10/image-7.png 1687w, https://blogit.create.pt/wp-content/uploads/2019/10/image-7-300x73.png 300w, https://blogit.create.pt/wp-content/uploads/2019/10/image-7-768x186.png 768w, https://blogit.create.pt/wp-content/uploads/2019/10/image-7-1024x248.png 1024w, https://blogit.create.pt/wp-content/uploads/2019/10/image-7-696x169.png 696w, https://blogit.create.pt/wp-content/uploads/2019/10/image-7-1068x259.png 1068w" sizes="(max-width: 1687px) 100vw, 1687px" /></figure>



<p>Add the lambda trigger and set it with the sqs queue that will receive the body of the email.</p>



<figure class="wp-block-image"><img decoding="async" width="1859" height="875" src="https://i1.wp.com/blogit.create.pt/wp-content/uploads/2019/10/image-8.png?fit=696%2C328&amp;ssl=1" alt="trigger" class="wp-image-11632" srcset="https://blogit.create.pt/wp-content/uploads/2019/10/image-8.png 1859w, https://blogit.create.pt/wp-content/uploads/2019/10/image-8-300x141.png 300w, https://blogit.create.pt/wp-content/uploads/2019/10/image-8-768x361.png 768w, https://blogit.create.pt/wp-content/uploads/2019/10/image-8-1024x482.png 1024w, https://blogit.create.pt/wp-content/uploads/2019/10/image-8-696x328.png 696w, https://blogit.create.pt/wp-content/uploads/2019/10/image-8-1068x503.png 1068w, https://blogit.create.pt/wp-content/uploads/2019/10/image-8-892x420.png 892w" sizes="(max-width: 1859px) 100vw, 1859px" /></figure>



<h3 class="wp-block-heading">Lets Test it !!!</h3>



<p>Now the only thing that we need to do is send a new message to the SQS queue with the email body that we want.</p>



<figure class="wp-block-image"><img decoding="async" width="1917" height="875" src="https://i0.wp.com/blogit.create.pt/wp-content/uploads/2019/10/image-9.png?fit=696%2C317&amp;ssl=1" alt="send message" class="wp-image-11633" srcset="https://blogit.create.pt/wp-content/uploads/2019/10/image-9.png 1917w, https://blogit.create.pt/wp-content/uploads/2019/10/image-9-300x137.png 300w, https://blogit.create.pt/wp-content/uploads/2019/10/image-9-768x351.png 768w, https://blogit.create.pt/wp-content/uploads/2019/10/image-9-1024x467.png 1024w, https://blogit.create.pt/wp-content/uploads/2019/10/image-9-696x318.png 696w, https://blogit.create.pt/wp-content/uploads/2019/10/image-9-1068x487.png 1068w, https://blogit.create.pt/wp-content/uploads/2019/10/image-9-920x420.png 920w" sizes="(max-width: 1917px) 100vw, 1917px" /></figure>



<figure class="wp-block-image"><img decoding="async" width="980" height="898" src="https://blogit.create.pt////wp-content/uploads/2019/10/image-10.png" alt="Send message" class="wp-image-11634" srcset="https://blogit.create.pt/wp-content/uploads/2019/10/image-10.png 980w, https://blogit.create.pt/wp-content/uploads/2019/10/image-10-300x275.png 300w, https://blogit.create.pt/wp-content/uploads/2019/10/image-10-768x704.png 768w, https://blogit.create.pt/wp-content/uploads/2019/10/image-10-696x638.png 696w, https://blogit.create.pt/wp-content/uploads/2019/10/image-10-458x420.png 458w" sizes="(max-width: 980px) 100vw, 980px" /></figure>



<p>Now we will receive an email from  no-reply@sns.amazonaws.com with the body of the queue message.</p>



<figure class="wp-block-image"><img decoding="async" width="1101" height="407" src="https://i0.wp.com/blogit.create.pt/wp-content/uploads/2019/10/image-11.png?fit=696%2C258&amp;ssl=1" alt="Email" class="wp-image-11635" srcset="https://blogit.create.pt/wp-content/uploads/2019/10/image-11.png 1101w, https://blogit.create.pt/wp-content/uploads/2019/10/image-11-300x111.png 300w, https://blogit.create.pt/wp-content/uploads/2019/10/image-11-768x284.png 768w, https://blogit.create.pt/wp-content/uploads/2019/10/image-11-1024x379.png 1024w, https://blogit.create.pt/wp-content/uploads/2019/10/image-11-696x257.png 696w, https://blogit.create.pt/wp-content/uploads/2019/10/image-11-1068x395.png 1068w" sizes="(max-width: 1101px) 100vw, 1101px" /></figure>



<p>And there you have it. An email notification in AWS using SQS, SNS and Lambda.</p>



<p>Thanks for reading this post and have an&#8230;</p>



<figure class="wp-block-image"><img decoding="async" src="https://d2cnjxvu6pstmv.cloudfront.net/2016/03/14165712/awesomeday1.jpg" alt="Image result for aws awesome day" /></figure>



<p></p>
<p>The post <a href="https://blogit.create.pt/guilhermeperuzzi/2019/10/21/send-email-in-aws-using-sqs-sns-and-lambda-function/">Send Email In AWS Using SQS, SNS and Lambda Function</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/guilhermeperuzzi/2019/10/21/send-email-in-aws-using-sqs-sns-and-lambda-function/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
