GithubHelp home page GithubHelp logo

aws-samples / aws-serverless-lambda-layers Goto Github PK

View Code? Open in Web Editor NEW
22.0 3.0 8.0 545 KB

Sample serverless architecture showing an AWS Lambda function based on AWS Lambda Layers using the Serverless Framework (https://www.serverless.com) on AWS.

Home Page: https://github.com/aws-samples/aws-serverless-lambda-layers

License: MIT No Attribution

JavaScript 100.00%
aws lambda layers serverless-framework

aws-serverless-lambda-layers's Introduction

Develop & Deploy AWS Lambda Layers using Serverless Framework

Build Status Gitpod Ready-to-Code NPM version PyPI version NuGet version

Table of contents

  1. Install Serverless Framework
  2. Create a Serverless project
  3. Create a Lambda Function
  4. Deploy the Lambda Function
  5. Test the Lambda Function
  6. Exclude the node_modules from Lambda Function
  7. Deploy Lambda Function again
  8. Create Lambda Layer Serverless Project
  9. Modify Lambda Function to use Lambda Layer
  10. Cleanup

AWS Lambda Layers let you keep your deployment package small, which makes development easier. You can avoid errors that can occur when you install and package dependencies with your function code. A Lambda layer is an archive containing additional code, such as libraries, dependencies, or even custom runtimes. When you include a layer in a function, the contents are extracted to the /opt directory in the execution environment. You can include up to five layers per function, which count towards the standard Lambda deployment size limits.

Serverless Framework Open Source lets you develop and deploy serverless applications to AWS. The Serverless Framework CLI is the simplest way to develop infinitely scalable, pay-per-execution serverless applications. Following the AWS best practices, we will handle our application and the layers into two independent Serverless projects called:

  • lambda-service (will contain the Node function source code)
  • lambda-layer (will contain the lambda layers)

1. Install Serverless Framework

Install AWS Cloud9 Environment using the instructions here.

AWS Cloud9

Inside AWS Cloud9 environment, open a shell terminal and run the following:

npm install -g npm
npm i -g serverless
sls create --help

2. Create a Serverless project

mkdir serverless
cd serverless
sls create -t aws-nodejs -n lambda-service -p lambda-service

3. Create a Lambda Function

AWS Lambda Function

Replace handler.js with:

'use strict';
const moment = require("moment");
const dateNow = moment().format("MMM Do YY");

module.exports.hello = async event => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: dateNow
      }),
  };
};

Install the moment module.

cd lambda-service
npm init -y
npm i moment --save

Open serverless.yml under lambda-service and replace it with:

service: lambda-service

provider:
  name: aws
  apiGateway:
    shouldStartNameWithService: true
  runtime: nodejs12.x
  stage: dev
  region: us-east-1
  profile: default         

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          method: get
          path: hello

4. Deploy the Lambda Function

Inside the lambda-service directory, run the following commands:

sls package
sls deploy

5. Test the Lambda Function

Open the http api gateway endpoint in the browser, and the REST Api will return the current date in the MMM Do YY format.

6. Exclude the node_modules from Lambda Function

Replace the service: lambda-service with the following code inside serverless.yml file:

service: lambda-service
package:
  exclude:
    - node_modules/**

7. Deploy Lambda Function again

sls package
sls deploy

8. Test the Lambda Function again

Open the http api gateway endpoint in the browser, and this time you will get internal error.

9. Create Lambda Layer Serverless Project

AWS Lambda Function Inside the serverless directory, run the following commands:

sls create -t aws-nodejs -n lambda-layer -p lambda-layer

Replace the contents of serverless.yml under lambda-layer as:

service: lambda-layer

provider:
  name: aws
  runtime: nodejs12.x
  stage: dev
  region: us-east-1
  profile : default         

layers:
  MomentLayer:
    path: moment_layer
    description: "Moment Dependencies"

10. Create and deploy Lambda Layer

Open a shell prompt under lambda-layer directory

mkdir moment_layer && cd moment_layer
npm init -y
npm i moment --save
cd ../
sls package
sls deploy

Note down the AWS urn of the lambda layer that gets created.

11. Modify Lambda Function to use Lambda Layer

Open serverless.yml under lambda-service directory.

Replace it with:

service: lambda-service
package:
  exclude:
    - node_modules/**

provider:
  name: aws
  apiGateway:
    shouldStartNameWithService: true
  runtime: nodejs12.x
  stage: dev
  region: us-east-1
  profile: default         

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          method: get
          path: hello
    environment:
      NODE_PATH: "./:/opt/node_modules"
    layers:
      - arn:aws:lambda:us-east-1:xxxxxxxxxxx:layer:MomentLayer:1

Replace the ARN of lambda layer created in previous step.

12. Redeploy the Lambda Function

Open shell prompt in lambda-service directory

sls package
sls deploy

13. Retest the Lambda Fucntion

Open the http api gateway endpoint in the browser, and the REST Api will return the current date in the MMM Do YY format.

14. Cleanup

Inside the lambda-service directory, run:

sls remove

Inside the lambda-layer directory, run:

sls remove

Finally using the AWS Management Console, remove the AWS Cloud9 environment.

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

aws-serverless-lambda-layers's People

Contributors

amazon-auto avatar dependabot[bot] avatar ibuchh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.