Skip to content

Commit

Permalink
Document the aws jslib's EventBridge support
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Sep 28, 2023
1 parent c22b9c7 commit 5fbfefb
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/data/markdown/docs/20 jslib/01 jslib/01 aws.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ description: "aws is a library implementing APIs for accessing a selection of AW
The `aws` module is a JavaScript library that wraps around some Amazon AWS services API.

The library exposes a couple of configuration and client classes allowing to interact with a subset of AWS services in the context of k6 load test scripts:
- [EventBridge](/javascript-api/jslib/aws/eventbridgeclient): a class to send custom events to Amazon EventBridge.
- [KMSClient](/javascript-api/jslib/aws/kmsclient): a class to list and generate keys from the AWS Key Management Service.
- [S3Client](/javascript-api/jslib/aws/s3client): a class to list S3 buckets and the objects they contain, as well as uploading, downloading and deleting objects from them.
- [SecretsManagerClient](/javascript-api/jslib/aws/secretsmanagerclient): a class to list, get, create, update, and delete secrets from the AWS secrets manager service.
- [KMSClient](/javascript-api/jslib/aws/kmsclient): a class to list and generate keys from the AWS Key Management Service.
- [SystemsManagerClient](/javascript-api/jslib/aws/systemsmanagerclient): a class to fetch parameters from the AWS Systems Manager Service.
- [SQSClient](/javascript-api/jslib/aws/sqsclient): a class to list and send messages to SQS queues.
- [SystemsManagerClient](/javascript-api/jslib/aws/systemsmanagerclient): a class to fetch parameters from the AWS Systems Manager Service.
- [SignatureV4](/javascript-api/jslib/aws/signaturev4): a class to sign and pre-sign requests to AWS services using the [Signature V4](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html) algorithm.
- [AWSConfig](/javascript-api/jslib/aws/awsconfig/): a class is used by each client classes to provide them access to AWS credentials as well as configuration.

Expand All @@ -34,6 +35,7 @@ This documentation is for the last version only. If you discover that some code
| Library | Description |
| :--------------------------------------------------------------------- | :------------------------------------------------------------------- |
| [AWSConfig](/javascript-api/jslib/aws/awsconfig) | Class to configure AWS client classes. |
| [EventBridge](/javascript-api/jslib/aws/eventbridgeclient) | Client class to interact with AWS EventBridge service. |
| [KMSClient](/javascript-api/jslib/aws/kmsclient) | Client class to interact with AWS Key Management Service. |
| [S3Client](/javascript-api/jslib/aws/s3client) | Client class to interact with AWS S3 buckets and objects. |
| [SecretsManager](/javascript-api/jslib/aws/secretsmanagerclient) | Client class to interact with AWS secrets stored in Secrets Manager. |
Expand Down
65 changes: 65 additions & 0 deletions src/data/markdown/docs/20 jslib/01 jslib/01 aws/00 EventBridge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
title: 'EventBridgeClient'
head_title: 'EventBridgeClient'
description: 'EventBridgeClient allows interacting with AWS EventBridge service'
excerpt: 'EventBridgeClient class allows sending custom events to Amazon EventBridge so that they can be matched to rules.'
---

`EventBridgeClient` interacts with the AWS EventBridge service.

With it, you can send custom events to Amazon EventBridge. These events can then be matched to rules defined in EventBridge. For a full list of supported operations, see [Methods](#methods).

Both the dedicated `event-bridge.js` jslib bundle and the all-encompassing `aws.js` bundle include the `EventBridgeClient`.

### Methods

| Function | Description |
| :--------------------------------------------------------------------------------------------- | :------------------------------------------------- |
| [putEvents(input)](/javascript-api/jslib/aws/eventbridgeclient/eventbridgeclient-putevents/) | Send custom events to Amazon EventBridge |

### Throws

EventBridgeClient methods will throw errors in case of failure.

| Error | Condition |
| :----------------------- | :-------------------------------------------------------------------------- |
| InvalidSignatureError | when invalid credentials were provided or the request signature is invalid. |
| EventBridgeServiceError | when AWS replied to the requested operation with an error. |

### Examples

<CodeGroup labels={[]}>

```javascript
import { AWSConfig, EventBridgeClient } from 'https://jslib.k6.io/aws/0.10.0/event-bridge.js';

const awsConfig = new AWSConfig({
region: __ENV.AWS_REGION,
accessKeyId: __ENV.AWS_ACCESS_KEY_ID,
secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY,
sessionToken: __ENV.AWS_SESSION_TOKEN,
});

const eventBridge = new EventBridgeClient(awsConfig);

export default async function () {
const eventDetails = {
Source: 'my.custom.source',
Detail: { key1: 'value1', key2: 'value2' },
DetailType: 'MyDetailType',
Resources: ['arn:aws:resource1'],
};

const input = {
Entries: [eventDetails]
};

try {
await eventBridge.putEvents(input);
} catch (error) {
console.error(`Failed to put events: ${error.message}`);
}
}
```

</CodeGroup>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
title: 'EventBridgeClient.putEvents'
description: 'EventBridgeClient.putEvents sends custom events to Amazon EventBridge'
excerpt: 'EventBridgeClient.putEvents sends custom events to Amazon EventBridge'
---

`EventBridgeClient.putEvents` sends custom events to Amazon EventBridge so that they can be matched to rules.

### Parameters

| Parameter | Type | Description |
| :------------ | :-------------- | :----------------------------------------------------------------------------------------------------------------------- |
| input | [PutEventsInput](#puteventsinput) | An array of objects representing events to be submitted. |

#### PutEventsInput

| Parameter | Type | Description |
| :-------- | :-------------- | :----------------------------------------------------------------------------------------------------------------------- |
| Entries | [EventBridgeEntry](#eventbridgeentry)[] | An array of objects representing events to be submitted. |
| EndpointId | string (optional) | The ID of the target to receive the event. |

#### EventBridgeEntry

| Parameter | Type | Description |
| :-------- | :----- | :----------------------------------------------------------------------------------------------------------------------- |
| Source | string | The source of the event. |
| Detail | object | A JSON object containing event data. |
| DetailType | string | Free-form string used to decide what fields to expect in the event detail. |
| Resources | string[] (optional) | AWS resources, identified by Amazon Resource Name (ARN), which the event primarily concerns. |
| EventBusName | string (optional) | The event bus that will receive the event. If you omit this, the default event bus is used. Only the AWS account that owns a bus can write to it. |


### Returns

| Type | Description |
| :-------------- | :---------------------------------------------------------------------------------- |
| `Promise<void>` | A Promise that fulfills when the events have been sent to Amazon EventBridge. |

### Example

<CodeGroup labels={[]}>

```javascript
import { AWSConfig, EventBridgeClient } from 'https://jslib.k6.io/aws/0.10.0/event-bridge.js';

const awsConfig = new AWSConfig({
region: __ENV.AWS_REGION,
accessKeyId: __ENV.AWS_ACCESS_KEY_ID,
secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY,
sessionToken: __ENV.AWS_SESSION_TOKEN,
});

const eventBridge = new EventBridgeClient(awsConfig);
const eventEntry = {
Source: "my.source",
Detail: {
key: "value"
},
DetailType: "MyDetailType",
Resources: ["resource-arn"],
};

export default async function () {
await eventBridge.putEvents({
Entries: [eventEntry]
});
}
```

</CodeGroup>

0 comments on commit 5fbfefb

Please sign in to comment.