serverless-plugin-greengrass v1.3.4
Serverless Greengrass
A serverless plugin to deploy functions to Greengrass Group.
This plugin will create:
- New AWS::Greengrass::FunctionDefinition with project's Lambdas declared.
- New AWS::Greengrass::SubscriptionDefinition with project's events subscriptions.
- New AWS::Greengrass::GroupVersion to associate the new Function Definition with your existing Greengrass Group. Deploy phase will trigger a new deployment with the new Group Version declared.
Requirements
Create a Greengrass Group and a Greengrass Core and configure your device to connect to it.
Usage
Installation
$ npm install serverless-plugin-greengrass --save-devor using yarn
$ yarn add serverless-plugin-greengrassAdd this plugin to your serverless.yml file:
plugins:
- serverless-plugin-greengrassConfiguration
Minimal required configuration:
custom:
greengrass:
groupId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx # Greengrass Group id
functions:
myfunction:
handler: tasks/door.handler(in order to retrieve group id got to AWS Console, then got to "IoT Greengrass" service, select the Greengrass Groups and under "Setting" menu you will find the "Group ID" section)
Advanced configuration:
custom:
greengrass:
autoDeploy: true # set to "false" to disable automatic deploy after "sls deploy"
deployTimeout: 30 # deploy timeouts in seconds, default 30
groupId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
defaults:
pinned: false # check if is a long running or on-demand
memorySize: 16384, # 16 MB expressed in KB
timeout: 6 # function timeout
encodingType: json # The expected encoding type of the input payload, can be binary or json.
accessSysfs: false # allowed to access the host's /sys folder
environment:
myVarA: 'myValueA' # deployed to Greengrass for all functions
functions:
myfunction:
handler: tasks/door.handler
greengrass:
handler: tasks/door.handlerIot # override handler for Greengrass deployed function
pinned: true # override default values
environment:
myVarC: 'myValueC' # deployed to Greengrass only for this functionPlease note: memorySize must be expressed in KB (unlike normal Lambdas expressed in MB) and greater or equal to 2048 (2MB). For more infos check FunctionConfiguration documentation.
Include only specific functions:
custom:
greengrass:
groupId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
include:
- myfunctionA # Only function "myfunctionA" will be deployed to Greengrass
functions:
myfunctionA:
handler: tasks/door.handler
myfunctionB:
handler: tasks/door.handler
myfunctionC:
handler: tasks/door.handlerExclude functions:
custom:
greengrass:
groupId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
exclude:
- myfunctionB # Only function "myfunctionA" and "myfunctionC" will be deployed to Greengrass
functions:
myfunctionA:
handler: functions/myfunctionA.handler
myfunctionB:
handler: functions/myfunctionB.handler
myfunctionC:
handler: functions/myfunctionC.handlerFunctions resources ids and permissions (locals / machine learning / secret):
custom:
greengrass:
groupId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
defaults:
resources: # available for all functions
- xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:ro # resource id with only read permission
- xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx # only, resource id, permission by default will be "ro"
functions:
myfunctionA:
handler: tasks/door.handler
greengrass:
resources:
- xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:rw # resource id with read and write permissionin order to retrieve the resource id got to AWS Console, then got to "IoT Greengrass" service, select the Greengrass Groups and under "Resources" menu you will find all available resources. Select resource you want to add and grab the last part of URL:
https://<your region>.console.aws.amazon.com/iot/home?region=<your region>#/greengrass/groups/<your group id>/resources/<resource id>
Subscription management:
custom:
greengrass:
groupId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
subscriptions: # global subscription
- source: arn:aws:iot:us-east-1:123456789012:thing/MyLightBulb # a Lambda function ARN, a connector ARN, "cloud" or "GGShadowService"
target: arn:aws:greengrass:us-east-1::/connectors/TwilioNotifications/versions/3
subject: /test/topic/
functions:
myfunctionA:
handler: functions/myfunctionA.handler
greengrass:
subscriptions: # leave target empty to use current function arn
- source: "GGShadowService"
subject: /test/topic/
myfunctionB:
handler: functions/myfunctionB.handler
greengrass:
subscriptions: # leave source empty to use current function arn
- target: arn:aws:iot:us-east-1:123456789012:thing/MyLightBulb
subject: /test/topic/
myfunctionC:
handler: functions/myfunctionC.handler
greengrass:
subscriptions: # multiple subscription types are supported
- source: arn:partition:service:region:account-id:resource-id
subject: /test/topic/a/
- target: "cloud"
subject: /test/topic/b/For more info about subscription value check AWS documentation.
Deploy
Execute a simple Serverless deploy, with autoDeploy enabled:
custom:
greengrass:
autoDeploy: true # or leave empty$ serverless deployServerless: Packaging service...
Serverless: Excluding development dependencies...
Greengrass: Loading functions... # <--- here plugin will load functions configurations
Greengrass: Creating new Group Version... # <--- here add a new Group Version to CloudFormation template
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service example.zip file to S3 (2.42 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
...............................
Serverless: Stack update finished... # <--- CloudFormation Stack deployed the new Group Version
Service Information
Greengrass: Creating new deployment for version xxxxxxxxxx... # <--- here plugin will execute a Greengrass deploy
Greengrass: Checking deploy progress...
........................ # <--- wait until Greengrass deploy is completed
Greengrass: Deploy successfully executed. # <--- all functions are deployed to your Greengrass Group
Serverless: Removing old service artifacts from S3...
Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.Manually execute deploy, with autoDeploy disabled:
custom:
greengrass:
autoDeploy: false$ serverless deploy
$ serverless greengrass deployRedeploy
To redeploy latest version run:
$ serverless greengrass redeployDelete/Reset
Execute a simple Serverless remove, with autoDeploy enabled:
custom:
greengrass:
autoDeploy: true # or leave empty$ serverless removethis will also run a reset deployment operations against Greengrass group.
Manually execute reset, with autoDeploy disabled:
custom:
greengrass:
autoDeploy: false$ serverless remove
$ serverless greengrass resetDebug
Set DEBUG environment variable to "yes" to enable debug log:
export DEBUG=yes
$ serverless greengrass deploy # will be printed a more verbose logExtra tips
Setup Raspberry Pi
- Download latest version of Raspbian Lite version from official download page
- Extract
.imgfile from previously download zip archive - Build SD card using Etcher, selecting previously extracted
.imgfile - Mount
bootpartition created on SD card - Create a new file
wpa_supplicant.confwith you WiFi credentials (edit info between«):
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=«your_ISO-3166-1_two-letter_country_code»
network={
ssid="«your_SSID»"
psk="«your_PSK»"
key_mgmt=WPA-PSK
}- Create a new empty file
sshwithout extensions in order to enable SSH service - Insert SD card into Raspberry Pi and boot it up
- Check into your modem info page a WiFi newly connected device called "raspberry" and get the IP
- Connect to Raspberry from your computer using SSH
ssh pi@<raspberry ip>- Login using
raspberrydefault password - Install and setup Greengrass:
export AWS_ACCESS_KEY_ID=«your_access_key»
export AWS_SECRET_ACCESS_KEY=«your_secret_access_key»
sudo su
wget -q -O ./gg-device-setup-latest.sh https://d1onfpft10uf5o.cloudfront.net/greengrass-device-setup/downloads/gg-device-setup-latest.sh && chmod +x ./gg-device-setup-latest.sh && sudo -E ./gg-device-setup-latest.sh bootstrap-greengrass-interactive- Automatically starting AWS Greengrass on a Raspberry Pi on system boot using this guide
TODO
- Allow to create and provision a new Greengrass Groups
- Deploy to multiple Greengrass Groups
- Add Greengrass Groups Subscription support
- Allow to use resource name instead of id (separated by local, machine learning and secret)