Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(events): fromApiDestinationAttributes import method #29943

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/aws-cdk-lib/aws-events-targets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,30 @@ const rule = new events.Rule(this, 'Rule', {
});
```

You can also import an existing connection and destination
to create additional rules:

```ts
const connection = events.Connection.fromEventBusArn(
this,
'Connection',
'arn:aws:events:us-east-1:123456789012:event-bus/EventBusName',
'arn:aws:secretsmanager:us-east-1:123456789012:secret:SecretName-f3gDy9',
);

const apiDestinationArn = 'arn:aws:events:us-east-1:123456789012:api-destination/DestinationName';
const destination = events.ApiDestination.fromApiDestinationArn(
this,
'Destination',
{ apiDestinationArn, connection },
);

const rule = new events.Rule(this, 'OtherRule', {
schedule: events.Schedule.rate(Duration.minutes(10)),
targets: [new targets.ApiDestination(destination)],
});
```

## Put an event on an EventBridge bus

Use the `EventBus` target to route event to a different EventBus.
Expand Down
41 changes: 40 additions & 1 deletion packages/aws-cdk-lib/aws-events/lib/api-destination.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Construct } from 'constructs';
import { HttpMethod, IConnection } from './connection';
import { CfnApiDestination } from './events.generated';
import { IResource, Resource } from '../../core';
import { ArnFormat, IResource, Resource, Stack } from '../../core';

/**
* The event API Destination properties
Expand Down Expand Up @@ -62,12 +62,51 @@ export interface IApiDestination extends IResource {
readonly apiDestinationArn: string;
}

/**
* The properties to import an existing Api Destination
*/
export interface ApiDestinationAttributes {
/**
* The ARN of the Api Destination
*/
apiDestinationArn: string;
/**
* The Connection to associate with the Api Destination
*/
connection: IConnection;
}

/**
* Define an EventBridge Api Destination
*
* @resource AWS::Events::ApiDestination
*/
export class ApiDestination extends Resource implements IApiDestination {
/**
* Create an Api Destination construct from an existing Api Destination ARN.
*
* @param scope The scope creating construct (usually `this`).
* @param id The construct's id.
* @param attributes The impported Api Destination attributes.
*/
public static fromApiDestinationArn(
scope: Construct,
id: string,
{ apiDestinationArn, connection }: ApiDestinationAttributes,
): IApiDestination {
const apiDestinationName = Stack.of(scope).splitArn(apiDestinationArn, ArnFormat.SLASH_RESOURCE_NAME).resourceName;
if (!apiDestinationName) {
throw new Error(`Could not extract Api Destionation name from ARN: '${apiDestinationArn}'`);
}

class Import extends Resource implements IApiDestination {
public readonly apiDestinationArn = apiDestinationArn;
public readonly apiDestinationName = apiDestinationName!;
public readonly connection = connection;
}

return new Import(scope, id);
}
/**
* The Connection to associate with Api Destination
*/
Expand Down
44 changes: 44 additions & 0 deletions packages/aws-cdk-lib/aws-events/test/api-destination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,47 @@ test('creates an api destination for an EventBus', () => {
Name: 'ApiDestination',
});
});

test('imports an api destination from its arn', () => {
// GIVEN
const stack = new Stack();
const connection = events.Connection.fromEventBusArn(
stack,
'Connection',
'arn:aws:events:us-east-1:123456789012:event-bus/EventBusName',
'arn:aws:secretsmanager:us-east-1:123456789012:secret:SecretName-f3gDy9',
);

// WHEN
const apiDestinationArn = 'arn:aws:events:us-east-1:123456789012:api-destination/DestinationName';
const destination = events.ApiDestination.fromApiDestinationArn(
stack,
'ApiDestination',
{ apiDestinationArn, connection },
);

// THEN
expect(destination.apiDestinationArn).toEqual('arn:aws:events:us-east-1:123456789012:api-destination/DestinationName');
expect(destination.apiDestinationName).toEqual('DestinationName');
});

test('throws if imported api destination ARN is invalid', () => {
// GIVEN
const stack = new Stack();
const connection = events.Connection.fromEventBusArn(
stack,
'Connection',
'arn:aws:events:us-east-1:123456789012:event-bus/EventBusName',
'arn:aws:secretsmanager:us-east-1:123456789012:secret:SecretName-f3gDy9',
);

// THEN
const apiDestinationArn = 'arn:aws:events:us-east-1:123456789012:api-destination';
expect(() => {
events.ApiDestination.fromApiDestinationArn(
stack,
'ApiDestination',
{ apiDestinationArn, connection },
);
}).toThrow("Could not extract Api Destionation name from ARN: 'arn:aws:events:us-east-1:123456789012:api-destination'");
});
Loading