internal virtual PutTargetsResponse PutTargets(PutTargetsRequest request) { var marshaller = PutTargetsRequestMarshaller.Instance; var unmarshaller = PutTargetsResponseUnmarshaller.Instance; return(Invoke <PutTargetsRequest, PutTargetsResponse>(request, marshaller, unmarshaller)); }
/// <summary> /// Adds target(s) to a rule. Targets are the resources that can be invoked when a rule /// is triggered. For example, AWS Lambda functions, Amazon Kinesis streams, and built-in /// targets. Updates the target(s) if they are already associated with the role. In other /// words, if there is already a target with the given target ID, then the target associated /// with that ID is updated. /// /// /// <para> /// In order to be able to make API calls against the resources you own, Amazon CloudWatch /// Events needs the appropriate permissions. For AWS Lambda and Amazon SNS resources, /// CloudWatch Events relies on resource-based policies. For Amazon Kinesis streams, CloudWatch /// Events relies on IAM roles. For more information, see <a href="http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/EventsTargetPermissions.html">Permissions /// for Sending Events to Targets</a> in the <b><i>Amazon CloudWatch Developer Guide</i></b>. /// </para> /// /// <para> /// <b>Input</b> and <b>InputPath</b> are mutually-exclusive and optional parameters of /// a target. When a rule is triggered due to a matched event, if for a target: /// </para> /// <ul> <li>Neither <b>Input</b> nor <b>InputPath</b> is specified, then the entire /// event is passed to the target in JSON form.</li> <li><b>InputPath</b> is specified /// in the form of JSONPath (e.g. <b>$.detail</b>), then only the part of the event specified /// in the path is passed to the target (e.g. only the detail part of the event is passed). /// </li> <li><b>Input</b> is specified in the form of a valid JSON, then the matched /// event is overridden with this constant.</li> </ul> /// <para> /// <b>Note:</b> When you add targets to a rule, when the associated rule triggers, new /// or updated targets might not be immediately invoked. Please allow a short period of /// time for changes to take effect. /// </para> /// </summary> /// <param name="request">Container for the necessary parameters to execute the PutTargets service method.</param> /// /// <returns>The response from the PutTargets service method, as returned by CloudWatchEvents.</returns> /// <exception cref="Amazon.CloudWatchEvents.Model.ConcurrentModificationException"> /// This exception occurs if there is concurrent modification on rule or target. /// </exception> /// <exception cref="Amazon.CloudWatchEvents.Model.InternalException"> /// This exception occurs due to unexpected causes. /// </exception> /// <exception cref="Amazon.CloudWatchEvents.Model.LimitExceededException"> /// This exception occurs if you try to create more rules or add more targets to a rule /// than allowed by default. /// </exception> /// <exception cref="Amazon.CloudWatchEvents.Model.ResourceNotFoundException"> /// The rule does not exist. /// </exception> public PutTargetsResponse PutTargets(PutTargetsRequest request) { var marshaller = new PutTargetsRequestMarshaller(); var unmarshaller = PutTargetsResponseUnmarshaller.Instance; return(Invoke <PutTargetsRequest, PutTargetsResponse>(request, marshaller, unmarshaller)); }
public async Task <string> SetTarget(AmazonCloudWatchEventsClient client, String rulename, String Inputtarget) { try { var putTargetRequest = new PutTargetsRequest { Rule = rulename, Targets = { new Target { Arn = "arn:aws:lambda:us-east-2:394451858625:function:Ec2StartStop", Id = "myCloudWatchEventsTarget", Input = Inputtarget } } }; var putTargetsResponse = await client.PutTargetsAsync(putTargetRequest); return("successfully created target"); } catch (Exception e) { throw new Exception("unable to set target"); } }
/// <summary> /// Initiates the asynchronous execution of the PutTargets operation. /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the PutTargets operation.</param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public Task <PutTargetsResponse> PutTargetsAsync(PutTargetsRequest request, System.Threading.CancellationToken cancellationToken = default(CancellationToken)) { var marshaller = new PutTargetsRequestMarshaller(); var unmarshaller = PutTargetsResponseUnmarshaller.Instance; return(InvokeAsync <PutTargetsRequest, PutTargetsResponse>(request, marshaller, unmarshaller, cancellationToken)); }
/// <summary> /// Initiates the asynchronous execution of the PutTargets operation. /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the PutTargets operation.</param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> /// <seealso href="http://docs.aws.amazon.com/goto/WebAPI/events-2015-10-07/PutTargets">REST API Reference for PutTargets Operation</seealso> public virtual Task <PutTargetsResponse> PutTargetsAsync(PutTargetsRequest request, System.Threading.CancellationToken cancellationToken = default(CancellationToken)) { var options = new InvokeOptions(); options.RequestMarshaller = PutTargetsRequestMarshaller.Instance; options.ResponseUnmarshaller = PutTargetsResponseUnmarshaller.Instance; return(InvokeAsync <PutTargetsResponse>(request, options, cancellationToken)); }
internal virtual PutTargetsResponse PutTargets(PutTargetsRequest request) { var options = new InvokeOptions(); options.RequestMarshaller = PutTargetsRequestMarshaller.Instance; options.ResponseUnmarshaller = PutTargetsResponseUnmarshaller.Instance; return(Invoke <PutTargetsResponse>(request, options)); }
static async Task Main(string[] args) { // Create Eventbridge & Lambda Client var EBclient = new AmazonEventBridgeClient(awsAccessKeyId: "Your-Access-Key", awsSecretAccessKey: "Your-Secret-Access-Key", region: Amazon.RegionEndpoint.APSoutheast1); var lambdaClient = new AmazonLambdaClient(awsAccessKeyId: "Your-Access-Key", awsSecretAccessKey: "Your-Secret-Access-Key", region: Amazon.RegionEndpoint.APSoutheast1); // Create a Scheduled Rule on EventBridge var putRuleRequest = new PutRuleRequest(); putRuleRequest.Name = "Rule2"; putRuleRequest.ScheduleExpression = "cron(*/1 * * * ? *)"; // Cron Expression (UTC Time): Minute Hour (DayofMonth) Month (DayofWeek) Year putRuleRequest.EventBusName = "default"; putRuleRequest.State = RuleState.ENABLED; var putRuleResponse = await EBclient.PutRuleAsync(putRuleRequest); Console.WriteLine(putRuleResponse.HttpStatusCode); Console.WriteLine(putRuleResponse.RuleArn); // Add Permission of the ScheduledRule to invoke function in Lambda var addPermissionRequest = new Amazon.Lambda.Model.AddPermissionRequest(); addPermissionRequest.Action = "lambda:InvokeFunction"; addPermissionRequest.Principal = "events.amazonaws.com"; addPermissionRequest.StatementId = "lambda-rule-2"; addPermissionRequest.SourceArn = putRuleResponse.RuleArn; addPermissionRequest.FunctionName = "TestEBSNS"; var addPermissionResponse = await lambdaClient.AddPermissionAsync(addPermissionRequest); Console.WriteLine(addPermissionResponse.HttpStatusCode); // Set Target of the Schedule Rule to Lambda Function var putTargetRequest = new PutTargetsRequest(); putTargetRequest.Rule = "Rule2"; putTargetRequest.EventBusName = "default"; var eventTarget = new Target(); eventTarget.Arn = "arn:aws:lambda:your-region:your-account:function:TestEBSNS"; eventTarget.Input = "{ \"Key1\":\"Rule1\", \"Key2\":\"Rule2\" }"; eventTarget.Id = "1"; List <Target> eventTargets = new List <Target>(); eventTargets.Add(eventTarget); putTargetRequest.Targets = eventTargets; var putTargetResponse = await EBclient.PutTargetsAsync(putTargetRequest); Console.WriteLine(putTargetResponse.HttpStatusCode); }
public static void PutTargetExample() { AmazonCloudWatchEventsClient client = new AmazonCloudWatchEventsClient(); var putTargetRequest = new PutTargetsRequest { Rule = "DEMO_EVENT", Targets = { new Target { Arn = "LAMBDA_FUNCTION_ARN", Id = "myCloudWatchEventsTarget" } } }; client.PutTargets(putTargetRequest); }
public string Add(Notification input, ILambdaContext context) { if (input == null) { throw new ArgumentNullException("Input required"); } //var topicId = Guid.NewGuid().ToString(); var region = RegionEndpoint.APSoutheast1; //Create topic in SNS var snsClient = new AmazonSimpleNotificationServiceClient(region); var createTopicRequest = new CreateTopicRequest(); createTopicRequest.Name = input.TopicName; var createTopicResponse = snsClient.CreateTopicAsync(createTopicRequest).Result; if (createTopicResponse.HttpStatusCode != HttpStatusCode.OK) { throw new TestDonkeyException("Can't add topic"); } //Create rule in CloudWatch Events var cloudWatchEventClient = new AmazonCloudWatchEventsClient(region); var putRuleRequest = new PutRuleRequest(); putRuleRequest.ScheduleExpression = input.Cron; putRuleRequest.Name = $"testdonkey-rule-{ input.TopicId }"; putRuleRequest.State = RuleState.ENABLED; var putRuleResponse = cloudWatchEventClient.PutRuleAsync(putRuleRequest).Result; if (putRuleResponse.HttpStatusCode != HttpStatusCode.OK) { throw new TestDonkeyException("Can't add rule"); } //Set target for CloudWatch Events var putTargetsRequest = new PutTargetsRequest(); putTargetsRequest.Rule = putRuleRequest.Name; putTargetsRequest.Targets = new List <Target> { new Target() { Arn = "arn:aws:lambda:ap-southeast-1:404276529491:function:TestDonkeyLambda", Id = $"testdonkey-target-{ input.TopicId }", Input = $"{{\"TopicId\":\"{ input.TopicId }\"}}" } }; var putTargetsResponse = cloudWatchEventClient.PutTargetsAsync(putTargetsRequest).Result; if (putTargetsResponse.HttpStatusCode != HttpStatusCode.OK) { var deleteRuleRequest = new DeleteRuleRequest(); deleteRuleRequest.Name = putRuleRequest.Name; cloudWatchEventClient.DeleteRuleAsync(deleteRuleRequest); throw new TestDonkeyException("Can't add target"); } //Add permission to accept CloudWatch Events trigger for Lambda var lambdaClient = new AmazonLambdaClient(region); var addPermissionRequest = new Amazon.Lambda.Model.AddPermissionRequest(); addPermissionRequest.FunctionName = "TestDonkeyLambda"; addPermissionRequest.Action = "lambda:InvokeFunction"; addPermissionRequest.StatementId = $"testdonkey-lambda-{ input.TopicId }"; addPermissionRequest.Principal = "events.amazonaws.com"; addPermissionRequest.SourceArn = putRuleResponse.RuleArn; var addPermissionResponse = lambdaClient.AddPermissionAsync(addPermissionRequest).Result; if (addPermissionResponse.HttpStatusCode != HttpStatusCode.Created) { throw new TestDonkeyException("Can't add permission"); } //Save notification to DynamoDB var dynamoDBClient = new AmazonDynamoDBClient(region); var item = new Dictionary <string, AttributeValue>(); item.Add("id", new AttributeValue() { S = input.TopicId }); item.Add("name", new AttributeValue() { S = input.TopicName }); item.Add("arn", new AttributeValue() { S = createTopicResponse.TopicArn }); item.Add("cron", new AttributeValue() { S = input.Cron }); var putItemResponse = dynamoDBClient.PutItemAsync("Topics", item).Result; if (putItemResponse.HttpStatusCode != HttpStatusCode.OK) { throw new TestDonkeyException("Can't add item"); } return("success"); }