Search code examples
amazon-web-servicesaws-lambdaserverless-frameworkaws-sam-cli

How can I utilize AWS::Serverless::LayerVersion in order to use external libraries in my AWS Lambda functions


I need to use external library that is located on my local file system in order to successfully execute my Lambda function. Using AWS SAM framework I found out that this can be done by specifying AWS::Serverless::LayerVersion resource.

What I am not sure is how does this exactly work and how do I specify path to my external library. Do I first need to deploy my external library to S3 bucket or?


Solution

  • You need to deploy the jar on layer in AWS Lambda layers section

    AWS Lambda Layers :

    You can configure your Lambda function to pull in additional code and content in the form of layers. A layer is a ZIP archive that contains libraries, a custom runtime, or other dependencies. With layers, you can use libraries in your function without needing to include them in your deployment package.

    Following are the steps to use AWS lambda layers

    • Write a Lambda layer code
    • Package Lambda layer
    • Deploy Lambda layer
    • Attached a layer to function Call a method
    • Verify the results

    Once you complete writing your function make sure the pom.xml contains the artifacts and maven-shade-plugin

    <groupId>java-lambda-layer</groupId>
    <artifactId>java-lambda-layer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build> 
    <plugins> 
    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-shade-plugin</artifactId> 
    <version>2.3</version> 
    <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> </configuration> 
    <executions> 
    <execution> 
    <phase>package</phase> 
    <goals> 
    <goal>shade</goal> 
    </goals> 
    </execution> 
    </executions> 
    </plugin> 
    </plugins> 
    </build>
    

    Run Maven

     mvnclean install and package
    

    Please read further on following link