<?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 Sébastien ☁ Stormacq on Medium]]></title>
        <description><![CDATA[Stories by Sébastien ☁ Stormacq on Medium]]></description>
        <link>https://medium.com/@sebsto?source=rss-198b44d70be4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*KXFWL9H2VSbcwmzi_CDpQg.png</url>
            <title>Stories by Sébastien ☁ Stormacq on Medium</title>
            <link>https://medium.com/@sebsto?source=rss-198b44d70be4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 03:44:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sebsto/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[How to Set up Amazon API Gateway as a Proxy to Debug Account Linking]]></title>
            <link>https://medium.com/@sebsto/how-to-set-up-amazon-api-gateway-as-a-proxy-to-debug-account-linking-ba4b457d82d9?source=rss-198b44d70be4------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba4b457d82d9</guid>
            <category><![CDATA[oauth]]></category>
            <category><![CDATA[api-gateway]]></category>
            <category><![CDATA[account-linking]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[alexa]]></category>
            <dc:creator><![CDATA[Sébastien ☁ Stormacq]]></dc:creator>
            <pubDate>Sun, 20 Jan 2019 13:51:01 GMT</pubDate>
            <atom:updated>2019-01-20T13:51:01.472Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you ever developed an Alexa skill that uses account linking? Do you remember the first time you tried to click on the “Link Account” button and feared for the result? I bet you first saw the dreadful error message: “Unable to Link your skill”. Sometimes trying to figure out what an error is, is like searching for a needle in a haystack. You have no clue.</p><p>Most of the errors that I have seen when working with developers, fall in two categories:</p><ul><li>Error of configuration inside the Alexa developer console. These are the easy ones to catch. We just need to compare the configuration with a working one, such as the one <a href="https://developer.amazon.com/public/community/post/Tx3CX1ETRZZ2NPC/Alexa-Account-Linking-5-Steps-to-Seamlessly-Link-Your-Alexa-Skill-with-Login-wit">described in this blog post</a>.</li><li>Errors at the OAuth Server level. These most often happen when you are developing your own OAuth server and it is not fully compliant with OAuth 2.0 specifications.</li></ul><p>When you have access to the OAuth server logs, debugging the error message you see in the Alexa App is relatively easy. You just enable full HTTP trace on the server side and search for the error or the misconfiguration on the server. Full HTTP trace includes the full HTTP headers, query string and body passed by the Alexa service to your server.</p><p>With a bit of experience, catching an OAuth error in HTTP stack trace takes only a few minutes.</p><p>The problem is that most developers we are working with, have no access to the OAuth servers or the server logs. Either they are using a third party OAuth server (<a href="http://login.amazon.com/">Login With Amazon</a>, <a href="https://developers.facebook.com/docs/facebook-login">Login With Facebook</a>, <a href="https://developers.google.com/identity/sign-in/web/sign-in">Login with Google</a> and the likes), or they are working in a large enterprises where another team is operating the OAuth server. Meeting that team and asking them to change logging level or to request access to the logs can take weeks, or may not be possible at all.</p><p>This article explains how to setup an HTTP proxy between Alexa Skill Service and your OAuth server to capture all HTTP traffic and log it. By analyzing the logs, you can inspect the HTTP URLs, query strings, headers and full bodies exchanged. Setting such a proxy requires infrastructure to host the proxy: a networked server, with a runtime to deploy your code etc … this is unnecessary heavy lifting where Amazon Web Services can help.</p><p>We will use <a href="https://aws.amazon.com/api-gateway/">Amazon API Gateway</a> instead and will configure it to act as an HTTP Proxy, sitting between Amazon’s Alexa Skill Service and your OAuth server.</p><p>Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. With a few clicks in the <a href="https://console.aws.amazon.com/console/home?region=us-east-1">AWS Management Console</a>, you can create an API that acts as a “front door” for applications to access data, business logic, or functionality from your back-end services.</p><p><a href="http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy.html">API Gateway HTTP Proxy Integration mode</a> is a new feature of API Gateway that was launched on Sept. 20th2016. You can read <a href="https://aws.amazon.com/blogs/aws/api-gateway-update-new-features-simplify-api-development/">the post by AWS Director of Evangelism, Jeff Bar’s</a>, if you want to learn more about this.</p><p>The diagram below shows where API Gateway, with HTTP Proxy Integration, fits in the OAuth Architecture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/0*eqJhe8d1d22n4mTZ.jpg" /></figure><h3>High level steps to create such a configuration are:</h3><ol><li>Create an API Gateway API</li><li>Configure the API Gateway to work in HTTP Proxy Integration mode. In that mode, the API Gateway passes the entire requests and responses between the front end and your OAuth server.</li><li>Configure Account Linking Authentication URL and Access Token URL in the <a href="https://developer.amazon.com/edw/home.html%22%20%5Cl%20%22/skills/list">Alexa developer console</a> to point to the API Gateway</li><li>Trigger an account linking flow from your Alexa app</li><li>Use <a href="https://console.aws.amazon.com/cloudwatch/home?region=us-east-1%22%20%5Cl%20%22logs:">Cloudwatch Logs</a> to see the full HTTP trace of the traffic exchanged between Alexa and your OAuth server.</li></ol><p>If you are new to AWS, setting up and configuring an API Gateway might be intimidating. This is why we created a CloudFormation template for you to get you started in minutes.</p><p><a href="https://aws.amazon.com/cloudformation/">CloudFormation</a> is an AWS service that enables you to describe your AWS resources as a JSON file, these JSON files can later be ‘executed’ to tear up and tear down your AWS environments. This gives us a number of benefits, including version control and repeatability. You can read more about AWS CloudFormation in general over in the AWS developer docs <a href="http://aws.amazon.com/cloudformation">here</a>.</p><p>You can download the CloudFormation templates from this link. Chose the template matching the region where you will execute it, either us-east-1 (Northern Virginia) or eu-west-1 (Ireland)</p><p><a href="https://alexademo.ninja/httpproxy/cloudformation-us-east-1.json">https://alexademo.ninja/httpproxy/cloudformation-us-east-1.json</a></p><p><a href="https://alexademo.ninja/httpproxy/cloudformation-eu-west-1.json">https://alexademo.ninja/httpproxy/cloudformation-eu-west-1.json</a></p><p>When executing the template, it will prompt you for the OAuth Authentication URL and the Access Token URL and then create an API Gateway and configure it as in HTTP Proxy Integration mode for you. It will then output two URLs, one for authentication and the other for the access token. You can use these URLs in the Alexa Developer console to configure account linking on your skill.</p><p>Warning : this template has been optimized to run from the Northern Virginia or Ireland regions (us-east-1 and eu-west-1) and will not work from other regions. In the rest of this document, we are assuming you are using Northern Virginia (us-east-1) region, do not forget to adjust slightly for Ireland (eu-east-1).</p><h3>Let’s go through this step by step:</h3><p>1. In the AWS Console, open CloudFormation and verify you’re well connected to N. Virginia (us-east-1) region, then click on “Create Stack”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9euftvesmtb6wLpx.jpg" /></figure><p>2. Click on “Choose File” and select cloudformation-us-east-1.json downloaded from the link above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*R12jPEnhCLR8xEZl.jpg" /></figure><p>3. Click Next on the bottom on the screen</p><p>4. Give your stack a name, such as “HTTPProxy” and the two URLs needed to connect to on your OAuth server; the authentication URL and the access token URL. The template gives the ones for “Login With Amazon” by default, be sure to type the correct values for your authentication server. Click “Next” when ready.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9L71uZu61-4mTbtC.jpg" /></figure><p>5. Ignore the settings on the following screen and click “Next” again.</p><p>6. On the last screen, review your settings and click on the checkbox “I acknowledge that AWS CloudFormation might create IAM resources.”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6TckEq_600UUKr3-.jpg" /></figure><p>The reason why this box is present is because the template asks CloudFormation to create an IAM role to authorize API Gateway to send its log to CloudWatch Logs. The template also creates a <a href="http://docs.aws.amazon.com/lambda/latest/dg/welcome.html">Lambda function</a> to perform on the fly some URL parsing required to configure the API Gateway. A second IAM role is created to allow the Lambda function to send its own logs to CloudWatch Logs.</p><p>The creation of the stack should take 1–2 minutes; maybe time for a very short coffee break, like an expresso. When you return, the stack status must be CREATE_COMPLETED. If you have an error (ROLLBACK_COMPLETED), click on the “Event” tab just below and scroll down to search for the very first error in the stack. Usually error messages are descriptive enough to help you figure out what went wrong.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wbmXoJVzKUxsMzZ7.jpg" /></figure><p>Congratulations! Now the API Gateway is up and running, ready to accept requests. Next steps are to configure Account Linking in the Alexa developer console to point to the API Gateway.</p><p>7. Click on the “Outputs” tab to copy / paste the two URLs that have been created for you and report them to the Alexa developer console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mgDhl4-79fehxdUU.jpg" /></figure><p>Pay attention when copy / pasting. ProxyAuthenticationURL goes to “Authentication URL” in the Alexa developer console and ProxyAccessTokenURL goes to “Access Token URI” in the Alexa developer console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4_z93JJ6tormMxJv.jpg" /></figure><p>8. Trigger an authentication on your skill and test your setup.</p><p>If everything goes well, and if account linking was working before making this change, it must still work. The difference now is that all the HTTP traffic between Alexa and your OAuth server is logged into CloudWatch Logs.</p><p>In the AWS Console, open CloudWatch and click on Logs. You should see three new Logs Groups.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Hddj32loqLuVCXN4.jpg" /></figure><p>The “Lambda” one contains the log of a temporary lambda function used during the stack creation only.<br> The “API Gateway Execution” contains the log of your traffic. This group contains hundreds of streams, you can sort them by date to see the ones that recently received new logs.</p><p>9. Click on “API-Gateway-Execution-Log_*” to open the group, sort the streams by date and click on the last one or the one before last. One contains the request to your Authentication URL, the other the request to your Access Token URL.</p><p>By looking at the logs, you can see entries for your request (path, query string, headers, body) and for the response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mxbMrQoxRG_9GZx0.jpg" /></figure><p>Enjoy! You know have a full HTTP logging system to debug your OAuth configuration.</p><p>Once you will finish your debugging session, do not forget to tear down the infrastructure to minimize your costs and exposure. You can delete the entire setup from the AWS CloudFormation console, just click on “Delete Stack” to delete everything except the log files. Log files are preserved for you for later analysis.</p><h3>How much does it cost?</h3><p>API Gateway is a very cost effective service, AWS will charge you $3.5 per million requests received + $0.09 / GB for data transfer.</p><p>AWS Cloudwatch Logs will charge you $0.50 / GB for storage log files and it is very unlikely you pass the part of 100 Mb for a single logging session.</p><p>Hypothetically, let’s say you are running a debugging session with 10.000 HTTP requests, 200 bytes each, it will cost you</p><p>0,01 million requests x $3.5 = $0.035</p><p>200 bytes x 10.000 x 2 (in and out) = 0.004 Gb data transfer x $0.09 / Gb = $0.00039</p><p>Logs (assuming logs are 10x more verbose than content) : 0.04 Gb x $0.05 / Gb = $0.002</p><p>Total: $0.035 + $0.00039 + $0.002 = $0.03739</p><p>In this scenario, running debugging sessions for a few days with thousands of HTTP requests will cost you less than a dime.</p><h3>Keep us posted</h3><p>Are you having problems following these instructions? Or the API gateway configuration does not work with your OAuth server? Let us know by posting a message in the <a href="https://forums.developer.amazon.com/spaces/23/index.html">Alexa Developer forums</a> or by tweeting us <a href="https://twitter.com/AlexaDevs">@AlexaDevs</a>.</p><p><em>Originally published at </em><a href="https://developer.amazon.com/blogs/alexa/post/TxQN2C04S97C0J/how-to-set-up-amazon-api-gateway-as-a-proxy-to-debug-account-linking"><em>developer.amazon.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba4b457d82d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Alexa Account Linking: 5 Steps to Seamlessly Link Your Alexa Skill to User Systems That Require…]]></title>
            <link>https://medium.com/@sebsto/alexa-account-linking-5-steps-to-seamlessly-link-your-alexa-skill-to-user-systems-that-require-20cf86237dbf?source=rss-198b44d70be4------2</link>
            <guid isPermaLink="false">https://medium.com/p/20cf86237dbf</guid>
            <category><![CDATA[account-linking]]></category>
            <category><![CDATA[oauth]]></category>
            <category><![CDATA[alexa]]></category>
            <dc:creator><![CDATA[Sébastien ☁ Stormacq]]></dc:creator>
            <pubDate>Sun, 20 Jan 2019 13:43:18 GMT</pubDate>
            <atom:updated>2019-01-20T13:43:18.164Z</atom:updated>
            <content:encoded><![CDATA[<h3>Alexa Account Linking: 5 Steps to Seamlessly Link Your Alexa Skill to User Systems That Require Authentication</h3><p>August 03, 2016</p><p><strong><em>Editor’s Note: </em></strong><em>This post was updated in January 2019.</em></p><p>Some skills require the ability to connect the identity of an Alexa end user with a user in another system, such as Twitter, Facebook, Amazon, and many others. For example, suppose you own a web-based service “<em>Car-Fu</em>” that lets users order taxis. It would be very convenient for people to access Car-Fu by voice (“Alexa, ask Car-Fu to order a taxi”).</p><p>To accomplish that, you’d use a process called <a href="https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/linking-an-alexa-user-with-a-user-in-your-system">account linking</a>, which provides a secure way for Alexa skills to connect with third-party systems requiring authentication.</p><p>Skills that use the <a href="https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/overviews/understanding-the-smart-home-skill-api">Smart Home Skill API</a> <em>must</em> use account linking (with the authorization code grant flow) to connect the Alexa user with their <em>device cloud account</em>. Custom skills can use account linking if desired. However, if your <em>custom skill</em> merely needs to keep track of a user to save attributes between sessions, you do not need to use account linking.</p><p>There are many ways you can use account linking to enhance your Alexa skills. For example:</p><ul><li>You can map this user profile to an existing user in your user database, using the email address as key. This would allow you to create a contextual skill that behaves according to your user’s preferences and history.</li><li>You can decide what authorizations this user will have in your system.</li><li>You can use services such as <a href="https://aws.amazon.com/cognito/">Amazon Cognito Identity Pools</a> to acquire an AWS Access Key and Secret Key to interact with AWS Services such as <a href="https://aws.amazon.com/dynamodb/">Amazon DynamoDB</a>.</li></ul><h3>The Basics of Account Linking with the Login with Amazon (LWA) Service</h3><p>Account linking leverages <a href="http://oauth.net/">OAuth</a> 2.0; an open protocol that provides a simple, standards-based method for web, mobile and desktop applications to request user authorization from remote servers.</p><p>As a skill developer, you could set up and configure your own OAuth server and identity management system. At some large companies, an OAuth server is probably already available and Identity Management procedures already in place. However, at smaller companies, this would require you to build, operate, and maintain your own complex system to manage user identities, passwords, and profiles in a secure and scalable way.</p><p>Many organizations rely instead on well-known identity providers, available on the internet. These are sites where nearly everyone has an account, such as Facebook, Google, Twitter, and Amazon. The service that acts as a public-facing identity provider for Amazon is <a href="https://login.amazon.com/">Login with Amazon</a>.</p><p>When using OAuth, you delegate user authentication to a third-party Identity Provider (IDP). As illustrated below, the user is redirected to the IDP web site. User authentication happens according to the IDP’s policies (username and password, one-time password, biometric, etc.), and upon successful authentication, the IDP generates an <a href="https://tools.ietf.org/html/rfc6749#section-4.2">implicit grant</a> (aka bearer token) or an <a href="https://tools.ietf.org/html/rfc6749#section-4.1">authorization code grant</a>.</p><p>The bearer token is the token you’ll use for accessing information and services. On the other hand, an authorization code can only be used to <em>request</em> a bearer token. This usually happens on the backend, between your application server and the IDP service. While an implicit grant is often faster and simpler for developers to request, an authorization code grant is generally considered more secure and some IDPs may require it for sensitive information or services. Also, a code grant allows for automatic refreshing of the bearer token after a given expiry, which will be set according to the IDP’s policy. When using an implicit grant, the user has to manually re-authenticate themselves when attempting to use the service, which, depending on the lifespan of the bearer token, can cause friction for account linking in applications.</p><p>After authentication is complete and a valid token is received, your application is responsible for managing authorization based on the customer’s profile.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zYe0rRX2gJYdObQF.jpg" /></figure><p><em>Figure 1: OAuth data flow</em></p><h3>Account Linking Step by Step</h3><p>Follow these steps to configure your Alexa skills with account linking and Login with Amazon.</p><p>If you haven’t created a skill yet, please first use the <a href="https://developer.amazon.com/alexa-skills-kit">Alexa Skills Kit</a> to build something relatively simple, such as a <a href="https://github.com/alexa/skill-sample-nodejs-trivia">trivia skill</a>. You can get a prototype up and running in just a few hours and come back here once it’s published.</p><h3>Step 1: Create a Login with Amazon (LWA) Security Profile</h3><p>First, you need to create a LWA profile for your Alexa skill. Here’s how.</p><ul><li>Connect to <a href="https://developer.amazon.com/">https://developer.amazon.com</a> and authenticate with your Amazon credentials</li><li>Click on “Login with Amazon” then “Security Profiles”</li><li>Click on “Create a New Security Profile”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JkGcqlCK5tksRK7E.png" /></figure><p><em>Figure 2: Access Login With Amazon</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*o7aQ_0JDuZCAosEY.png" /></figure><p><em>Figure 3: Create a Security Profile</em></p><p>Fill in all three required fields to create your security profile and click “Save.” For the purpose of this article, I am using Amazon’s privacy policy URL. <em>Make sure to replace the link with a link to your own Data Privacy policy. </em>The end result should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CN-wq1khzSvuUcGV.png" /></figure><p><em>Figure 4: Create Security Profile</em></p><p>Before you complete this step, be sure to copy your Client ID and Client Secret from the Web Settings tab (see Figure 4) to a text editor so they’re easily available. You’ll need these values later in the process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fqHLnSYdcG_sQSe4.png" /></figure><p><em>Figure 5: Collect Client ID and Client Secret</em></p><h3>Step 2: Configure Account Linking in the Alexa Developer Console</h3><p>Now that you’ve configured Login with Amazon, you can configure Account Linking for your Alexa skill. (Remember, you need to have a skill already created to do this). In the Alexa developer console, choose the skill you want to edit (or create a new skill).</p><p>Under the “Build” tab for your skill, click “Account Linking” on the left side. To enable account linking, turn on the “Do you allow users to create an account or link to an existing account with you?” toggle.</p><p>Choose “Allow users to enable skill without account linking” if you want to allow users to start to use the skill without Account Linking and let them link their account later during the skill usage (see <a href="https://developer.amazon.com/blogs/alexa/post/6cf86afd-a908-4e4f-b1ba-05caa0c299e0/announcing-optional-account-linking-functionality-allowing-customers-to-enable-and-use-your-alexa-skill-without-first-linking-accounts">Optional Account Linking</a> for the details).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Mua1Kv8nOo8BO1_g.png" /></figure><p><em>Figure 6: Account Linking Details</em></p><p>Then, enter the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SanF3ocLjPAiswzE.png" /></figure><p><em>Figure 7: Account Linking Details</em></p><p>The values you need to enter into these fields are detailed in the table below.</p><p><strong>Key : Value</strong></p><p>Authorization Grant Type : Auth Code Grant</p><p>Authorization URI : <a href="https://www.amazon.com/ap/oa">https://www.amazon.com/ap/oa</a></p><p>This is the URL where Alexa Service will send the customers for authentication. This URL will show customers the Login With Amazon login page.</p><p>Access Token URI : <a href="https://api.amazon.com/auth/o2/token">https://api.amazon.com/auth/o2/token</a></p><p>This is the URI the Alexa Service needs to use to exchange an authorization code for a token and to refresh expired tokens. This is the same value for all Login with Amazon applications.</p><p>Client ID : The Client ID received from Login with Amazon during Step 1 above. This has a format such as amzn1-application-oa2-client-xxx</p><p>Client Secret : The client secret received from Login With Amazon during Step 1 above.</p><p>Scope : Login with Amazon supports <a href="https://developer.amazon.com/public/apis/engage/login-with-amazon/docs/customer_profile.html">several scopes</a>, for this example, let’s use “profile”. This will allow your code to retrieve a unique Amazon userid, email address, and full name for the user.</p><p>The last step is to configure Login with Amazon to accept our redirection URLs. The redirection URLs are given on the Alexa Developer Console, 1 per geographic area, under “Redirect URL”. These are the URLs starting by <a href="https://pitangui.amazon.com/%E2%80%A6r">https://pitangui.amazon.com/…</a> (North America) or <a href="https://layla.amazon.com/...">https://layla.amazon.com/...</a> (EU and India) <a href="https://alexa.amazon.co.jp/">https://alexa.amazon.co.jp</a>... (Far East)</p><p>Copy this value to that text editor I suggested earlier and go back to the Login with Amazon console.</p><p>Next to the Security Profile you created in Step 1, click the button with the gear icon and select “Web Settings” from the menu.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FoP20LZ6ggUUHlqw.png" /></figure><p><em>Figure 8: Login with Amazon — Redirection URL</em></p><p>Click the “Edit” button in the lower right, then enter the Redirect URL, as it appears on the ASK Developer Console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qrRoXyENa8SOzN1z.jpg" /></figure><p><em>Figure 9: Login with Amazon — Redirect URL</em></p><p>Now you are ready to write some code.</p><h3>Step 3: Write Your Custom Skill Code</h3><p>For the purpose of this blog post, I chose to write the Alexa custom skill code in NodeJS and deploy it on AWS Lambda.</p><p>You can learn more about writing code for custom skills <a href="https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/overviews/understanding-custom-skills">here</a>.</p><p>When your skill is called the first time, you will receive a JSON document like this:</p><pre>{<br> &quot;session&quot;: {<br>    &quot;sessionId&quot;: &quot;SessionId.7c77f955-ae5e-46f1-a9ca-3fb354b1ce77&quot;,<br>    &quot;application&quot;: {<br>  &quot;applicationId&quot;: &quot;amzn1.echo-sdk-ams.app.fb2fc3e7-55e7-4f05-851e-7ad308a6b499&quot;<br>    },<br>    &quot;user&quot;: {<br>       &quot;userId&quot;: &quot;amzn1.ask.account.AFP3ZWPOS2BGJR7OWJZ3DHPKMOMNWY4AY66FUR7ILBWANIHQN73QHMAHCTVTAHFPUV3WAFNGKBYW5LFUE2WV4CCGEISSPHBHCW5SCYJDL3DLWRRRBB4CQIP3X3PV65V2AYMMXSNFPKHPZJFVAH5AHKHDLAER67H3AHL4XBUU76XEH5BGDZZNGV5T6HHAGN6KGZI777J3VA7WGPA&quot;<br>    },<br>    &quot;new&quot;: true<br>  },<br>  &quot;request&quot;: {<br>     &quot;type&quot;: &quot;IntentRequest&quot;,<br>     &quot;requestId&quot;: &quot;EdwRequestId.b595d667-caa3-4818-8d4a-c7a31e8adddc&quot;,<br>     &quot;timestamp&quot;: &quot;2016-05-27T15:36:26Z&quot;,<br>     &quot;intent&quot;: {<br>         &quot;name&quot;: &quot;SayHello&quot;,<br>         &quot;slots&quot;:{}<br>     },<br>     &quot;locale&quot;: &quot;en-US&quot;<br>   },<br> &quot;version&quot;: &quot;1.0&quot;<br>}</pre><p>Notice that the “user” section contains a userId properties, but no OAuth access token is available yet as we did not authenticate our user yet.</p><p>Whenever users invoke intents in your skill that rely on information obtained from account linking, your skill code should return a “LinkAccount” card to be displayed in the Alexa app or the Alexa web site. In this example, the card will contain a link allowing the user to authenticate on Login with Amazon. In addition to the card, it is a recommended practice to return a voice prompt inviting the user to authenticate in the app.</p><p>Below is an example of what this might look like using the <a href="https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs">ASK SDK for Node.js V2</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbe9de5581aa137038d857aecd77bde9/href">https://medium.com/media/cbe9de5581aa137038d857aecd77bde9/href</a></iframe><p>And the corresponding JSON output looks like this:</p><pre>{<br> &quot;version&quot;: &quot;1.0&quot;,<br> &quot;response&quot;: {<br>    &quot;outputSpeech&quot;: {<br>       &quot;type&quot;: &quot;PlainText&quot;,<br>       &quot;text&quot;: &quot;You must authenticate with your Amazon Account to use this skill. I sent instructions for how to do this in your Alexa App&quot;<br>    },<br>  &quot;card&quot;: {<br>     &quot;type&quot;: &quot;LinkAccount&quot;<br>  },<br>  &quot;shouldEndSession&quot;: true<br>  },<br> &quot;sessionAttributes&quot;: {}<br>}</pre><h3>Step 4: Test Account Linking on Your Skill</h3><p>When you invoke your skill from a device, you will receive a card in the Alexa app asking you to link your account.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bkiRRDh-4Hy39sUa.jpg" /></figure><p><em>Figure 10: Account Linking Card</em></p><p>Click on the “Link Account” link, and the Login with Amazon page will display.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/778/0*lwVmwOb5xxRMmzRg.jpg" /></figure><p><em>Figure 11: Login with Amazon Credentials</em></p><p>Authenticate using your regular Amazon login (the same login used when shopping on Amazon.com).</p><p>The very first time, Amazon will ask you if you authorize the Alexa skill to retrieve some data from your user profile.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/0*g2hTa8vAe3qRzMrG.png" /></figure><p><em>Figure 12: Login with Amazon — Authorization Screen</em></p><p>Click ‘Okay’ and a confirmation message appears.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/896/0*xKYcjnGuaN5zaP42.jpg" /></figure><p><em>Figure 13: Success — Account Linking Complete</em></p><h3>Step 5: Test the Skill Again</h3><p>Invoke the skill again. Now, because your account is linked, an Amazon OAuth Token is included in the input JSON document.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7d6a3b61500df7f8d48ce30bb3cc2c4/href">https://medium.com/media/c7d6a3b61500df7f8d48ce30bb3cc2c4/href</a></iframe><p>Your code can use this access token to fetch the <a href="https://developer.amazon.com/public/apis/engage/login-with-amazon/docs/customer_profile.html">user profile data</a> from the Amazon Profile service API. Here is an asynchronous function to retrieve the Amazon profile. This function uses the Axios request library, which you can add by typing “npm install axios” in your project’s root directory. You can then import it at the top of your index.js file.</p><pre>const axios = require(&#39;axios&#39;);</pre><p>And then in your handle function (notice the async and await keywords)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46be34a64f1442a70f2b93d5f579e154/href">https://medium.com/media/46be34a64f1442a70f2b93d5f579e154/href</a></iframe><p>The code above will return the following JSON output:</p><pre>{ &quot;version&quot;: &quot;1.0&quot;,<br>  &quot;response&quot;: {<br>    &quot;outputSpeech&quot;: {<br>       &quot;type&quot;: &quot;PlainText&quot;,<br>       &quot;text&quot;: &quot;Hello, Sebastien&quot;<br>    },<br>  &quot;shouldEndSession&quot;: true<br>  },<br>  &quot;sessionAttributes&quot;: {}<br>}</pre><h3>Keep Us Posted on Your Success</h3><p>Congratulations, if you coded along to this point, you have successfully implemented Account Linking with the Login with Amazon service.</p><p>Tell us how you’re enhancing your Alexa skills using account linking by tweeting us <a href="https://twitter.com/AlexaDevs">@AlexaDevs</a>. For further info, check out our developer pages on <a href="https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/linking-an-alexa-user-with-a-user-in-your-system">Account Linking</a> and how to use the <a href="https://developer.amazon.com/appsandservices/apis/engage/login-with-amazon">Login with Amazon</a> service.</p><p><em>Originally published at </em><a href="https://developer.amazon.com/blogs/alexa/post/Tx3CX1ETRZZ2NPC/alexa-account-linking-5-steps-to-seamlessly-link-your-alexa-skill-with-login-with-amazon"><em>developer.amazon.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=20cf86237dbf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Running AWS AppSync clients on Android v5 and older]]></title>
            <link>https://medium.com/@sebsto/running-aws-appsync-clients-on-android-v5-and-older-6ec3003aed80?source=rss-198b44d70be4------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ec3003aed80</guid>
            <category><![CDATA[aws-appsync]]></category>
            <category><![CDATA[ssl]]></category>
            <category><![CDATA[appsync]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[tls]]></category>
            <dc:creator><![CDATA[Sébastien ☁ Stormacq]]></dc:creator>
            <pubDate>Thu, 03 Jan 2019 23:00:00 GMT</pubDate>
            <atom:updated>2019-01-21T08:31:00.878Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*KyGjZI4ZoQtz565O22i_Jg.jpeg" /></figure><p>When developing mobile applications, supporting older devices is a common requirement, in particular for Android mobiles where the market is much more fragmented than on iOS. <a href="https://developer.apple.com/support/app-store/">According to Apple</a>, 72% of the devices sold in the last 4 years are running iOS 12, while <a href="https://developer.android.com/about/dashboards/">according to Google</a>, only 21% of devices are running Android 8.x (Oreo). On Android, you will need to support all versions back to Android 5.x (Lollipop) to reach 90% of the active devices.</p><p>One of the challenges doing so is to continue to use state-to-the-art network protocols on operating systems released several years ago, and not always maintained anymore. For example, Android 5.x only supports TLSv1.0 and SSLv3 protocols by default, which <a href="https://blog.qualys.com/ssllabs/2014/12/08/poodle-bites-tls">have been proved to be insecure</a> since then. Backend services, on the other side, now rely on TLSv1.2, in order to meet strict security requirements (<a href="https://threatpost.com/federal-agencies-told-to-support-tls-1-2-by-2015/105906/">NIST</a>, <a href="https://blog.pcisecuritystandards.org/are-you-ready-for-30-june-2018-sayin-goodbye-to-ssl-early-tls">PCI-DSS</a> are just two examples).</p><p>For example, AWS AppSync endpoints only offer TLSv1.1 and TLSv1.2 during the protocol negotiation. You can verify this using the nmap command, such as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c07d29d8680fd324377e1f714e8daf76/href">https://medium.com/media/c07d29d8680fd324377e1f714e8daf76/href</a></iframe><p>So, on the client side, you have <a href="https://github.com/square/okhttp/issues/1934">TLSv1.0 and SSLv3 by default</a>, while on the server side, you have TLSv1.1 and TLSv1.2.</p><p>On Android Lollipop, if you try <a href="https://github.com/awslabs/aws-mobile-appsync-sdk-android">to instantiate an AppSync client</a> using the default configuration, such as this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8907ad49a7c15bd6320b11815d58c220/href">https://medium.com/media/8907ad49a7c15bd6320b11815d58c220/href</a></iframe><p>You will get a runtime error like the below:</p><p>12-23 15:07:04.414 2414-2431/com.stormacq.android.myapp W/RetryInterceptor: Encountered IO Exception making HTTP call [javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb7e76370: <strong>Failure in SSL library</strong>, usually a protocol error error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:<strong>sslv3 alert handshake failure</strong> (external/openssl/ssl/s23_clnt.c:741 0xa909d925:0x00000000)]</p><p>You’ll need to write an extra bit of code to use TLSv1.1 or TLSv1.2 on Android 5.x. The good news is that TLSv1.1 and TLSv1.2 are available on Android 5.x, they are just not enabled by default.</p><p>To enable these protocols, two steps are needed.</p><p>First, you will need to create your own <a href="https://developer.android.com/reference/javax/net/ssl/SSLSocketFactory">SSLSocketFactory</a> class. This class’ responsibility is to create and configure SSL TLS Sockets. The <a href="https://github.com/sebsto/maxi-80-android-exoplayer2/blob/maxi80/Maxi80/src/main/java/com/stormacq/android/maxi80/TLSSocketFactory.kt">implementation</a> is mostly boilerplate code, with the exception of adding TLSv1.1 and TLSv1.2 in the list of supported protocols to new sockets.</p><p>socket.enabledProtocols = arrayOf(&quot;TLSv1.1&quot;, &quot;TLSv1.2&quot;)</p><p>Second, you will need to tell AppSync Client to use that new SSLSocketFactory class instead of the default. As AppSync Client is using <a href="https://github.com/square/okhttp/">OKHTTP</a> library behind the scene, you’ll need first to create your own OKHttpClient and configure it to use your SSLSocketFactory class created just before. Then you will provide AppSync Client with your own OkHttpClient. The code looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d2fa9709a72d58bb103e638a1ad21ac/href">https://medium.com/media/8d2fa9709a72d58bb103e638a1ad21ac/href</a></iframe><p>Full code is available on <a href="https://github.com/sebsto/maxi-80-android-exoplayer2/blob/maxi80/Maxi80/src/main/java/com/stormacq/android/maxi80/Maxi80Application.kt#L96">GitHub</a>.</p><p>This is something you are likely to do only once, as one App Sync client object can be reused for all calls. I am typically instantiating it the onCreate() method of the Application() object, or a bit later depending if your application identifies users or not.</p><p>Now you are ready to use AppSync client with TLSv1.1 and TLSv1.2 on Android 5.x or older.</p><p>Let me know your feedback on <a href="https://twitter.com/sebsto">Twitter</a>.</p><p><em>Originally published at </em><a href="https://www.stormacq.com/aws/appsync/2019/01/03/tlsv1_with_app_sync.html"><em>stormacq.com</em></a><em> on January 3, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ec3003aed80" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Receive Alerts When Specific APIs Are Called by Using AWS CloudTrail, Amazon SNS, and AWS…]]></title>
            <link>https://medium.com/@sebsto/how-to-receive-alerts-when-specific-apis-are-called-by-using-aws-cloudtrail-amazon-sns-and-aws-bc18c89a9a1b?source=rss-198b44d70be4------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc18c89a9a1b</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[cloudtrail]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[sns]]></category>
            <dc:creator><![CDATA[Sébastien ☁ Stormacq]]></dc:creator>
            <pubDate>Fri, 15 May 2015 16:20:31 GMT</pubDate>
            <atom:updated>2019-01-20T13:29:40.063Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Receive Alerts When Specific APIs Are Called by Using AWS CloudTrail, Amazon SNS, and AWS Lambda</h3><p>Let’s face it — not all APIs were created equal. For example, you may be really interested in knowing when any of your Amazon EC2 instances are terminated (ec2:TerminateInstance), but less interested when an object is put in an Amazon S3 bucket (s3:PutObject). In this example, you can delete an object, but you can’t bring back that terminated instance. So this begs the question, “Is there a way to be notified when certain APIs are called using my AWS account?” The answer is yes! But how, though, can you be notified if unexpected API calls are made to supported services in your AWS account?</p><p>This blog post will show you how to receive email notifications by using AWS CloudTrail, Amazon Simple Notification Service (SNS), and AWS Lambda when specific APIs that you are interested in monitoring are called in your AWS account. Specifically, this post includes step-by-step configuration instructions for implementing a solution, which combines:</p><ul><li>CloudTrail — As the source of log files for analysis.</li><li>Lambda — To implement the parsing and filtering logic.</li><li>SNS — To send notifications.</li></ul><p>The following diagram shows how these AWS services work together.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/0*_8V01W75nnkGkG9a.png" /></figure><p>The following is a breakdown of the process shown in the preceding diagram:</p><ol><li>CloudTrail generates a log entry for every API call made on your account. CloudTrail aggregates the log entries in JSON text files, zips the files, and copies them to the Amazon S3 bucket configured to receive CloudTrail log files.</li><li>The log parsing logic is deployed as a Lambda function. The Lambda function is triggered when CloudTrail copies a new file to your S3 bucket.</li><li>The Lambda function uses a configuration file that contains a list of specific API keywords that, when detected, will trigger a notification. The configuration file is stored in an S3 bucket.</li><li>Whenever relevant keywords are detected in the CloudTrail logs, the Lambda function posts a notification to an SNS topic.</li><li>SNS dispatches the notification to every topic subscriber via email, Amazon SQS, SMS, HTTP call, or mobile push.</li></ol><p>The rest of this blog post walks through the step-by-step instructions to implement the flow shown in the preceding diagram. I will complete the AWS interactions in this walkthrough via the <a href="http://aws.amazon.com/cli/">AWS Command Line Interface (CLI)</a> on an Amazon Linux instance.</p><p>The prerequisites for these step-by-step configuration instructions are the following:</p><ul><li>An AWS account.</li><li>Basic knowledge of AWS concepts such as regions and access keys.</li><li>The AWS CLI installed on your computer and configured with administrative privileges (the commands will not run under an EC2 role — they require actual user credentials). You can follow <a href="http://docs.aws.amazon.com/cli/latest/userguide/installing.html">instructions to install the AWS CLI</a> and <a href="http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-quick-configuration">documentation to configure the AWS CLI</a> with your access key, secret key, default output format, and default region. Be sure to use AWS CLI version 1.7.22 or more recent.</li><li>To test the Lambda function on your laptop before deploying it on AWS, you must also install <a href="https://nodejs.org/download/">NodeJS4.3</a> and the npm tool and be familiar with a *nix-family shell.</li></ul><p>This walkthrough will use us-west-2, US West (Oregon), as the default region. The full source code for this walkthrough is available on <a href="https://github.com/awslabs/aws-training-demo/tree/master/service/lambda/cloudtrail">GitHub</a>.</p><h3>The Walkthrough</h3><h3>Step 1: Create an S3 bucket and configure CloudTrail</h3><p>First, you will create an S3 bucket for CloudTrail to store the CloudTrail log files of your account’s API calls and to enable the CloudTrail service itself. If you have already enabled CloudTrail for your account, you can skip directly to Step 2.</p><p>The S3 bucket must have a unique name and requires an <a href="http://docs.aws.amazon.com/AmazonS3/latest/dev/using-iam-policies.html">IAM bucket policy</a> to ensure that the CloudTrail service has sufficient privileges to create files in the bucket. An <a href="http://docs.aws.amazon.com/awscloudtrail/latest/userguide/create_trail_bucket_policy.html">Amazon S3 Bucket Policy</a> provides a CloudTrail default S3 policy.</p><p>The following cloudtrail create-subscription command will automatically create the bucket, associate a bucket policy for CloudTrail access, and enable and configure CloudTrail for your account in that region.</p><p>Make sure you modify the <strong>$BUCKETNAME</strong> value, because S3 bucket names must be globally unique.</p><pre>REGION=us-west-2<br><strong>BUCKETNAME</strong>=cloudtrail-logs-abcd1234<br>TRAILNAME=security-blog-post<br>aws cloudtrail create-subscription --region $REGION --s3-new-bucket <strong>$BUCKETNAME</strong> --name $TRAILNAME</pre><p>From now on, all your AWS API calls from the console, AWS CLI, or the SDK will be recorded and delivered to you in a text file with content similar to the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea4fb96c0c550ae7cf45a94f2ba3c604/href">https://medium.com/media/ea4fb96c0c550ae7cf45a94f2ba3c604/href</a></iframe><h3>Step 2: Create an SNS topic and subscribe an email address</h3><p>You will now create an SNS topic that will send subscribers a copy of every message published to the topic. Subscribers can use different protocols: HTTPS, Mail, SQS, SMS (to US-based numbers only), and push notifications to mobile devices.</p><p>For the purpose of this walkthrough, you will subscribe an email address to the SNS topic. The Lambda function will post an SNS notification for every unexpected API call detected in the CloudTrail logs. Create the SNS topic and subscribe your email address to it (replace <strong>&lt;YOUR EMAIL ADDRESS&gt;</strong> with your actual email address).</p><pre>EMAIL=<strong>&lt;YOUR EMAIL ADDRESS&gt;<br></strong>TOPIC_ARN=$(aws sns create-topic --name cloudtrail_notifications --output text --region $REGION)<br>aws sns subscribe --protocol email --topic-arn $TOPIC_ARN --notification-endpoint $EMAIL --region $REGION<br>echo $TOPIC_ARN</pre><p>Be sure to write down the topic ARN given as output of the previous command. You will need the topic ARN to configure the Lambda function later. The topic ARN should look similar to the following.</p><pre>arn:aws:sns:us-west-2:577xxxxxxx68:cloudtrail_notifications</pre><p>You will receive a confirmation email (check your spam folder, just in case). The email contains a link to confirm the subscription. You must click this link to complete the subscription process.</p><h3>Step 3: Create IAM roles for your Lambda function</h3><p>A Lambda function needs an IAM role to work correctly.</p><p>The IAM <em>execution role</em> refers to an IAM role that grants your custom code permissions to access the AWS resources it needs. Lambda assumes this role while executing Lambda functions on your behalf.</p><p>A role has two parts: a trust policy that defines which services or users can be authorized to assume the role, and one or more policies defining the permissions granted. Lambda assumes a role when your function is executed. Your function has only the permissions described in the role’s policies.</p><p>The following command creates the trust policy to authorize Lambda to assume the execution role.</p><pre>cat &gt; role_trust_policy.json &lt;&lt;EOF<br>{   &quot;Version&quot;: &quot;2012-10-17&quot;,   <br>    &quot;Statement&quot;: [{       <br>       &quot;Sid&quot;: &quot;&quot;,<br>       &quot;Effect&quot;: &quot;Allow&quot;,<br>       &quot;Principal&quot;: {<br>         &quot;Service&quot;: &quot;lambda.amazonaws.com&quot;<br>       },<br>       &quot;Action&quot;: &quot;sts:AssumeRole&quot;<br>     }]<br>}<br>EOF</pre><p>Then, create the execution role itself.</p><pre>ROLE_ARN=$(aws iam create-role --role-name lambda-security-blog --assume-role-policy-document file://./role_trust_policy.json --query &#39;Role.Arn&#39; --output text)<br>echo $ROLE_ARN</pre><p>Now that you have created the execution role, add the permissions that the Lambda function requires. The following commands assume that environment variables set by previous commands still exist.</p><pre>cat &gt; role_exec_policy.json &lt;&lt;EOF <br>{<br>   &quot;Version&quot;: &quot;2012-10-17&quot;,<br>   &quot;Statement&quot;: [<br>     {<br>       &quot;Effect&quot;: &quot;Allow&quot;,<br>       &quot;Action&quot;: [<br>         &quot;logs:*&quot;<br>       ],<br>       &quot;Resource&quot;: &quot;arn:aws:logs:*:*:*&quot;<br>     },<br>     {<br>       &quot;Effect&quot;: &quot;Allow&quot;,<br>       &quot;Action&quot;: [<br>         &quot;s3:GetObject&quot;<br>       ],<br>       &quot;Resource&quot;: [<br>         &quot;arn:aws:s3:::$BUCKETNAME/*&quot;<br>       ]<br>     },<br>     {<br>       &quot;Effect&quot;: &quot;Allow&quot;,<br>       &quot;Action&quot;: [&quot;sns:Publish&quot;],<br>       &quot;Resource&quot;: [ &quot;$TOPIC_ARN&quot;<br>       ]<br>     }   <br>   ]<br> }<br> EOF</pre><p>The first statement in the preceding command gives the Lambda function authorization to publish its own logs to Amazon CloudWatch Logs. The second statement gives the Lambda function authorization to read from the CloudTrail log S3 bucket. And the last statement gives the Lambda function authorization to post messages on the SNS topic created earlier.</p><p>The following command will add these three statements to the role.</p><pre>aws iam put-role-policy --role-name lambda-security-blog --policy-name lambda-execution-role --policy-document file://./role_exec_policy.json</pre><h3>Step 4: Create the Lambda function</h3><p>Now that you have enabled CloudTrail, its associated S3 bucket, and an SNS topic, you are ready to create the CloudTrail log parsing function and deploy it to Lambda.</p><h4>Create a bucket in which to store the configuration file</h4><p>The Lambda function uses regular expressions to match unexpected API calls. It will use an external configuration file that is stored on S3 to store the regular expressions that you want the function to find. Using an external configuration file allows you to fine-tune the expression without having to redeploy the Lambda function with every change.</p><p>To simplify deployment, the configuration file will be stored in the same S3 bucket as the CloudTrail log files. The configuration file appears as follows.</p><pre>{<br>    &quot;source&quot; : &quot;iam|signin|sts&quot;,<br>    &quot;regexp&quot; : &quot;Password|AccessKey|PutRolePolicy|User|Token|Login&quot;,<br>    &quot;sns&quot; : {<br>       &quot;region&quot; : &quot;us-west-2&quot;,<br>       &quot;topicARN&quot; : &quot;arn:aws:sns:us-west-2:577xxxxxxx68:PushNotifications&quot;<br>    }<br>}</pre><p>These are the parts of the configuration file:</p><ul><li>The source parameter defines the service APIs that you will monitor, which can be adjusted to match your requirements.</li><li>The regexp parameter defines the list of API calls that will trigger notifications. This also can be adjusted to match your requirements.</li><li>The sns section defines your SNS configuration. Adjust it to the region and topicARN created in Step 2 earlier in this blog post.</li></ul><p>Edit the configuration file to meet your requirements, and then copy it to your S3 bucket by using the following command.</p><pre>aws s3 cp filter_config.json s3://<strong>$BUCKETNAME</strong>/filter_config.json --region $REGION</pre><h4>Create the Lambda function</h4><p>You can create the Lambda function by using any good JavaScript text editor. At the time of this writing, Lambda is using the Node.js framework. The core of your Lambda function will be a JavaScript function, defined as a Node.js handler. When writing your code, remember that Node.js is asynchronous. Every function call returns immediately. You can use callbacks or futures to synchronize your code execution.</p><p>When deploying the Lambda function, you will define the events that will trigger the execution of the Lambda function. Lambda can currently trigger functions in response to an API call, a change in a DynamoDB table, an S3 event, or an SNS notification.</p><p>This walkthrough uses the Q JavaScript Library. Q is abstracting asynchronous operations as <em>promise objects</em> (also known as <em>futures</em>). The Lambda function performs the following operations in sequential order:</p><ol><li>Download the Lambda function configuration file created at the beginning of Step 4.</li><li>Download the CloudTrail log file of whose availability you have just been notified. Lambda passes a record object to the function. This record object contains the name of the file just created on S3.</li><li>Unzip the CloudTrail log file to temporary storage.</li><li>Filter the file, looking for log entries from selected AWS services only. In this example, it is looking for IAM, sign-in, and STS-generated records only.</li><li>Search for given regular expressions.</li><li>Send notifications to SNS when there is a match.</li></ol><p>I will break the Lambda function into separate JavaScript functions, each subfunction implementing one of the preceding operations. The core of the Lambda function is included below. It implements the operations as described.</p><p>The full source code is available on <a href="https://github.com/awslabs/aws-training-demo/tree/master/service/lambda/cloudtrail">GitHub</a>. For clarity, snippets presented below do not include error handling, logging, or Q-related code.</p><pre>exports.handler = function(event, context) {<br>    var bucket = event.Records[0].s3.bucket.name;<br>    var key    = event.Records[0].s3.object.key;</pre><pre>     // Download from S3, Gunzip, Filter and send notifications<br>    download(bucket, key)<br>   .then(extract)<br>   .then(filter)<br>   .then(notify)<br>   .catch(function (error) {<br>      // Handle any error from all above steps<br>   })<br>   .done(function() {<br>      context.done();<br>   });<br>};</pre><p>The Q Library will ensure that each function is called sequentially.</p><p><strong>Download the configuration file</strong></p><p>Use the AWS JavaScript SDK to download the configuration file from S3.</p><pre>var s3     = new aws.S3({apiVersion: &#39;2006-03-01&#39;});<br>var params = { Bucket:<strong>FILTER_CONFIG_BUCKET</strong>, Key:<strong>FILTER_CONFIG_FILE</strong> };<br>var body   = s3.getObject(params)<br>                .createReadStream()<br>                .on(&#39;data&#39;, function(data) {<br>                  FILTER_CONFIG = JSON.parse(data);<br>              });</pre><p>This code downloads the configuration file and parses the text to create a JavaScript object. You will need to adjust the value of the global variables <strong>FILTER_CONFIG_BUCKET</strong> and <strong>FILTER_CONFIG_FILE</strong> to the name of your bucket and configuration file.</p><p><strong>Download the CloudTrail log file from S3</strong></p><p>Download the CloudTrail log file from S3 with the fs module <a href="http://nodejs.org/api/fs.html">provided by Node.js</a> for the I/O operations to a local directory (/tmp) and the AWS Node.js SDK for the S3 API.</p><pre>// extract file name from key name<br>var fileName = key.match(/.*/(.*).json.gz$/)[1]<br>var file     = fs.createWriteStream(&#39;/tmp/&#39; + fileName + &#39;.json.gz&#39;);</pre><pre>// pipe from S3 to local file<br>var s3     = new aws.S3({apiVersion: &#39;2006-03-var params = { Bucket:bucket, Key:key };<br>var stream = s3.getObject(params).createReadStream();   stream.pipe(file);</pre><p>This will place a copy of the file in /tmp on the Lambda container.</p><p><strong>Unzip the CloudTrail log file to temporary storage</strong></p><p>To extract the text-based log file from the compressed archive, use the Node.js GZIP Library.</p><pre>var gzFileName = file.path.match(/.*/(.*).json.gz$/)[1];<br>var jsFileName = &#39;/tmp/&#39; + gzFileName + &#39;.json&#39;;<br>var zlib  = require(&#39;zlib&#39;); var unzip = zlib.createGunzip();<br>var inp   = fs.createReadStream(file.path);<br>var out   = fs.createWriteStream(jsFileName);   inp.pipe(unzip).pipe(out);</pre><p>This will create an input stream to read the file from the local file system, and then pipe this input stream to an output stream attached to another file on the file system.</p><p><strong>Filter for selected AWS services</strong></p><p>Now that you have a JSON text file available locally, parse it to look for the AWS services and API for which you will generate an SNS notification.</p><pre>var cloudTrailLog = require(file);<br>var records       = cloudTrailLog.Records.filter(function(x) {<br>    return x.eventSource.match(new RegExp(FILTER_CONFIG.source)); });</pre><p><strong>Search for regular expression</strong></p><p>The last step loads an array of record objects in memory for parsing. For every matching record, build a message and pass it to the sendNotification() function.</p><pre>for (var i = 0; i &lt; records.length; i++) {<br>   if (records[i].eventName.match(new RegExp(FILTER_CONFIG.regexp))) {<br>     var message = &quot;Event  : &quot; + records[i].eventName + &quot;\n&quot; +                   &quot;Source : &quot; + records[i].eventSource + &quot;\n&quot; +                   &quot;Params : &quot; +                   JSON.stringify(records[i].requestParameters, null, &#39;&#39;) + &quot;\n&quot; +                   &quot;Region : &quot; + records[i].awsRegion + \&quot;n&quot;<br>       var task = sendNotification(message,                                   FILTER_CONFIG.sns.topicARN, &#39;&#39;);<br>  }<br>}</pre><p><strong>Send SNS Notifications</strong></p><p>Finally, send the SNS notification using the AWS JavaScript SDK.</p><pre>var sns = new aws.SNS({<br>    apiVersion: &#39;2010-03-31&#39;,<br>    region: FILTER_CONFIG.sns.region });<br>    var params = {}<br>    params = {<br>      Message: msg,<br>      TopicArn: topicARN };<br>    sns.publish(params, function(err,data) {<br>      if (err) {<br>       // an error occurred<br>    } else {<br>       // successful response<br>    }<br> });</pre><h4>Test the function locally</h4><p>Before uploading the function to Lambda and testing it on AWS, we recommend testing the function on your local machine. This will considerably reduce your develop-deploy-test-debug cycle by quickly catching programming mistakes early in the development cycle. Testing Lambda functions locally before you deploy them should be considered a best practice because it allows you to test your code as you build it and customize it for your AWS account.</p><p>To test the Lambda function locally, I wrote a small piece of code that will invoke the Lambda function as it is invoked from the Lambda service itself. This stub code will provide two objects to your function:</p><ol><li>An input record, similar to the one Lambda will provide when hooked to S3 events. It will create a file (input.json) that will be used to store that record.</li><li>A context object similar to the one Lambda is providing to your function.</li></ol><p>The input record for this test looks like the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9ead6725b4b819ab8e5ecb67d3e8be1/href">https://medium.com/media/e9ead6725b4b819ab8e5ecb67d3e8be1/href</a></iframe><p>Be sure that the bucket name and key point to a real CloudTrail log in your bucket. The size and eTag values are not used by the function and do not need to be adjusted to your data.</p><p>You can obtain this input record object from a real Lambda function or from <a href="http://docs.aws.amazon.com/lambda/latest/dg/walkthrough-s3-events-adminuser-create-test-function-upload-zip-test.html">the documentation</a>. The invocation function is in main.js and looks like the following code.</p><pre> //your lambda function<br> var lambda  = require(&#39;./cloudtrail.js&#39;);<br> <br> //your input record<br> var event   = require(&#39;./input.json&#39;);<br> <br> var context  = {}<br> context.done = function(arg1, arg2) {<br>   console.log(&#39;context.done&#39;)<br> }<br> <br> lambda.handler(event, context)</pre><p>To test your code locally, first install dependencies on your local machine.</p><pre>npm install aws-sdk q</pre><p>The AWS SDK must be configured with an access key, secret key, and default region. If you are using the AWS CLI as described above, you are configured appropriately; otherwise, use aws configure to generate a default configuration file.</p><p>Then, invoke node from your laptop’s command line.</p><pre>node main.js</pre><p>Observe the output of the logs and fix any errors before deploying your code to Lambda.</p><h4>Deploy the Lambda function</h4><p>As soon as you’re confident the function works as expected, you’re ready to deploy it to Lambda. First, create a .zip file that contains the function (cloudtrail.js) and its dependencies (the Q library).</p><pre>zip -r  cloudtrail.zip cloudtrail.js node_modules -x node_modules/aws-sdk/*</pre><p>You do not need to include the AWS JavaScript SDK because Lambda’s execution environment provides it by default.</p><p>Now, upload and create the function on Lambda (<strong>$ROLE_ARN</strong> must be set with the value returned by aws iam create-role in Step 3, earlier in this post.)</p><pre>FUNCTION_ARN=$(aws lambda create-function --zip-file fileb://./cloudtrail.zip --function-name cloudtrail --runtime nodejs4.3 --role &quot;$ROLE_ARN&quot; --handler cloudtrail.handler --region $REGION --output text --query &quot;FunctionArn&quot;)</pre><p>Take note of the Function ARN returned because you will need it in the next step.</p><h4>Hook the function to S3</h4><p>The last thing to do is to configure S3 to trigger the Lambda function when a file is created in the bucket. This is a two-step process. You first need to add permission to the Lambda function to authorize your S3 bucket to trigger the Lambda function execution.</p><pre>AWS_ACCOUNT_ID=$(echo $ROLE_ARN | sed &#39;s/^arn:aws:iam::(.*):.*$/1/&#39;)<br>aws lambda add-permission --function-name cloudtrail --statement-id Id-cloudtrail  --action &quot;lambda:InvokeFunction&quot; --principal s3.amazonaws.com --source-arn arn:aws:s3:::$BUCKETNAME --source-account $AWS_ACCOUNT_ID --region $REGION</pre><p>Be sure to replace <strong>$FUNCTION_ARN</strong> with the function ARN returned when you uploaded the code in Step 4, earlier in this post.</p><p>Next, configure the S3 notification system. You can perform this configuration either from the S3 web console or the command line, using the AWS CLI. When using the AWS CLI, a configuration file is required.</p><pre>cat &gt; s3_notifications_config.json &lt;&lt;EOF {     &quot;CloudFunctionConfiguration&quot;: {<br>     &quot;CloudFunction&quot;: &quot;<strong>$FUNCTION_ARN</strong>&quot;,<br>     &quot;Id&quot;: &quot;cloudtrail-lambda-notifications&quot;,<br>     &quot;Event&quot;: &quot;s3:ObjectCreated:*&quot; }<br>}<br>EOF</pre><p>Be sure to replace <strong>&lt;FUNCTION_ARN&gt;</strong> with the Lambda function ARN that was returned when you uploaded the function in the “Deploy the function” subsection, earlier in this post.</p><p>Finally, turn on event notification from S3 to Lambda.</p><pre>aws s3api put-bucket-notification --notification-configuration file://./s3_notifications_config.json --bucket <strong>$BUCKETNAME</strong> --region $REGION</pre><h3>Step 5: End-to-end testing</h3><p>If you have followed the instructions and completed the steps to this point, congratulations! You are now ready to do end-to-end testing of your deployment.</p><p>The good news is that only a few steps are required to test your deployment. You just need to generate a few unexpected API calls (the ones you have configured in your filter_config.json configuration file). If you are using the configuration file proposed above, sign in to the AWS Management Console, create a couple of roles, and then attach and remove policies from these roles. After a few minutes, CloudTrail will generate the log files, and S3 will trigger your Lambda function and will post messages to your SNS topic.</p><h3>Step 6: Troubleshooting</h3><p>If you don’t receive email notifications immediately, be patient. CloudTrail can take a few minutes to post the CloudTrail log files, and the email delivery depends on your email infrastructure.</p><p>If you have not received emails after 10–15 minutes, check the following:</p><ul><li>Are there CloudTrail log files in your bucket? If not, check your CloudTrail configuration.</li><li>Has your Lambda function been invoked? If not, check your S3 notification settings, including Lambda function invocation permissions.</li><li>Check Lambda logs on CloudWatch logs. The link is available on the Lambda console.</li><li>From the logs:</li><li>Is the event-filtered list empty? Check your regular expression filter in the configuration file.</li><li>Does an error occur when posting to SNS? Check the Lambda function execution role policies.</li><li>Still not receiving email notifications? Did you validate your email address when you subscribed to the topic? Did you check your junk mail folder?</li></ul><h3>Conclusion</h3><p>This blog post walked through the steps required to invoke a Lambda function when CloudTrail makes log files available, and then to filter these files to generate notifications on unexpected API calls. As usual, do not forget to delete services and resources when you are not using them anymore to avoid any extra charges incurred by testing this scenario.</p><p>All source code presented in this blog post is available for you to download on <a href="https://github.com/awslabs/aws-training-demo/tree/master/service/lambda/cloudtrail">GitHub</a>.</p><p>If you have questions or suggestions, visit the <a href="https://forums.aws.amazon.com/forum.jspa?forumID=168">CloudTrail forum</a> and the <a href="https://forums.aws.amazon.com/forum.jspa?forumID=186">Lambda forum</a>.</p><p>– Sébastien</p><p><em>Originally published at </em><a href="https://aws.amazon.com/blogs/security/how-to-receive-alerts-when-specific-apis-are-called-by-using-aws-cloudtrail-amazon-sns-and-aws-lambda/"><em>aws.amazon.com</em></a><em> on May 15, 2015.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc18c89a9a1b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>