Serverless Framework Deployment of safe-settings on AWS
August 10, 2022 Ā· View on GitHub
safe-settings receives HTTP POST Webhook payload. This deployment uses @probot/adapter-aws-lambda-serverless to deploy the GitHub App written in Node.js running on AWS Lambda and using the Serverless Framework.
This App does not include any kind of persistence (database). For more advanced examples, check out the serverless/examples repository which includes Typescript, Mongo, DynamoDB and other examples.
Usage
Pre-Requisites
In order to deploy the app from you local environment, follow the Serverless user guide for AWS.
If you use this example as a template, make sure to update serverless.yml and set new values for
serviceapporg
Make sure to create the following parameters on https://app.serverless.com:
APP_IDPRIVATE_KEYWEBHOOK_SECRET
NOTE:
Make sure you increase the timeout for the Lambda function to the MAX (15 Mins). Follow the instructions here
CD
For continuous deployment via GitHub action, copy probot/example-aws-lambda-serverless repository's deploy workflow and create the following secrets:
SERVERLESS_ACCESS_KEY- You can create a Serverless access key athttps://app.serverless.com/<your org>/settings/accessKeysAWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY- you will likely find your AWS credentials in~/.aws/credentials
Debug via unit tests
- Intall nyc and mocha:
npm install -g nyc mocha - From the VSCode
RUN AND DEBUGmenu selectMochaand click the green arrow to start debugging.
Debug by launching probot locally and sending it a payload
- Point your GitHub app to your local using something like smee.io
- Copy .env-sample to .env and populate with values specific for your GitHub app. For the
PRIVATE_KEYreplace newlines with\\nto make the string value a single line. - From the VSCode
RUN AND DEBUGmenu selectLaunch Probotand click the green arrow to start debugging.
Deployment
$ serverless package
$ serverless deploy
After deploying, you should see output similar to:
Deploying safe-settings to stage dev (us-east-1)
ā Service deployed to stack safe-settings-dev (152s)
dashboard: https://app.serverless.com/yadhav/apps/safe-settings/safe-settings/dev/us-east-1
endpoint: POST - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/api/github/webhooks
functions:
webhooks: safe-settings-dev-webhooks (9 MB)
Note: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to http event docs.
Invocation
After successful deployment, you can call the created application via HTTP:
curl -X POST --header @headers.txt --data @data.json https://xxxxxxx.execute-api.us-east-1.amazonaws.com/api/github/webhooks
Which should result in response similar to the following (removed input content for brevity):
{"ok":true}
Local development
You can invoke your function locally by using the following command:
serverless invoke local --function hello
Which should result in response similar to the following:
{
"statusCode": 200,
"body": "{\n \"message\": \"Go Serverless v3.0! Your function executed successfully!\",\n \"input\": \"\"\n}"
}
Alternatively, it is also possible to emulate API Gateway and Lambda locally by using serverless-offline plugin. In order to do that, execute the following command:
serverless plugin install -n serverless-offline
It will add the serverless-offline plugin to devDependencies in package.json file as well as will add it to plugins in serverless.yml.
After installation, you can start local emulation with:
serverless offline
To learn more about the capabilities of serverless-offline, please refer to its GitHub repository.