Creating a Lambda Layer for GraphQL API queries, with AWS Amplify
2 min readJul 7, 2021
If you have to access your GraphQL API from your AWS Lambda Functions, this might be useful for you. It creates a signed request, assuming that your Lambda Function’s IAM Role has access to the resource.
First, let’s create our layer:
amplify function addSelect which capability you want to add: Lambda function (serverless function)
❯ Lambda layer (shared code & resource used across functions)Provide a name for your Lambda layer: graphQlChoose the runtime that you want to use: NodeJSThe current AWS account will always have access to this layer.
Optionally, configure who else can access this layer.
(Hit <Enter> to skip)
Then go to the layer’s lib folder:
cd amplify/backend/function/<layername>/lib/nodejs
Install the following packages:
npm install graphql --save
npm install graphql-tag --save
npm install url --save
npm install uuid --save
Now, go to the layer’s opt folder and create a file named graphQlRun.js with the following code:
cd amplify/backend/function/<layername>/opt
Now it’s time to test it in a Lambda Function. Let’s create a test function and run some GraphQL query.
$ amplify function addSelect which capability you want to add:❯ Lambda function (serverless function)
Lambda layer (shared code & resource used across functions)Provide an AWS Lambda function name: test
Choose the runtime that you want to use: NodeJS
Choose the function template that you want to use: Hello World
Do you want to configure advanced settings? Yes
Do you want to access other resources in this project from your Lambda function? Yes
Select the categories you want this function to have access to. api
Select the operations you want to permit on database Query, Mutation
Do you want to invoke this function on a recurring schedule? No
Do you want to enable Lambda layers for this function? YesProvide existing layers or select layers in this project to access from this function (pick up to 5): <select the layer you've created>
Select a version for graphQl: Always choose latest version
Do you want to configure environment variables for this function? No
Do you want to configure secret values this function can access? No
Do you want to edit the local lambda function now? Yes
Push the changes to the cloud.
$ amplify push --yes
Now run your test function so you can check the results. That’s it!