Created
July 31, 2022 06:05
-
-
Save leegilmorecode/207633547fc8e4988141c6a8a250b9de to your computer and use it in GitHub Desktop.
Direct integration example between API Gateway and Amazon EventBridge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const eventBus: IEventBus = EventBus.fromEventBusName( | |
this, | |
"OnlineOrders", | |
"OnlineOrdersEventBus" | |
); | |
// event bridge options for the api gateway integration | |
const eventBridgeOptions: apigw.IntegrationOptions = { | |
credentialsRole: apigwRole, | |
requestParameters: { | |
"integration.request.header.X-Amz-Target": "'AWSEvents.PutEvents'", | |
"integration.request.header.Content-Type": | |
"'application/x-amz-json-1.1'", | |
}, | |
requestTemplates: { | |
"application/json": `{"Entries": [{"Source": "com.lee.pizza", "Detail":"$util.escapeJavaScript($input.json('$'))", "DetailType": "CreateOrder", "EventBusName": "${eventBus.eventBusName}"}]}`, | |
}, | |
integrationResponses: [ | |
{ | |
statusCode: "200", | |
responseTemplates: { | |
"application/json": "Created", | |
}, | |
}, | |
], | |
}; | |
// the create order endpoint persists a message directly on eventbridge | |
orders.addMethod( | |
"POST", | |
new apigw.Integration({ | |
type: apigw.IntegrationType.AWS, | |
uri: `arn:aws:apigateway:${cdk.Aws.REGION}:events:path//`, | |
integrationHttpMethod: "POST", | |
options: eventBridgeOptions, | |
}), | |
{ methodResponses: [{ statusCode: "200" }] } | |
); | |
const createOrdersDlq: sqs.Queue = new sqs.Queue(this, "CreateOrdersDlq", { | |
removalPolicy: RemovalPolicy.DESTROY, | |
queueName: "create-orders-dlq", | |
}); | |
new events.Rule(this, "CreateOrderLambdaProcessorRule", { | |
eventBus, | |
eventPattern: { source: ["com.lee.pizza"] }, | |
targets: [ | |
new targets.LambdaFunction(createOrderHandler, { | |
deadLetterQueue: createOrdersDlq, | |
}), | |
], | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment