Deploy your own npm packages for Alexa as Lambda Layer by Serverless Framework

In the many cases, we’re using similar packages to build own Alexa Skill by AWS Lambda. We have own pack […]

広告ここから
広告ここまで

目次

    In the many cases, we’re using similar packages to build own Alexa Skill by AWS Lambda.
    We have own package.json or npm install commands lists and run them before starting to build the skill.

    But for now, we don’t have to do the repeating tasks, because we can easy to import these packages by AWS Lambda Layers.

    In this posts, we can learn how to deploy AWS Lambda Layers by Serverless Framework.

    Goal

    We can launch an AWS Lambda for Alexa without running npm i -S ask-sdk.

    Prepare

    To begin, we need to install the latest Serverless Framework. Please run npm i -g serverless@latest to install new it.

    Make directories

    To adding own library into the Layer, we have to place it to the specified place.

    Including Library Dependencies in a Layer

    Today, We will put a npm library into the Layer. So we’ll create the following directories structures.

    $ tree -L 3
    .
    ├── files
    │   └── nodejs
    │       ├── node_modules
    │       ├── package.json
    │       └── yarn.lock
    └── serverless.yml

    And the commands it the following.

    $ touch serverless.yml
    $ mkdir -p files/nodejs
    $ cd files/nodejs
    $ npm init -y
    $ yarn add ask-sdk ask-utils

    Making the serverless.yml

    In the Serverless Framework, we can define the Layer like the following example.

    service: ask-sdk-layers
    frameworkVersion: ">=1.34.0"
    provider:
      name: aws
      runtime: nodejs8.10
    
    layers:
      helloLayer:
        description: ASK SDK Packages
        path: files
        compatibleRuntimes:
          - nodejs8.10

    The frameworkVersion property is not necessary, but the older serverlesscommand will not work well. So to define it is more kindly for other member.

    Deploy

    After defining the YAML file, we can deploy by sls deploycommand.

    Service Information
    service: ask-sdk-layer
    stage: dev
    region: us-east-1
    stack: ask-sdk-layers-dev
    api keys:
      None
    endpoints:
      None
    functions:
      None
    layers:
      helloLayer: arn:aws:lambda:us-east-1:99999999:layer:helloLayer:1

    You can put the layer into your Lambda Function to put the ARN that shows at layers.

    Using layer from the Serverless Framework Project

    Finally, we’re trying to use the layer from an AWS Lambda deploying by Serverless Framework. We can put the Layer by following properties.

    service: first-alexa
    provider:
      name: aws
      runtime: nodejs8.10
    
    functions:
      hello:
        handler: index.handler
        layers:
          - arn:aws:lambda:us-east-1:99999999:layer:helloLayer:4

    And if we define the same stack, we can use CloudFormation function like this.

    service: practice-of-layer
    frameworkVersion: ">=1.34.0"
    provider:
      name: aws
      runtime: nodejs8.10
    
    layers:
      hello:
        description: My first layer
        path: files
        compatibleRuntimes:
          - nodejs8.10
    
    functions:
      hello:
        handler: index.handler
        layers:
          - {Ref: HelloLambdaLayer}

    広告ここから
    広告ここまで

    Random posts

    Home
    Search
    Bookmark