There are more AWS SDK examples available in the AWS Doc SDK Examples
Use PutTargets
with an AWS SDK or CLI
The following code examples show how to use PutTargets
.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code examples:
- .NET
-
- AWS SDK for .NET
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Add an Amazon SNS topic as a target for a rule.
/// <summary> /// Add an Amazon SNS target topic to a rule. /// </summary> /// <param name="ruleName">The name of the rule to update.</param> /// <param name="targetArn">The ARN of the Amazon SNS target.</param> /// <param name="eventBusArn">The optional event bus name, uses default if empty.</param> /// <returns>The ID of the target.</returns> public async Task<string> AddSnsTargetToRule(string ruleName, string targetArn, string? eventBusArn = null) { var targetID = Guid.NewGuid().ToString(); // Create the list of targets and add a new target. var targets = new List<Target> { new Target() { Arn = targetArn, Id = targetID } }; // Add the targets to the rule. var response = await _amazonEventBridge.PutTargetsAsync( new PutTargetsRequest() { EventBusName = eventBusArn, Rule = ruleName, Targets = targets, }); if (response.FailedEntryCount > 0) { response.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to add target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return targetID; }
Add an input transformer to a target for a rule.
/// <summary> /// Update an Amazon S3 object created rule with a transform on the target. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <param name="targetArn">The ARN of the target.</param> /// <param name="eventBusArn">Optional event bus ARN. If empty, uses the default event bus.</param> /// <returns>The ID of the target.</returns> public async Task<string> UpdateS3UploadRuleTargetWithTransform(string ruleName, string targetArn, string? eventBusArn = null) { var targetID = Guid.NewGuid().ToString(); var targets = new List<Target> { new Target() { Id = targetID, Arn = targetArn, InputTransformer = new InputTransformer() { InputPathsMap = new Dictionary<string, string>() { {"bucket", "$.detail.bucket.name"}, {"time", "$.time"} }, InputTemplate = "\"Notification: an object was uploaded to bucket <bucket> at <time>.\"" } } }; var response = await _amazonEventBridge.PutTargetsAsync( new PutTargetsRequest() { EventBusName = eventBusArn, Rule = ruleName, Targets = targets, }); if (response.FailedEntryCount > 0) { response.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to add target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return targetID; }
-
For API details, see PutTargets in AWS SDK for .NET API Reference.
-
- C++
-
- SDK for C++
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Include the required files.
#include <aws/core/Aws.h> #include <aws/events/EventBridgeClient.h> #include <aws/events/model/PutTargetsRequest.h> #include <aws/events/model/PutTargetsResult.h> #include <aws/core/utils/Outcome.h> #include <iostream>
Add the target.
Aws::CloudWatchEvents::EventBridgeClient cwe; Aws::CloudWatchEvents::Model::Target target; target.SetArn(lambda_arn); target.SetId(target_id); Aws::CloudWatchEvents::Model::PutTargetsRequest request; request.SetRule(rule_name); request.AddTargets(target); auto putTargetsOutcome = cwe.PutTargets(request); if (!putTargetsOutcome.IsSuccess()) { std::cout << "Failed to create CloudWatch events target for rule " << rule_name << ": " << putTargetsOutcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully created CloudWatch events target for rule " << rule_name << std::endl; }
-
For API details, see PutTargets in AWS SDK for C++ API Reference.
-
- CLI
-
- AWS CLI
-
To add targets for CloudWatch Events rules
This example adds a Lambda function as the target of a rule:
aws events put-targets --rule
DailyLambdaFunction
--targets "Id"="1","Arn"="arn:aws:lambda:us-east-1:123456789012:function:MyFunctionName"This example sets an Amazon Kinesis stream as the target, so that events caught by this rule are relayed to the stream:
aws events put-targets --rule
EC2InstanceStateChanges
--targets "Id"="1","Arn"="arn:aws:kinesis:us-east-1:123456789012:stream/MyStream","RoleArn"="arn:aws:iam::123456789012:role/MyRoleForThisRule"This example sets two Amazon Kinesis streams as targets for one rule:
aws events put-targets --rule
DailyLambdaFunction
--targets "Id"="Target1","Arn"="arn:aws:kinesis:us-east-1:379642911888:stream/MyStream1","RoleArn"="arn:aws:iam::379642911888:role/ MyRoleToAccessLambda" "Id"="Target2"," Arn"="arn:aws:kinesis:us-east-1:379642911888:stream/MyStream2","RoleArn"="arn:aws:iam::379642911888:role/MyRoleToAccessLambda"-
For API details, see PutTargets
in AWS CLI Command Reference.
-
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Add an Amazon SNS topic as a target for a rule.
// Add a rule which triggers an SNS target when a file is uploaded to an S3 // bucket. public static void addSnsEventRule(EventBridgeClient eventBrClient, String ruleName, String topicArn, String topicName, String eventRuleName, String bucketName) { String targetID = java.util.UUID.randomUUID().toString(); Target myTarget = Target.builder() .id(targetID) .arn(topicArn) .build(); List<Target> targets = new ArrayList<>(); targets.add(myTarget); PutTargetsRequest request = PutTargetsRequest.builder() .eventBusName(null) .targets(targets) .rule(ruleName) .build(); eventBrClient.putTargets(request); System.out.println("Added event rule " + eventRuleName + " with Amazon SNS target " + topicName + " for bucket " + bucketName + "."); }
Add an input transformer to a target for a rule.
public static void updateCustomRuleTargetWithTransform(EventBridgeClient eventBrClient, String topicArn, String ruleName) { String targetId = java.util.UUID.randomUUID().toString(); InputTransformer inputTransformer = InputTransformer.builder() .inputTemplate("\"Notification: sample event was received.\"") .build(); Target target = Target.builder() .id(targetId) .arn(topicArn) .inputTransformer(inputTransformer) .build(); try { PutTargetsRequest targetsRequest = PutTargetsRequest.builder() .rule(ruleName) .targets(target) .eventBusName(null) .build(); eventBrClient.putTargets(targetsRequest); } catch (EventBridgeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
-
For API details, see PutTargets in AWS SDK for Java 2.x API Reference.
-
- JavaScript
-
- SDK for JavaScript (v3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Import the SDK and client modules and call the API.
import { EventBridgeClient, PutTargetsCommand, } from "@aws-sdk/client-eventbridge"; export const putTarget = async ( existingRuleName = "some-rule", targetArn = "arn:aws:lambda:us-east-1:000000000000:function:test-func", uniqueId = Date.now().toString(), ) => { const client = new EventBridgeClient({}); const response = await client.send( new PutTargetsCommand({ Rule: existingRuleName, Targets: [ { Arn: targetArn, Id: uniqueId, }, ], }), ); console.log("PutTargets response:"); console.log(response); // PutTargets response: // { // '$metadata': { // httpStatusCode: 200, // requestId: 'f5b23b9a-2c17-45c1-ad5c-f926c3692e3d', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // FailedEntries: [], // FailedEntryCount: 0 // } return response; };
-
For API details, see PutTargets in AWS SDK for JavaScript API Reference.
-
- SDK for JavaScript (v2)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. // Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create CloudWatchEvents service object var ebevents = new AWS.EventBridge({ apiVersion: "2015-10-07" }); var params = { Rule: "DEMO_EVENT", Targets: [ { Arn: "LAMBDA_FUNCTION_ARN", Id: "myEventBridgeTarget", }, ], }; ebevents.putTargets(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
-
For API details, see PutTargets in AWS SDK for JavaScript API Reference.
-
- Kotlin
-
- SDK for Kotlin
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. // Add a rule that triggers an SNS target when a file is uploaded to an S3 bucket. suspend fun addSnsEventRule( ruleName: String?, topicArn: String?, topicName: String, eventRuleName: String, bucketName: String, ) { val targetID = UUID.randomUUID().toString() val myTarget = Target { id = targetID arn = topicArn } val targetsOb = mutableListOf<Target>() targetsOb.add(myTarget) val request = PutTargetsRequest { eventBusName = null targets = targetsOb rule = ruleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.putTargets(request) println("Added event rule $eventRuleName with Amazon SNS target $topicName for bucket $bucketName.") } }
Add an input transformer to a target for a rule.
suspend fun updateCustomRuleTargetWithTransform( topicArn: String?, ruleName: String?, ) { val targetId = UUID.randomUUID().toString() val inputTransformerOb = InputTransformer { inputTemplate = "\"Notification: sample event was received.\"" } val target = Target { id = targetId arn = topicArn inputTransformer = inputTransformerOb } val targetsRequest = PutTargetsRequest { rule = ruleName targets = listOf(target) eventBusName = null } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.putTargets(targetsRequest) } }
-
For API details, see PutTargets
in AWS SDK for Kotlin API reference.
-