Created
December 14, 2022 18:33
-
-
Save leegilmorecode/55ee492fa1beb7023af4578506eb23ea to your computer and use it in GitHub Desktop.
Example of using Amazon EventBridge API Destinations to integrate with an external REST API
This file contains 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
// create the orders connection for the api destination | |
const externalOrdersConnection: events.Connection = new events.Connection( | |
this, | |
'ExternalOrdersApiDestinationsConnection', | |
{ | |
authorization: events.Authorization.apiKey( | |
'x-api-key', | |
SecretValue.unsafePlainText('SuperSecretKey!12345') // this is for the demo only | |
), | |
description: 'External Orders API Destination Connection', | |
connectionName: 'ExternalOrdersApiDestinationsConnection', | |
} | |
); | |
// create the api destination for the external orders connection | |
const externalOrdersDestination: events.ApiDestination = | |
new events.ApiDestination(this, 'ExternalOrdersDestination', { | |
connection: externalOrdersConnection, | |
endpoint: `${externalApi}/orders/`, | |
description: 'The api destination for our external orders api', | |
rateLimitPerSecond: 50, | |
httpMethod: events.HttpMethod.POST, | |
apiDestinationName: 'ExternalOrdersDestination', | |
}); | |
// create the target rule for the api destination | |
new events.Rule(this, 'ExternalOrdersApiDestinationsRule', { | |
eventBus: sharedEventBus, | |
ruleName: 'ExternalOrdersApiDestinationsRule', | |
description: 'Rule for the external orders API Destination', | |
eventPattern: { | |
source: ['com.internal.orders'], | |
detailType: ['OrderCreated'], // when an order is created in the internal system | |
}, | |
targets: [ | |
new targets.ApiDestination(externalOrdersDestination, { | |
retryAttempts: 10, | |
event: events.RuleTargetInput.fromEventPath('$.detail'), // we only want to pass the http body as the detail | |
headerParameters: {}, | |
queryStringParameters: {}, | |
maxEventAge: Duration.minutes(60), | |
deadLetterQueue: new sqs.Queue(this, 'external-orders-api-dlq', { | |
removalPolicy: RemovalPolicy.DESTROY, | |
queueName: 'external-orders-api-dlq', // we ensure any failures go to a dead letter queue | |
}), | |
}), | |
], | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment