<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: David Borgenvik</title>
    <description>The latest articles on DEV Community by David Borgenvik (@dborgenvik).</description>
    <link>https://dev.to/dborgenvik</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F150129%2Ff8be7217-fc1f-4af9-b0a8-71c055c4576f.jpeg</url>
      <title>DEV Community: David Borgenvik</title>
      <link>https://dev.to/dborgenvik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dborgenvik"/>
    <language>en</language>
    <item>
      <title>The context property of AWS Lambda authorizer</title>
      <dc:creator>David Borgenvik</dc:creator>
      <pubDate>Sun, 16 Feb 2020 16:09:13 +0000</pubDate>
      <link>https://dev.to/dborgenvik/the-context-property-of-aws-lambda-authorizer-1ibc</link>
      <guid>https://dev.to/dborgenvik/the-context-property-of-aws-lambda-authorizer-1ibc</guid>
      <description>&lt;p&gt;Last week we ran into a problem where a new lambda didn't get invoked by the API Gateway even though the Authorizer had returned a seemingly correct response. It turned out that this exception: &lt;code&gt;"Execution failed due to configuration error: Invalid JSON in response: Can not deserialize instance of java.lang.String out of START_ARRAY token"&lt;/code&gt; was thrown when the API Gateway did the integration request to the lambda. &lt;/p&gt;

&lt;p&gt;The overview of the solution looks like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb8cm5l7f523eancrp7xq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb8cm5l7f523eancrp7xq.png" alt="Overview of the solution"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The tricky thing with this specific problem is that you will not see any errors unless you know where to look. So let's break down the overview above into steps and look at where the exception happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Client does request and sends in a JWT in the Authorization header&lt;/li&gt;
&lt;li&gt;API Gateway detects that the requested API has an Authorizer and does not have any cached authorizer-response for this JWT. &lt;/li&gt;
&lt;li&gt;API Gateway calls the Authorizer with the JWT in the Authorizer header.&lt;/li&gt;
&lt;li&gt;The Authorizer verifies the JWT and respons with a JSON object containing 3 properties principalId, policyDocument and context.&lt;/li&gt;
&lt;li&gt;The API Gateway tries to do a Lambda proxy integration request&lt;/li&gt;
&lt;li&gt;Lambda calls Dynamodb and returns response&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So the problem we faced was that we created a malformed context property in step 4, by setting some of the properties of the context object to other value types than the approved &lt;em&gt;string&lt;/em&gt;, &lt;em&gt;number&lt;/em&gt; and &lt;em&gt;bool&lt;/em&gt;. But there's no validation when we return the response from the Authorizer. Instead the problem happens in step 5 when API Gateway does the integration request to the proxy lambda with a malformed context object. The first symptom is simply that the request doesn't reach the lambda. So what we needed to do to find the problem was to look at the logs of the API Gateway itself. These logs can be a bit hard to find unless you know where to look. Firstly if it isn't already activated, you need to activate logging in API Gateway by doing this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to AWS API Gateway&lt;/li&gt;
&lt;li&gt;Click on the API you want to activate logs for&lt;/li&gt;
&lt;li&gt;Click on Stages&lt;/li&gt;
&lt;li&gt;Click on the stage you want to activate logs for&lt;/li&gt;
&lt;li&gt;Click on the Logs/Tracing tab&lt;/li&gt;
&lt;li&gt;Check Enable Cloud Watch logs&lt;/li&gt;
&lt;li&gt;Keep Log level: Error (to only get the faulty logs)&lt;/li&gt;
&lt;li&gt;Check Log full requests/responses data
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Funcs6g34vvcgcujqxzch.png" alt="Activate API Gateway logging"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now after trying the request again, we can go to CloudWatch logs. The log group will start with API-Gateway-Execution-Logs_{the-id-of-you-api}. To find the id of your API go to API Gateway. The id will be visible at the top. When we found the log group we searched for &lt;em&gt;"error"&lt;/em&gt; and could then find the exception row: "Cannot deserialize" exception. Just before that we can see the Authorizer response:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvcis25vp6ek1ggtquecr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvcis25vp6ek1ggtquecr.png" alt="Faulty property in the context object of the Authorizer response"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To fix this problem we created a generic stringify-complex-properties method in our Authorizer that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Will go through and stringify any properties that are of type 'object'.
 * @param object
 * @returns mutated object
 */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stringifyComplexProperties&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;prop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;prop&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding a call to the &lt;code&gt;stringifyComplexProperties&lt;/code&gt; method fixed the problem and the API Gateway could now successfully do the integration request towards the lambda:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;principalId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;stringifyComplexProperties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;contextObject&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="na"&gt;policyDocument&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;Version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2012-10-17&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;Statement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;statements&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;AWS has some good documentation regarding the Lambda Authorizer that also states clearly what is OK to set in the &lt;em&gt;context&lt;/em&gt; property. But hopefully this post can help anyone else that fails to read the docs 😉: &lt;br&gt;
&lt;a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>authorizer</category>
      <category>lambda</category>
      <category>aws</category>
      <category>serverless</category>
    </item>
  </channel>
</rss>
