Showcase how to use a custom authoriser to attach api key
This project uses Serverless Node.js Starter that includes serverless-webpack plugin, Babel, and Mocha. It supports:
- ES7 syntax in your handler functions
- Use async/await
- And much more!
- Run API Gateway locally
- Use
yarn serve
- Use
- Support for unit tests
- Run
yarn test:unit
to run your tests
- Run
- Sourcemaps for proper error messages
- Error message show the correct line numbers
- Works in production with CloudWatch
- Automatic support for multiple handler files
- No need to add a new entry to your
webpack.config.js
- No need to add a new entry to your
- Add environment variables for your stages
- Yarn
curl -o- -L https://yarnpkg.com/install.sh | bash
yarn bash completion
- https://github.com/dsifford/yarn-completion
# Install dependencies
yarn install
customDomain:
domainName: shane.shanelee.xyz
certificateName: '*.shanelee.xyz'
basePath: ''
stage: ${self:provider.stage}
createRoute53Record: true
endpointType: 'regional'
To create the custom domain
Make sure and change the values to suit your needs
sls create_domain --stage dev
To spin up a local dev server that will more closely match the API Gateway endpoint/experience:
yarn serve
Automated Dependency Updates
Renovate runs continuously to detect the latest available versions. And automagicaly creates PR on your github project with changelog and release notes.
For more info and how to authorise the github app check out onboarding guide
Assuming you've already set up your default AWS credentials
yarn deploy:dev
will deploy to "dev" environment.
After you've deployed, the output of the deploy script will give you the API endpoint for your deployed function(s), and most importantly the api key value
Copy this value into the custom authoriser variable usageIdentifierKey
located at ./src/authoriser.js
the Lambda authorizer Lambda function must also return an API key of the usage plan as the usageIdentifierKey property value
Under the API Key Source section in the Settings pane for API Gateway console, choose AUTHORIZER
from the drop-down list. Save changes and then you need to redeploy your service
yarn deploy:dev
The client can now call the API key-required methods without explicitly supplying any API key. The authorizer-returned API key is used automatically. So you should be able to test the deployed API via that URL.
$ curl -H 'Authorization: Bearer xxx' 'https://xyz.execute-api.us-east-1.amazonaws.com/dev/hello'
{"message":" Hello world Your function executed successfully! (with a delay)"}
See.. No API Key Needed!!