A super simple example GraphQL API that can be hosted on AWS Lambda.
src
contains the code to integrate theasync-graphql
crate with thelambda
crates for a serverless GraphQL experience. Both POST and GET queries are supported on any path..github
contains Renovate support for keeping dependencies up to date..github/workflows
contains a GitHub Actions workflow that will build and test the code on every push as well as deploy on pushes to main. There is also an integration test which runs after deploy.
This is set up for the most simple use-case: a function URL. You will need to make some tweaks to get this working with API Gateway.
- Install cargo-lambda
- Install Zig (needed by cargo-lambda)
- An AWS credentials file (for deploying only)
cargo lambda watch
will start the service lazily (only compiles when the first request comes in), then watch and reload your code on changes. The URL will behttp://localhost:9000/lambda-url/graphql-example
(where graphql-example is the name of the binary once you change it).- To deploy, first run
cargo lambda build --release --arm64
(for graviton processors) thencargo lambda deploy
. - To get the GitHub Actions to deploy on push to main, you need to set two GitHub Secrets for AIM credentials:
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
. - For the integration tests to work, fill in the
FUNCTION_URL
secret with the URL of the deployed function. This assumes that the same AWS credentials which can deploy the function can invoke it—if this isn't true, you'll need to tweak theintegration_test
job in.github/workflows/release.yml
. It also assumes that your function is secured with the AWS_IAM type of function security. You'll need to tweak this job as soon as you change the schema.
Here's an example IAM policy to use for GitHub secrets (both deploying and integration testing):
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"lambda:CreateFunction",
"lambda:UpdateFunctionCode",
"lambda:InvokeFunctionUrl",
"lambda:GetFunction"
],
"Resource": "<your function arn>"
}
]
}