Create Lambda layer

First create a lambda layer for our dependencies. Lambda layers are a powerful way of bundling Lambda dependencies in a way that makes them more easily reused. They can also improve the performance of Lambda functions by making it easier for dependencies to be pre-loaded prior to Lambda invocation. Finally, by putting all your dependencies in a layer, your actual Lambda code can be kept lean, which makes it a lot easier to edit and maintain, even in the AWS Management Console if you prefer.

Execute the following commands in your Cloud9 terminal to generate and publish the Lambda Layer to your AWS account.

mkdir -p ~/environment/ambSupplyChainConnector/fabric-client-layer/nodejs
cat <<EOT > ~/environment/ambSupplyChainConnector/fabric-client-layer/nodejs/package.json
{
  "name": "fabric-client-layer",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Carl Youngblood",
  "license": "Apache-2.0",
  "dependencies": {
    "aws-sdk": "^2.724.0",
    "fabric-client": "^1.4.10",
    "loglevel": "^1.6.8"
  }
}
EOT
cd ~/environment/ambSupplyChainConnector/fabric-client-layer/nodejs
nvm install lts/erbium
nvm use lts/erbium
npm install
cd ..
zip -r nodejs.zip nodejs
aws lambda publish-layer-version --layer-name fabric-client-layer --description "NodeJS lts/erbium layer with aws-sdk, fabric@1.4, and loglevel" --license-info "Apache-2.0" --compatible-runtimes "nodejs12.x" --zip-file fileb://$HOME/environment/ambSupplyChainConnector/fabric-client-layer/nodejs.zip

The process takes a few minutes to complete, as it installs many different NodeJS dependencies. When it’s finished, you should see some output from the layer publish command that looks like this.

{
    "Content": {
        "Location": "https://prod-04-2014-layers.s3.us-east-1.amazonaws.com/snapshots/123456789012/fabric-client-layer-36b82be1-ae4e-428e-938e-20ae0b31fc99?versionId=j6Ds5gB8rNuWgCyR89oOlWEW.ki.9PrL&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEJH%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJIMEYCIQCbTmqy59h4uazVt03rlZPpx5oiCRiahb8bearlxJjVZQIhANXf4k9WUc22fpV67Acjgc0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A25DCYHY37OC6X5H2%2F20200812%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=c920d296b186fcf304702866a506d70a05a881aeb50f5a42ce3f3213ea5d02d1",
        "CodeSha256": "kv/RihGqBawkUaZY4pcCul59onCMr/j+Gssf+iYTx9A=",
        "CodeSize": 32858516
    },
    "LayerArn": "arn:aws:lambda:us-east-1:123456789012:layer:fabric-client-layer",
    "LayerVersionArn": "arn:aws:lambda:us-east-1:123456789012:layer:fabric-client-layer:2",
    "Description": "NodeJS lts/erbium layer with aws-sdk, fabric@1.4, and loglevel",
    "CreatedDate": "2020-08-12T01:20:24.457+0000",
    "Version": 2,
    "CompatibleRuntimes": [
        "nodejs12.x"
    ],
    "LicenseInfo": "Apache-2.0"
}