<?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: Victor Peralta</title>
    <description>The latest articles on DEV Community by Victor Peralta (@vprdev).</description>
    <link>https://dev.to/vprdev</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%2F542647%2F84de32af-398b-4635-aaf4-571b787c1a62.jpg</url>
      <title>DEV Community: Victor Peralta</title>
      <link>https://dev.to/vprdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vprdev"/>
    <language>en</language>
    <item>
      <title>How to reference Lambda Layers in imports using AWS Lambda with Typescript
</title>
      <dc:creator>Victor Peralta</dc:creator>
      <pubDate>Wed, 20 Jan 2021 01:49:21 +0000</pubDate>
      <link>https://dev.to/vprdev/how-to-reference-lambda-layers-in-imports-using-aws-lambda-with-typescript-5627</link>
      <guid>https://dev.to/vprdev/how-to-reference-lambda-layers-in-imports-using-aws-lambda-with-typescript-5627</guid>
      <description>&lt;p&gt;Using Lambda Layers and Typescript are great ways to improve your development experience when working with AWS Lambda functions, however,  you might run into an annoying issue that at first (for me, at least) doesn't seem to have an obvious solution: how to reference layers in your Typescript import statements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating an AWS Lambda Function
&lt;/h3&gt;

&lt;p&gt;Let's start off with a simple Typescript CDK project with one AWS Lambda function. When using AWS CDK or AWS SAM, I like to create a &lt;code&gt;src/&lt;/code&gt; directory for my actual code. Inside the &lt;code&gt;src/&lt;/code&gt; directory, let's create a new folder for our function, called &lt;code&gt;testLambda/&lt;/code&gt;, and an &lt;code&gt;index.ts&lt;/code&gt; file to put our lambda's code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-src/
  -testLambda/
    -index.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const handler =  async (event: any) =&amp;gt; {

    const response = {
        statusCode: 200,
        body: "Hello World"
    }

    return response;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating a layer
&lt;/h3&gt;

&lt;p&gt;Now, let's say that instead of "Hello World", we want to return just the first character in the string. This may be a common functionality that we may want to implement in many lambdas, so we decide to put it into a layer.&lt;/p&gt;

&lt;p&gt;Let's create a separate directory inside our &lt;code&gt;src/&lt;/code&gt; directory, &lt;code&gt;testLayer/&lt;/code&gt;, where we will store out layer's code, and then create a file &lt;code&gt;utils.ts&lt;/code&gt;, where our utility functions will live.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-src/
  -testLambda/
    -index.ts
  -testLayer/
    -utils.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is our utils file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const getFirstLetter = (inputString: string): string =&amp;gt; {
    return inputString.charAt(0);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usually, we would have to import our &lt;code&gt;getFirstLetter&lt;/code&gt; function following our directory structure like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { getFirstLetter } from "../testLayer/nodejs/utils"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, lambda layers have to be imported into functions with a  &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path"&gt;specific path&lt;/a&gt;  depending on what runtime you use. In the case of Node, custom code in lambda layers is stored in the &lt;code&gt;/opt/nodejs/&lt;/code&gt; path.&lt;/p&gt;

&lt;p&gt;We are required to import the function using the previous path:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { getFirstLetter } from "/opt/nodejs/utils"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After importing the function and using it to get our first letter out of "Hello World", our whole &lt;code&gt;index.ts&lt;/code&gt; file looks like this so far:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { getFirstLetter } from "/opt/nodejs/utils"

export const handler =  async (event: any) =&amp;gt; {

    const response = {
        statusCode: 200,
        body: getFirstLetter("Hello World")
    }

    return response;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although this is technically correct, Typescript doesn't like it when it can't find the file referenced in the import statement, thus, if we want to compile the project with &lt;code&gt;tsc&lt;/code&gt;, we will get this error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/testLambda/index.ts:1:32 - error TS2307: Cannot find module 'opt/nodejs/utils' or its corresponding type declarations.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This happens because we're not actually keeping this directory in our development machine. Luckily, to fix this issue involves only a small change in the Typescript configuration file for something called  &lt;a href="https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping"&gt;path mapping&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We must add the nodes "baseUrl" and "paths" to the tsconfig.json file, where we can specify the base directory of our project, relative to &lt;code&gt;tsconfig.json&lt;/code&gt;, and any path mappings, respectively:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    "baseUrl": ".",
    "paths": {
      "/opt/nodejs/*": ["src/testLayer/nodejs/*"]
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What this does, is tell the Typescript compiler that the files in &lt;code&gt;/opt/nodejs/&lt;/code&gt; are really in &lt;code&gt;/src/testLayer/nodejs&lt;/code&gt;, so the error when compiling goes away and you can keep enjoying all the benefits of Typescript and layers along with AWS Lambda.&lt;/p&gt;

&lt;p&gt;You can find the code for this guide in this repository&lt;br&gt;
&lt;a href="https://github.com/VictorPeralta/lambda-layers-typescript"&gt;https://github.com/VictorPeralta/lambda-layers-typescript&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
