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

后端 未结 3 779
别那么骄傲
别那么骄傲 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:16

    This is how I trigger an AppSync mutation using by making a simple HTTP-request, using axios.

    const AWS = require('aws-sdk');
    const axios = require('axios');
    
    exports.handler = async (event) => {    
        let result.data = await updateDb(event);
    
        return result.data;
    };
    
    function updateDb({ owner, thingName, key }){
        let req = new AWS.HttpRequest('https://xxxxxxxxxxx.appsync-api.eu-central-1.amazonaws.com/graphql', 'eu-central-1');
        req.method = 'POST';
        req.headers.host = 'xxxxxxxxxxx.appsync-api.eu-central-1.amazonaws.com';
        req.headers['Content-Type'] = 'multipart/form-data';
        req.body = JSON.stringify({
            "query":"mutation ($input: UpdateUsersCamsInput!) { updateUsersCams(input: $input){ latestImage uid name } }",
            "variables": {
                "input": {
                    "uid": owner,
                    "name": thingName,
                    "latestImage": key
                }
            }
        });
    
        let signer = new AWS.Signers.V4(req, 'appsync', true);
        signer.addAuthorization(AWS.config.credentials, AWS.util.date.getDate());
    
        return axios({
            method: 'post',
            url: 'https://xxxxxxxxxxx.appsync-api.eu-central-1.amazonaws.com/graphql',
            data: req.body,
            headers: req.headers
        });
    }
    

    Make sure to give the IAM-role your Lambda function is running as, permissions for appsync:GraphQL.

提交回复
热议问题