Does SST.EventBus support APIDestinations? The <C...
# help
s
Does SST.EventBus support APIDestinations? The CDK EventBridge construct library supports
ApiDestination
, so I think it's supported by CDK
t
I think this wasn't available when we were making our construct but you should be able to add it right?
s
I think that's correct. All of the examples on

Serverless Patterns

are in SAM
I don't think CDK had support for API Destinations out of the gate
I'l dig into it and share what I find
f
Hey @Seth Geoghegan, u’d create an Api Destination target like this:
Copy code
import * as events from "aws-cdk-lib/aws-events";
import * as targets from "aws-cdk-lib/aws-events-targets";

const connection = new events.Connection(this, 'Connection', {
  authorization: events.Authorization.apiKey('x-api-key', SecretValue.secretsManager('ApiSecretName')),
  description: 'Connection with API Key x-api-key',
});

const destination = new events.ApiDestination(this, 'Destination', {
  connection,
  endpoint: '<https://example.com>',
  description: 'Calling <http://example.com|example.com> with API key x-api-key',
});

const target = new targets.ApiDestination(destination)
And then u can add the target to an existing rule or create a new rule.
s
Thanks @Frank! I've managed to get the basic API Destination working. I'm now trying to figure out how properly transform the event before Event Bridge passes the event to the APIDestination. Not only do I want to make an API request with credentials stored in Secrets Manager (got that working), but I need to add parameters to the body of the API request. I'm almost there!
OK, finally got it working. I struggled to determine how to transform an event like this
Copy code
{
  Detail: { "text": "I hope this works" },
  DetailType: "my.event.type",
  EventBusName: "my_event_bus",
  Source: "com.mytest",
}
Into an API request that looks like this:
Copy code
POST <https://someapi.com/blah>
Authorization: Bearer my-api-token-here
content-type: application/json

{
  "text": "I hope this works". // <---this is the request body
}
Here's how I got it working. Feels like a lot, but 🤷
Copy code
const secret = SecretValue.secretsManager("MyApiToken",{jsonField: 'my-token-key'})

 const connection = new events.Connection(this, "Connection", {
   authorization: events.Authorization.apiKey(
      "Authorization",
      secret
    ),
    description: "External API connection"
  });

 const destination = new events.ApiDestination(this, "Destination", {
      connection,
      endpoint: "<https://webhook.site/blahblahblah>",
      httpMethod: <http://events.HttpMethod.POST|events.HttpMethod.POST>,
      rateLimitPerSecond: 10,
      description: "Calling webhook with API key x-api-key",
 });

  // this is the Input Transformer configuration I struggled to get working
  const apiDestinationProps: targets.ApiDestinationProps = {
      event: RuleTargetInput.fromObject({
        text: events.EventField.fromPath('$.detail.text')
      })
  };

const myApiTarget = new targets.ApiDestination(destination, apiDestinationProps);

const rule = new events.Rule(this, "Rule", {
      eventBus: bus.eventBridgeEventBus, // bus is an SST EventBus
      eventPattern: {
        source: ["my_event_bus"],
        detailType: ["my.event.type"],
      },
      targets: [myApiTarget],
 });
t
just saw this yesterday
s
Oh yeah, that looks awesome! AWS has a similar functionality, but it's buried deep in the EventBridge console. I think what I struggled with the most was 1. understanding how to write an Input Transformer (this tool would definitely help) and 2. figuring out how to represent an Input Transformer in CDK. There are remarkably few examples laying around