如何将已签名的 HTTP 请求从 AWS Lambda 发送到 AppSync GraphQL?

Mic*_*ita 7 javascript amazon-web-services aws-lambda graphql aws-appsync

我不确定如何将签名的 http 请求发送到 AppSync GraphQL 端点。AWS 中没有用于执行此操作的库。

  • aws-amplify 不起作用,因为仅适用于浏览器,而不适用于 Lambda 函数。
  • aws-sdk AppSync 仅供管理员使用,它没有调用用户端 api 的方法

是否可以从 AWS Lambda 发出 IAM 签名的 HTTP 请求?(以某种简单的方式)

oie*_*elo 5

我建议阅读这篇文章:后端 GraphQL:如何从 AWS Lambda 触发 AWS AppSync 突变

引用作者/sf/users/91940901/,我们已经:

GraphQL 通过 HTTPS 路由。这意味着我们可以使用简单的 HTTPS POST 模拟 GraphQL 客户端库。由于我们使用的是 IAM,因此我们需要在提交请求之前对其进行签名。这是我的代码:

// ... 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
Run Code Online (Sandbox Code Playgroud)

我一直将它用作我所有 Lambda->AppSync 通信的模板!