Menu
Amazon CloudWatch Events
API Reference (API Version 2015-10-07)

PutTargets

Adds the specified targets to the specified rule, or updates the targets if they are already associated with the rule.

Targets are the resources that are invoked when a rule is triggered. Example targets include EC2 instances, AWS Lambda functions, Amazon Kinesis streams, Amazon ECS tasks, AWS Step Functions state machines, and built-in targets. Note that creating rules with built-in targets is supported only in the AWS Management Console.

For some target types, PutTargets provides target-specific parameters. If the target is an Amazon Kinesis stream, you can optionally specify which shard the event goes to by using the KinesisParameters argument. To invoke a command on multiple EC2 instances with one rule, you can use the RunCommandParameters field.

To be able to make API calls against the resources that you own, Amazon CloudWatch Events needs the appropriate permissions. For AWS Lambda and Amazon SNS resources, CloudWatch Events relies on resource-based policies. For EC2 instances, Amazon Kinesis streams, and AWS Step Functions state machines, CloudWatch Events relies on IAM roles that you specify in the RoleARN argument in PutTarget. For more information, see Authentication and Access Control in the Amazon CloudWatch Events User Guide.

Input, InputPath and InputTransformer are mutually exclusive and optional parameters of a target. When a rule is triggered due to a matched event:

  • If none of the following arguments are specified for a target, then the entire event is passed to the target in JSON form (unless the target is Amazon EC2 Run Command or Amazon ECS task, in which case nothing from the event is passed to the target).

  • If Input is specified in the form of valid JSON, then the matched event is overridden with this constant.

  • If InputPath is specified in the form of JSONPath (for example, $.detail), then only the part of the event specified in the path is passed to the target (for example, only the detail part of the event is passed).

  • If InputTransformer is specified, then one or more specified JSONPaths are extracted from the event and used as values in a template that you specify as the input to the target.

When you specify Input, InputPath, or InputTransformer, you must use JSON dot notation, not bracket notation.

When you add targets to a rule and the associated rule triggers soon after, new or updated targets might not be immediately invoked. Please allow a short period of time for changes to take effect.

This action can partially fail if too many requests are made at the same time. If that happens, FailedEntryCount is non-zero in the response and each entry in FailedEntries provides the ID of the failed target and the error code.

Request Syntax

Copy
{ "Rule": "string", "Targets": [ { "Arn": "string", "EcsParameters": { "TaskCount": number, "TaskDefinitionArn": "string" }, "Id": "string", "Input": "string", "InputPath": "string", "InputTransformer": { "InputPathsMap": { "string" : "string" }, "InputTemplate": "string" }, "KinesisParameters": { "PartitionKeyPath": "string" }, "RoleArn": "string", "RunCommandParameters": { "RunCommandTargets": [ { "Key": "string", "Values": [ "string" ] } ] } } ] }

Request Parameters

For information about the parameters that are common to all actions, see Common Parameters.

The request accepts the following data in JSON format.

Rule

The name of the rule.

Type: String

Length Constraints: Minimum length of 1. Maximum length of 64.

Pattern: [\.\-_A-Za-z0-9]+

Required: Yes

Targets

The targets to update or add to the rule.

Type: array of Target objects

Array Members: Minimum number of 1 item. Maximum number of 100 items.

Required: Yes

Response Syntax

Copy
{ "FailedEntries": [ { "ErrorCode": "string", "ErrorMessage": "string", "TargetId": "string" } ], "FailedEntryCount": number }

Response Elements

If the action is successful, the service sends back an HTTP 200 response.

The following data is returned in JSON format by the service.

FailedEntries

The failed target entries.

Type: array of PutTargetsResultEntry objects

FailedEntryCount

The number of failed entries.

Type: Integer

Errors

For information about the errors that are common to all actions, see Common Errors.

ConcurrentModificationException

There is concurrent modification on a rule or target.

HTTP Status Code: 400

InternalException

This exception occurs due to unexpected causes.

HTTP Status Code: 500

LimitExceededException

You tried to create more rules or add more targets to a rule than is allowed.

HTTP Status Code: 400

ResourceNotFoundException

The rule does not exist.

HTTP Status Code: 400

Examples

Adds a target to a Lambda function with the ID "MyTargetId" to the rule named "test"

The following is an example of a PutTargets request.

Sample Request

Copy
POST / HTTP/1.1 Host: events.<region>.<domain> x-amz-Date: <Date> Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=content-type;date;host;user-agent;x-amz-date;x-amz-target;x-amzn-requestid, Signature=<Signature> User-Agent: <UserAgentString> Content-Type: application/x-amz-json-1.1 Content-Length: <PayloadSizeBytes> Connection: Keep-Alive X-Amz-Target: AWSEvents.PutTargets { "Rule": "test", "Targets": [ { "Id": "MyTargetId", "Arn": "arn:aws:lambda:us-east-1:123456789012:function:MyFunction" } ] }

Sample Response

Copy
HTTP/1.1 200 OK x-amzn-RequestId: <RequestId> Content-Type: application/x-amz-json-1.1 Content-Length: <PayloadSizeBytes> Date: <Date> { "FailedEntries": [], "FailedEntryCount": 0 }

Uses Input Transformer to extract data from an event and input that data to the target

This example extracts the instance and state from an event, puts them into a simple text template, and passes this data to a Lambda function called MyFunction.

Sample Request

Copy
POST / HTTP/1.1 Host: events.<region>.<domain> x-amz-Date: <Date> Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=content-type;date;host;user-agent;x-amz-date;x-amz-target;x-amzn-requestid, Signature=<Signature> User-Agent: <UserAgentString> Content-Type: application/x-amz-json-1.1 Content-Length: <PayloadSizeBytes> Connection: Keep-Alive X-Amz-Target: AWSEvents.PutTargets { "Rule": "testrule", "Targets": [ { "Id": "MyTargetId", "Arn": "arn:aws:lambda:us-east-1:123456789012:function:MyFunction" "InputTransformer": { "InputPathsMap": {“instance”: “$.detail.instance”,”status”: "$.detail.status"}, "InputTemplate": "<instance> is in state <status>" } ] }

Example

Here is another sample request using InputTransformer. The input to the Lambda function is in JSON format, with an array substituted. Below that sample request are examples of an event and the resulting output to the target, using this sample request.

Copy
POST / HTTP/1.1 Host: events.<region>.<domain> x-amz-Date: <Date> Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=content-type;date;host;user-agent;x-amz-date;x-amz-target;x-amzn-requestid, Signature=<Signature> User-Agent: <UserAgentString> Content-Type: application/x-amz-json-1.1 Content-Length: <PayloadSizeBytes> Connection: Keep-Alive X-Amz-Target: AWSEvents.PutTargets { "Rule": "testrule", "Targets": [ { "Id": "MyTargetId", "Arn": "arn:aws:lambda:us-east-1:123456789012:function:MyFunction" "InputTransformer": { "InputPathsMap": {"commandstToRun": "$.detail.commands"} "InputTemplate": "{\"commands\": <commandsToRun>}" } ] }
Copy
Incoming event: { "Time": 1225864800, "Source": "foo", "Resources": ["foo", "foo"], "DetailType": "foo", "Detail": { "commands": ["ls -lrt", "echo HelloWorld!"] } }
Copy
Output sent to the target: { "commands" : ["ls -lrt", "echo HelloWorld!"] }

Sends a command to a list of EC2 instances specified by InstanceIds, using Amazon EC2 Run Command

Copy
POST / HTTP/1.1 Host: events.<region>.<domain> x-amz-Date: <Date> Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=content-type;date;host;user-agent;x-amz-date;x-amz-target;x-amzn-requestid, Signature=<Signature> User-Agent: <UserAgentString> Content-Type: application/x-amz-json-1.1 Content-Length: <PayloadSizeBytes> Connection: Keep-Alive X-Amz-Target: AWSEvents.PutTargets { "Rule": "testrule", "Targets": [ { "Id": "id123456789", "Arn": "arn:aws:ssm:us-east-1:12345679012:document/RestartLinuxService", "RoleArn": "arn:aws:iam::123456789012:role/MyRoleToAccessEC2" "RunCommandProperties": { "RunCommandTargets": [ { "Key":"InstanceIds", "Values":["i-123456789012", "i-098765432109"] } ] } ] }

Uses KinesisParameters to control shard assignment

In this example, KinesisParameters is used to specify that events related to status changes are sent to a shard specific to the affected instance ID.

Copy
POST / HTTP/1.1 Host: events.<region>.<domain> x-amz-Date: <Date> Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=content-type;date;host;user-agent;x-amz-date;x-amz-target;x-amzn-requestid, Signature=<Signature> User-Agent: <UserAgentString> Content-Type: application/x-amz-json-1.1 Content-Length: <PayloadSizeBytes> Connection: Keep-Alive X-Amz-Target: AWSEvents.PutTargets { "Rule": "StatusChangeRule", "Targets": [ { "Id" : "1", "Arn": "arn:aws:kinesis:us-east-1:123456789012:function:stream/mystream", "KinesisParameters":{ "PartitionKeyPath":"$.detail.instance-id"}' } ] }

Adds a Step Functions state machine as a target

This example targets a state machine called "HelloWorld", and sends the input constant "Hello World!" to that target.

Copy
POST / HTTP/1.1 Host: events.<region>.<domain> x-amz-Date: <Date> Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=content-type;date;host;user-agent;x-amz-date;x-amz-target;x-amzn-requestid, Signature=<Signature> User-Agent: <UserAgentString> Content-Type: application/x-amz-json-1.1 Content-Length: <PayloadSizeBytes> Connection: Keep-Alive X-Amz-Target: AWSEvents.PutTargets { "Rule": "testrule", "Targets": [ { "RoleArn": "arn:aws:iam::123456789012:role/MyRoleToAccessStepFunctions" "Arn":"arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld", "Input":"HelloWorld!" } ] }

Adds a target that creates three Amazon ECS tasks based on a task definition.

This example uses Amazon ECS as the target. You must have already created the task definition and cluster in Amazon ECS.

Copy
POST / HTTP/1.1 Host: events.<region>.<domain> x-amz-Date: <Date> Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=content-type;date;host;user-agent;x-amz-date;x-amz-target;x-amzn-requestid, Signature=<Signature> User-Agent: <UserAgentString> Content-Type: application/x-amz-json-1.1 Content-Length: <PayloadSizeBytes> Connection: Keep-Alive X-Amz-Target: AWSEvents.PutTargets { "Rule": "test", "Targets": [ { "Id": "Target1", "RoleArn": "arn:aws:iam::123456789012:role/MyRoleToAccessECS" "Arn": "arn:aws:ecs:us-east-1:123456789012:cluster/example-cluster", "ECSParameters": { "TaskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/example", "TaskCount": 3 } } ] }

Specifying two targets with one command

This example sets two simple targets with one command. In this example, both targets are AWS Lambda functions, but the two targets could be different AWS services as well.

Sample Request

Copy
POST / HTTP/1.1 Host: events.<region>.<domain> x-amz-Date: <Date> Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=content-type;date;host;user-agent;x-amz-date;x-amz-target;x-amzn-requestid, Signature=<Signature> User-Agent: <UserAgentString> Content-Type: application/x-amz-json-1.1 Content-Length: <PayloadSizeBytes> Connection: Keep-Alive X-Amz-Target: AWSEvents.PutTargets { "Rule": "test", "Targets": [ { "Id": "MyTargetId", "Arn": "arn:aws:lambda:us-east-1:123456789012:function:MyFunction" } { "Id": "MyTargetId2", "Arn": "arn:aws:lambda:us-east-1:123456789012:function:MyFunction2" } ] }

See Also

For more information about using this API in one of the language-specific AWS SDKs, see the following: