Skip to content

Instantly share code, notes, and snippets.

@realugbun
Created October 1, 2021 12:42
Show Gist options
  • Select an option

  • Save realugbun/de1c2b285ece03d996d5192423875376 to your computer and use it in GitHub Desktop.

Select an option

Save realugbun/de1c2b285ece03d996d5192423875376 to your computer and use it in GitHub Desktop.
AWS Cloud Development Kit (CDK) EventBridge API Destinations
// ####### Event bus #######
// Create the bus
const bus = new events.EventBus(this, "myEventBus");
new cdk.CfnOutput(this, "BusName", {value: bus.eventBusName});
// ####### Connection #######
// Create connection for API Destination to use. This example uses basic auth. Check the docs for other options.
const myConnection = new events.CfnConnection(this, 'myConnection',{
authorizationType:"BASIC",
authParameters:{}
});
// The auth types are not part of the CDK so they must be added manually
myConnection.addOverride('Properties.AuthParameters', 'BasicAuthParameters');
myConnection.addOverride('Properties.AuthParameters.BasicAuthParameters', 'Username');
myConnection.addOverride('Properties.AuthParameters.BasicAuthParameters', 'Password');
// Pull the user and pass from env
myConnection.addOverride('Properties.AuthParameters.BasicAuthParameters.Username', String(process.env.API_USERNAME));
myConnection.addOverride('Properties.AuthParameters.BasicAuthParameters.Password', String(process.env.API_PASSWORD));
// ####### API Destination #######
// Create API Destination for event bus
const myAPIDestination = new events.CfnApiDestination(this, 'myAPIDestination', {
connectionArn: myConnection.attrArn,
httpMethod: 'PUT',
invocationEndpoint: "https://example.com/endpoint", // Only https connections are supported
invocationRateLimitPerSecond: 300,
});
// ####### Rule #######
// Create Iam role for the EventBridge Rule to invoke the API Destination
const myAPIDestinationInvokeRole = new iam.Role(this, 'myAPIDestinationInvokeRole', {
assumedBy: new iam.ServicePrincipal('events'),
inlinePolicies:{
"invokeAPIDestinations": new iam.PolicyDocument({
statements: [new iam.PolicyStatement({
actions: ['events:InvokeApiDestination'],
resources: [myAPIDestination.attrArn]
})]
})
}
}
);
// Create the EventBride rule
// the cdk does not have a rule for API Destinations so it must be created manually using the generic resource type
// cf https://docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html
// The proper format can be seen at https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutTargets.html#API_PutTargets_Example_2
new cdk.CfnResource(this, `myAPIDestinationInvokeRule`, {
type: 'AWS::Events::Rule',
properties: {
EventBusName: bus.eventBusName,
EventPattern: {source: ['mySource']},
State: 'ENABLED',
Targets: [
{
Arn: myAPIDestination.attrArn,
RoleArn: myAPIDestinationInvokeRole.roleArn,
InputPath: '$.detail', // This will take the detail of the event and send it as the body of the message
Id: 'myTarget'
}
]
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment