Skip to content

Instantly share code, notes, and snippets.

@renegoretzka
Last active May 11, 2024 07:17
Show Gist options
  • Save renegoretzka/9ed754c4463b19c883ff8c98492ee5fe to your computer and use it in GitHub Desktop.
Save renegoretzka/9ed754c4463b19c883ff8c98492ee5fe to your computer and use it in GitHub Desktop.
Example to invoke AppSync from AWS Lambda

How to access an AppSync Endpoint from a Lambda function

Setup in project

Run amplify add function and go thru the manual configuration and add access to other resources. Add API here. Run amplify update api if you have a project already setup and configure IAM as an additional auth mode.

Setup in schema.graphql

Add the authorization as following to your type definition:

type Something
  @model
  @auth(
    rules: [
      { allow: private, provider: iam }
    ]
  ) {
  id: ID!
  something: String
}

This allows Lambda to fetch from this type.

Setup in lambda

  • Open the terminal in the amplify/backend/function/(nameOfYourFunction)/src directory in your newly created Lambda function.
  • Run npm install appsync-client graphql-tag

Change the code as you find in this gist's JavaScript file.

When you are done setting up your Lambda function to your desired result run amplify push to push your Lambda function to the AWS Cloud.

exports.getSomething = /* GraphQL */ `
query GetSomething($id: ID!) {
getSomething(id: $id) {
id
someField
}
}
`;
/* Amplify Params - DO NOT EDIT
* Note: This will be auto generated for you by the CLI!
API_APINAME_GRAPHQLAPIENDPOINTOUTPUT
API_APINAME_GRAPHQLAPIIDOUTPUT
ENV
REGION
Amplify Params - DO NOT EDIT */
const AppSyncClient = require('appsync-client').default
const gql = require('graphql-tag')
const config = {
apiUrl: process.env.API_APINAME_GRAPHQLAPIENDPOINTOUTPUT // Change the GraphQL Endpoint to your environment, which you can find in the Amplify Params
}
const client = new AppSyncClient(config)
const { getSomething } = require("./graphql.js"); // graphql.js exports the GraphQL query
const querySomething = async (id) => { // Example function
try {
const result = await client.request({
query: gql(getSomething), // use your graphql query here
variables: {
input: {
id
}
}
})
return result.getSomething;
} catch (error) {
console.log("Something went wrong in the querySomething function:", error)
}
}
exports.handler = async (event) => {
try {
return await querySomething("someRandomId"); // You can also use the arguments / input from the event.
} catch (error) {
console.log("Something went wrong in the handler:", error)
}
}
@charlieforward9
Copy link

Can somebody provide an example for how this is done with the AWS SDK for Javascript v3. The API has changed quite a bit and it is not straightforward on which class I should be using to create the request object.

@djom202
Copy link

djom202 commented May 11, 2024

Hi,

Currently I'm doing the same however I don't use IAM roles, I just use the api-key, that it will be possible to implement?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment