Skip to content

Commit adb44f4

Browse files
docs(events-targets): add documentation of invoking AwsApi method (#35309)
### Issue # (if applicable) None ### Reason for this change The `targets.AwsApi()` method enables us to call AWS API from EventBridge directly but there is no documentation about this feature. ### Description of changes Update README.md ### Describe any new or updated permissions being added None ### Description of how you validated changes None ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 8400b2b commit adb44f4

File tree

1 file changed

+64
-2
lines changed
  • packages/aws-cdk-lib/aws-events-targets

1 file changed

+64
-2
lines changed

packages/aws-cdk-lib/aws-events-targets/README.md

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Currently supported are:
1515
- [Start a StepFunctions state machine](#start-a-stepfunctions-state-machine)
1616
- [Queue a Batch job](#queue-a-batch-job)
1717
- [Invoke an API Gateway REST API](#invoke-an-api-gateway-rest-api)
18+
- [Invoke an AWS API](#invoke-an-aws-api)
1819
- [Invoke an API Destination](#invoke-an-api-destination)
1920
- [Invoke an AppSync GraphQL API](#invoke-an-appsync-graphql-api)
2021
- [Put an event on an EventBridge bus](#put-an-event-on-an-eventbridge-bus)
@@ -333,6 +334,67 @@ declare const rule: events.Rule;
333334
rule.addTarget(new targets.ApiGatewayV2(httpApi));
334335
```
335336

337+
## Invoke an AWS API
338+
339+
Use the `AwsApi` target to make direct AWS API calls from EventBridge rules. This is useful for invoking AWS services that don't have a dedicated EventBridge target.
340+
341+
### Basic Usage
342+
343+
The following example shows how to update an ECS service when a rule is triggered:
344+
345+
```ts
346+
const rule = new events.Rule(this, 'Rule', {
347+
schedule: events.Schedule.rate(Duration.hours(1)),
348+
});
349+
350+
rule.addTarget(new targets.AwsApi({
351+
service: 'ECS',
352+
action: 'updateService',
353+
parameters: {
354+
service: 'my-service',
355+
forceNewDeployment: true,
356+
},
357+
}));
358+
```
359+
360+
### IAM Permissions
361+
362+
By default, the AwsApi target automatically creates the necessary IAM permissions based on the service and action you specify. The permission format follows the pattern: `service:Action`.
363+
364+
For example:
365+
366+
- `ECS` service with `updateService` action → `ecs:UpdateService` permission
367+
- `RDS` service with `createDBSnapshot` action → `rds:CreateDBSnapshot` permission
368+
369+
### Custom IAM Policy
370+
371+
In some cases, you may need to provide a custom IAM policy statement, especially when:
372+
373+
- You need to restrict permissions to specific resources (instead of `*`)
374+
- The service requires additional permissions beyond the main action
375+
- You want more granular control over the permissions
376+
377+
```ts
378+
import * as iam from 'aws-cdk-lib/aws-iam';
379+
import * as s3 from 'aws-cdk-lib/aws-s3';
380+
381+
declare const rule: events.Rule;
382+
declare const bucket: s3.Bucket;
383+
384+
rule.addTarget(new targets.AwsApi({
385+
service: 's3',
386+
action: 'GetBucketEncryption',
387+
parameters: {
388+
Bucket: bucket.bucketName,
389+
},
390+
policyStatement: new iam.PolicyStatement({
391+
effect: iam.Effect.ALLOW,
392+
actions: ['s3:GetEncryptionConfiguration'],
393+
resources: [bucket.bucketArn],
394+
}),
395+
}));
396+
```
397+
336398
## Invoke an API Destination
337399

338400
Use the `targets.ApiDestination` target to trigger an external API. You need to
@@ -636,7 +698,7 @@ rule.addTarget(new targets.RedshiftQuery(workgroup.attrWorkgroupWorkgroupArn, {
636698

637699
## Publish to an SNS Topic
638700

639-
Use the `SnsTopic` target to publish to an SNS Topic.
701+
Use the `SnsTopic` target to publish to an SNS Topic.
640702

641703
The code snippet below creates the scheduled event rule that publishes to an SNS Topic using a resource policy.
642704

@@ -664,4 +726,4 @@ const rule = new events.Rule(this, 'Rule', {
664726
});
665727

666728
rule.addTarget(new targets.SnsTopic(topic, { authorizeUsingRole: true }));
667-
```
729+
```

0 commit comments

Comments
 (0)