Search code examples
amazon-web-servicesaws-api-gatewayamazon-sqs

API Gateway -> SQS HTTP POST MessageAttributes


I have an API gateway setup which sends to SQS which fires a Lambda, I am trying to pass message attributes to the SQS but when I hit the endpoint in postman I keep getting a 400 Bad Request.. what is the right way to send the attributes over a JSON POST body

here is body from postman (have tried a few options based on this link)

    "message": "Message",
   "MessageAttributes": {
    "Name": "Name",
      "Type": "String",
      "Value": "my value"
    
   }
}

Here is how API Gateway is configured

enter image description here


Solution

  • Incase someone stumbles on this later here is worked from the CDK side

    let intergation = new apiGateway.CfnIntegration(this, 'Integration', {
          apiId: props.httpApi.httpApiId,
          payloadFormatVersion: '1.0',
          integrationType: 'AWS_PROXY',
          credentialsArn: apigwRole.roleArn,
          integrationSubtype: 'SQS-SendMessage',
          requestParameters: {
            QueueUrl: sqsqueue.queueUrl,
            MessageBody: '$request.body',
            MessageAttributes: '$request.body.MessageAttributes'
          }
        })
    
        new apiGateway.CfnRoute(this, 'Route', {
          apiId: props.httpApi.httpApiId,
          routeKey: apiGateway.HttpRouteKey.with('/url/foo', apiGateway.HttpMethod.POST).key,
          target: `integrations/${intergation .ref}`
        }).addDependsOn(intergation);
    
    

    and the cloudformation

     MessageBody: $request.body
     MessageAttributes: $request.body.MessageAttribute
    

    then in post man the POST body content type as application/json

    {
       "message": "Message",
       "MessageAttributes": {
        "Attributes": {
          "DataType": "String",
          "StringValue": "my value"
        }
       }
    }
    

    the lamba would log out both separate for each Record from the event body

        Records: [
        {
         ....
          body: 'Message',
          attributes: [Object],
          messageAttributes: [Object]
      
        }
      ]
    }
    
    

    the messageAttributes object from above:

        {
      Attributes: {
        stringValue: 'my value',
        stringListValues: [],
        binaryListValues: [],
        dataType: 'String'
      }
    }
    

    This is using AWS API Gateway v2 HTTP API also