0

Trying to merge the principalId from Lambda Authorizer into the payload that will be sent to Event Bridge.

So far I've gotten close by transforming to map and re-creating the Json object. The problem is that this Json object is printed out as a key:value pair with no quotes, So there's an error thrown.

According to the docs, there's non $util.toJson($map) available, this seems to be only available for AppSync.

    const eventsAPI = new RestApi(this, 'eventsAPI', apigwOps);

    const LambdaAuth0Authorizer = new LambdaAuth0Authorizer(this, 'LambdaAuth0Authorizer', {
      env: {
        auth0Audience: '',
        auth0Domain: '',
      },
    });

    const eventTypeResource = eventsAPI.root.addResource('event');
    const options: IntegrationOptions = {
      credentialsRole: apigwRole,
      passthroughBehavior: PassthroughBehavior.NEVER,
      requestParameters: {
        'integration.request.header.X-Amz-Target': "'AWSEvents.PutEvents'",
        'integration.request.header.Content-Type': "'application/x-amz-json-1.1'",
      },

      requestTemplates: {
        'application/json': `
        #set ( $map = $util.parseJson($input.body) )
        #set ( $j = {
          "eventType": "$map['eventType']",
          "action": "$map['action']",
          "subject": "$map['subject']",
          "eventTime": $map['eventTime'],
          "actor": "$context.authorizer.principalId"
         } 
        )
                    {"Entries": 
                          [
                            {
                              "Source": "com.xyz", 
                              "Detail": "$util.escapeJavascript($j)", 
                              "Resources": [], 
                              "DetailType": "event", 
                              "EventBusName": "${eventBus.eventBusName}"
                            }
                          ]
                        }
          `,
      },
      integrationResponses: [
        {
          statusCode: '200',
          responseTemplates: {
            'application/json': '',
          },
        },
      ],
    };
    eventTypeResource.addMethod('POST', new Integration({
      type: IntegrationType.AWS,
      uri: `arn:aws:apigateway:${env.region}:events:path//`,
      integrationHttpMethod: 'POST',
      options: options,
    }), {
      authorizer: LambdaAuth0Authorizer.authorizer,
      methodResponses: [{ statusCode: '200' }],
      requestModels: { 'application/json': getEventModel(this, eventsAPI) },
      requestValidator: new RequestValidator(this, 'eventValidator', {
        restApi: eventsAPI,
        validateRequestBody: true,
      }),
    });

This generates:

{"Entries": 
  [
    {
      "Source": "com.uproarapi", 
      "Detail": "{eventType=FOLLOW, action=CREATE, subject=USER_777POIUY, eventTime=51644444444, actor=}", 
      "Resources": [], 
      "DetailType": "UpRoarEvent", 
      "EventBusName": "UpRoarEventBus"
    }
  ]
}

With an error:

{"Entries":[{"ErrorCode":"MalformedDetail","ErrorMessage":"Detail is malformed."}],"FailedEntryCount":1}
Esteban Rincon
  • 2,040
  • 3
  • 27
  • 44
  • already tried an ugly way: Adding more double quotes, but this throws Internal Server Error... – Esteban Rincon Dec 19 '22 at 15:43
  • Found a solution with existing question, marked this a dupe: https://stackoverflow.com/questions/68597006/add-or-update-property-on-json-object-with-mapping-template-aws-api-gateway – Esteban Rincon Dec 19 '22 at 18:42

0 Answers0