AWS Lambda + API Gateway for deploying GraphQL endpoints using the SAM framework. This is basically the SAM hello world app adapted for GraphQL endpoints.
- SAM_README.md autogenerated README from SAM framework
- template.yaml Lambda function and API gateway definitions for SAM framework
- src/ code to be included in Lambda functions
- helper/ some tools to make development easier
- tests/ pytest suite
# local app
PYTHONPATH=./src uvicorn helper.app:app --reload
# tests
PYTHONPATH=./src pytest tests
# redeploy
sam build && sam deploy --parameter-overrides "ParameterKey=Stage,ParameterValue=dev"
Initially deploy the dev stage for a certain region, stack-name, with admin cli user rights:
- (Go through template.yaml and find-replace
LambdaGraphql
if you want different names for those AWS resources) sam validate
sam build
sam deploy --parameter-overrides "ParameterKey=Stage,ParameterValue=dev" --guided
- Stack Name [sam-app]: stack-name
- AWS Region [us-east-1]: region
- Parameter Stage [dev]:
- Confirm changes before deploy [y/N]: n
- Allow SAM CLI IAM role creation [Y/n]: y
- LambdaGraphQLFunction may not have authorization defined, Is this okay? [y/N]: y
- Save arguments to configuration file [Y/n]: y
- SAM configuration file [samconfig.toml]:
- SAM configuration environment [default]:
- test
curl https://<id>.execute-api.<region>.amazonaws.com/dev/graphql/
(SAM output shows URL)
Will create a (git-ignored) samconfig.toml. Afterwards sam deploy
can be done without --guided
.
Make a IAM user or role for redeploying this stack.
Note: this user can do sam build
and sam deploy
but not sam validate
.
- Edit helper/config.py with
<account-id>
,<region>
,<stack-name>
- Run
python helper/policy_gen.py
to get policy JSON - Create new policy in AWS IAM with that policy JSON
- Create new AWS IAM user or role with CLI capabilities and attach that policy
- test by assuming that user/role (e.g.
aws configure
) - Try
sam build
,sam deploy --parameter-overrides "ParameterKey=Stage,ParameterValue=dev"
with it
Always re-deploy with that IAM user/role: sam build && sam deploy --parameter-overrides "ParameterKey=Stage,ParameterValue=dev"
.
Run pytests in tests/. See tests/conftest.py for config.
PYTHONPATH=./src pytest tests
There is a ASGI app in helper/app.py for easier GraphQL development locally.
PYTHONPATH=./src uvicorn helper.app:app --reload
Go to AWS CloudFormation and delete the entire stack. This should delete all resources (Lambda functions, API Gateways, roles, policies...).