Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send signed HTTP request from AWS Lambda to AppSync GraphQL?

I am not sure how to send signed http request do AppSync GraphQL endpoint. There is no library for do that in AWS.

  • aws-amplify don't work because works only in browser, not in Lambda function.
  • aws-sdk for AppSync is only for admin usage, it doesn't have methods for call user side api

It is possible to make IAM signed HTTP request from AWS Lambda? (in some easy way)

like image 970
Michal Kalita Avatar asked Jul 09 '19 12:07

Michal Kalita


People also ask

How do I connect lambda to AppSync?

From the schema editor in the AWS AppSync console, on the right side, choose Attach Resolver for getPost(id:ID!): Post . Choose your Lambda data source. In the request mapping template section, choose Invoke And Forward Arguments. In the response mapping template section, choose Return Lambda Result.

What type of API is used for the AppSync service in AWS?

A single data API AWS AppSync securely connects your GraphQL API to data sources like AWS DynamoDB, RDS, OpenSearch, and Lambda. Adding caches to improve performance, authentication to secure your data, and client-side data stores that keep off-line clients in sync are just as easy.

Does AWS AppSync use API gateway?

You can use AWS AppSync as a single interface to access and combine data from multiple microservices in your application, even if they're running in different environments such as containers in a VPC, behind a REST API on Amazon API Gateway, or behind a GraphQL API on another AWS AppSync endpoint.


1 Answers

i would recommend reading this article: Backend GraphQL: How to trigger an AWS AppSync mutation from AWS Lambda,

quoting the author, https://stackoverflow.com/users/1313441/adrian-hall, we've:

GraphQL is routed over HTTPS. That means we can simulate the GraphQL client libraries with a simple HTTPS POST. Since we are using IAM, we need to sign the request before we deliver it. Here is my code for this:

// ... more code here
    // POST the GraphQL mutation to AWS AppSync using a signed connection
    const uri = URL.parse(env.GRAPHQL_API);
    const httpRequest = new AWS.HttpRequest(uri.href, env.REGION);
    httpRequest.headers.host = uri.host;
    httpRequest.headers['Content-Type'] = 'application/json';
    httpRequest.method = 'POST';
    httpRequest.body = JSON.stringify(post_body);

    AWS.config.credentials.get(err => {
        const signer = new AWS.Signers.V4(httpRequest, "appsync", true);
        signer.addAuthorization(AWS.config.credentials, AWS.util.date.getDate());

        const options = {
            method: httpRequest.method,
            body: httpRequest.body,
            headers: httpRequest.headers
        };

        fetch(uri.href, options)
// ... more code here

I've been using it as a template for all my Lambda->AppSync communication!

like image 92
oieduardorabelo Avatar answered Sep 18 '22 00:09

oieduardorabelo