Skip to content

Instantly share code, notes, and snippets.

@leegilmorecode
Created December 14, 2022 18:21
Show Gist options
  • Save leegilmorecode/dcf53dcc88b481aebd5ca1cb568f54c2 to your computer and use it in GitHub Desktop.
Save leegilmorecode/dcf53dcc88b481aebd5ca1cb568f54c2 to your computer and use it in GitHub Desktop.
Direct integration between Amazon API Gateway and Amazon EventBridge
// .. creation of the eventbus above
const sharedEdaApi: apigw.RestApi = new apigw.RestApi(
this,
'SharedEdaApi',
{
description: 'shared eda api',
restApiName: 'shared-eda-api',
deploy: true,
deployOptions: {
stageName: 'prod',
dataTraceEnabled: true,
loggingLevel: apigw.MethodLoggingLevel.INFO,
tracingEnabled: true,
metricsEnabled: true,
},
}
);
const externalDomainEvents: apigw.Resource =
sharedEdaApi.root.addResource('events');
const apigwRole: iam.Role = new iam.Role(this, 'ApigwSharedEventBusRole', {
assumedBy: new iam.ServicePrincipal('apigateway.amazonaws.com'),
inlinePolicies: {
putEvents: new iam.PolicyDocument({
statements: [
new iam.PolicyStatement({
actions: ['events:PutEvents'],
resources: [sharedEventBus.eventBusArn],
}),
],
}),
},
});
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": "$util.escapeJavaScript($input.path('$.metadata.source'))", "Detail":"$util.escapeJavaScript($input.json('$'))", "DetailType": "$util.escapeJavaScript($input.path('$.metadata.eventName'))", "EventBusName": "${sharedEventBus.eventBusName}"}]}`,
},
integrationResponses: [
{
statusCode: '200',
responseTemplates: {
'application/json': 'Created',
},
},
],
};
externalDomainEvents.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' }] }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment