<?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: Tom Vincent</title>
    <description>The latest articles on DEV Community by Tom Vincent (@tlvince).</description>
    <link>https://dev.to/tlvince</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%2F918125%2F27a8c606-5f5e-433d-a5de-0fa1a374f468.jpg</url>
      <title>DEV Community: Tom Vincent</title>
      <link>https://dev.to/tlvince</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tlvince"/>
    <language>en</language>
    <item>
      <title>Lambdaless</title>
      <dc:creator>Tom Vincent</dc:creator>
      <pubDate>Tue, 30 Aug 2022 17:03:42 +0000</pubDate>
      <link>https://dev.to/aws-builders/lambdaless-2mjf</link>
      <guid>https://dev.to/aws-builders/lambdaless-2mjf</guid>
      <description>&lt;p&gt;Lets assume you need to expose a JSON file behind an API. Using a serverless approach with AWS, you might first reach for an architecture like the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rf4H3Px9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tebf4a70onpbkj4gz93m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rf4H3Px9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tebf4a70onpbkj4gz93m.png" alt="API Gateway to Lambda to S3" width="780" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;... i.e. an API Gateway in front of a Lambda, which calls S3. Alternatively, did you know you could remove the Lambda and have API Gateway call S3 directly?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G3nf5trl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tljzil0fbtktbpzlhiop.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G3nf5trl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tljzil0fbtktbpzlhiop.png" alt="API Gateway to Lambda" width="566" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is what I call "Lambdaless". It leverages API Gateway's &lt;code&gt;AWS&lt;/code&gt; &lt;a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-api-integration-types.html"&gt;integration type&lt;/a&gt;, which allows you to expose any AWS service without any intermediate application logic. &lt;a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/models-mappings.html"&gt;Mapping templates&lt;/a&gt; provide the glue to transform request/responses, using the &lt;a href="https://velocity.apache.org/engine/devel/vtl-reference.html"&gt;Velocity&lt;/a&gt; templating language (VTL) and &lt;a href="https://goessner.net/articles/JsonPath/"&gt;JSONPath&lt;/a&gt; expressions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Walkthrough
&lt;/h2&gt;

&lt;p&gt;Continuing with the S3 example above, create an API Gateway with a GET method and set up the integration request per the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;choose the AWS service type, region and Simple Storage Service (S3)&lt;/li&gt;
&lt;li&gt;select the GET HTTP method&lt;/li&gt;
&lt;li&gt;select the "use path override" action type&lt;/li&gt;
&lt;li&gt;enter the object's &lt;code&gt;&amp;lt;bucket&amp;gt;/&amp;lt;prefix&amp;gt;&lt;/code&gt; in the path override field&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pcjcRC2b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rs5kb8nq48h6b004xu7d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pcjcRC2b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rs5kb8nq48h6b004xu7d.png" alt="API Gateway S3 Integration Request" width="880" height="719"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create an IAM role that has a policy that has &lt;code&gt;s3:GetObject&lt;/code&gt; permission on your &lt;code&gt;&amp;lt;bucket&amp;gt;/&amp;lt;prefix&amp;gt;&lt;/code&gt; and a Trust Relationship that allows the API Gateway to assume it to be so. Now all you need to do is switch to the test view, click "test" and you should see the contents of your JSON object in the response body:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4yfnwn_S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wk9qm1wwx66b0wxke04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4yfnwn_S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wk9qm1wwx66b0wxke04.png" alt="API Gateway S3 Response" width="880" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mock integration
&lt;/h3&gt;

&lt;p&gt;Taking the JSON example to its logical conclusion, we can go a step further and remove S3 from the equation altogether. Choose the &lt;code&gt;MOCK&lt;/code&gt; integration type, add the required &lt;code&gt;{"statusCode": 200}&lt;/code&gt; request mapping template and move the contents of your JSON object to the integration response mapping template.&lt;/p&gt;

&lt;p&gt;This approach typically yields ~3ms response times (compared to ~65ms with the additional hop to S3) and is a good solution for static data.&lt;/p&gt;

&lt;h3&gt;
  
  
  DynamoDB
&lt;/h3&gt;

&lt;p&gt;Simple CRUD APIs with DynamoDB are a great fit for Lambdaless. API Gateway's &lt;a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variables-template-example"&gt;\$context variables&lt;/a&gt; includes &lt;code&gt;$context.requestId&lt;/code&gt;, which can be used as a entity's UUID, along with &lt;code&gt;$context.requestTimeEpoch&lt;/code&gt; for created/updated at timestamps.&lt;/p&gt;

&lt;p&gt;Request/response templates can be used to convert to/from DynamoDB's &lt;a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html#Programming.LowLevelAPI.DataTypeDescriptors"&gt;data type descriptors&lt;/a&gt;, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#set($inputRoot = $input.path('$'))
{
  "TableName": "my-table",
  "Key": {
    "uuid": {
      "S": "$context.requestId"
    }
  },
  "Item": {
    "uuid": {
      "S": "$context.requestId"
    },
    "name": {
      "S": "$inputRoot.name"
    },
    "items": {
      "L": [
        #foreach($item in $inputRoot.items)
        {
          "S": "$item"
        }#if($foreach.hasNext),#end
        #end
      ]
    },
    "createdAt": {
      "N": "$context.requestTimeEpoch"
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Other ideas
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;use the &lt;code&gt;HTTP_PROXY&lt;/code&gt; integration to bypass region-locked websites&lt;/li&gt;
&lt;li&gt;pump events into an SQS queue&lt;/li&gt;
&lt;li&gt;raise AWS Support tickets using your existing customer service solution&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Advantages
&lt;/h2&gt;

&lt;p&gt;A simple Lambda may seem innocuous at first, but each function comes with their own maintenance cost including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;maintaining the application code&lt;/li&gt;
&lt;li&gt;maintaining dependencies&lt;/li&gt;
&lt;li&gt;any CI/CD tooling around delivering that code&lt;/li&gt;
&lt;li&gt;performing runtime upgrades&lt;/li&gt;
&lt;li&gt;security scanning&lt;/li&gt;
&lt;li&gt;configuring monitoring and alerts (e.g. CloudWatch)&lt;/li&gt;
&lt;li&gt;configuring instrumentation (e.g. X-Ray)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Removing a Lambda means fewer resources to maintain, test and pay for.&lt;br&gt;
Latency is also reduced. There are less hops in the chain and the issue of cold starts disappears.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disadvantages
&lt;/h2&gt;

&lt;p&gt;There are however a number drawbacks to consider with this Lambdaless method. Probably most apparent is the fact that you can only integrate with a single service at a time. This limits the approach to simple integrations and rules out complex logic e.g. joins.&lt;/p&gt;

&lt;p&gt;Velocity, whilst offering some level of &lt;a href="https://velocity.apache.org/engine/devel/vtl-reference.html#directives"&gt;control flow&lt;/a&gt; such as &lt;code&gt;if/else&lt;/code&gt; and loops, as well as AWS's own extensions such as &lt;a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#util-template-reference"&gt;util functions&lt;/a&gt;, is somewhat of a niche language and introduces its own complexity over using your Lambda runtime language of choice (e.g. JavaScript, Python).&lt;/p&gt;

&lt;p&gt;This approach is also tightly coupled with API Gateway. The &lt;code&gt;AWS&lt;/code&gt; integration type and request/response mapping template approach is unique to API Gateway and therefore is less portable than Lambda application logic (which is easier to abstract from the Lambda environment itself).&lt;/p&gt;

&lt;p&gt;It also relies on "low-level" AWS APIs, which are less accessible and often sparsely documented compared to their corresponding SDK wrappers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/blogs/compute/using-amazon-api-gateway-as-a-proxy-for-dynamodb/"&gt;Using Amazon API Gateway as a proxy for DynamoDB&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/hackernoon/serverless-and-lambdaless-scalable-crud-data-api-with-aws-api-gateway-and-dynamodb-626161008bb2"&gt;Serverless and Lambdaless Scalable CRUD Data API with AWS API Gateway and DynamoDB&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Thanks&lt;/strong&gt; to &lt;a href="https://github.com/kahlos"&gt;Callum Vincent&lt;/a&gt; for reading drafts of this.&lt;/p&gt;

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