Manually sign AppSync URL to use in Lambda gives bad signature error

后端 未结 3 780
别那么骄傲
别那么骄傲 2021-01-03 13:48

In a Lambda, I would like to sign my AppSync endpoint with aws-signature-v4 in order to use it for a mutation.

The URL generated se

3条回答
  •  无人及你
    2021-01-03 14:20

    Adding an answer here because I had difficulty getting the accepted answer to work and I found an issue on the AWS SDK GitHub issues that said it's not recommended to use the AWS.Signers.V4 object in production. This is how I got it to work using the popular aws4 npm module that is recommended later on in the issue linked above.

    const axios = require('axios');
    const aws4 = require('aws4');
    
    const query = `
        query Query {
          todos {
            id,
            title
          }
        }`
    
    const sigOptions = {
            method: 'POST',
            host: 'xxxxxxxxxx.appsync-api.eu-west',
            region: 'eu-west-1',
            path: 'graphql',
            body: JSON.stringify({
                query
            }),
            service: 'appsync'
        };
    
    const creds = {
        // AWS access tokens
    }
    
    axios({
            url: 'https://xxxxxxxxxx.appsync-api.eu-west/graphql',
            method: 'post',
            headers: aws4.sign(sigOptions, creds).headers,
            data: {
                query
            }
        }).then(res => res.data))
    

提交回复
热议问题