Use PutEvents com um AWS SDK - AWS SDKExemplos de código

Há mais AWS SDK exemplos disponíveis no GitHub repositório AWS Doc SDK Examples.

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Use PutEvents com um AWS SDK

Os exemplos de código a seguir mostram como usar o PutEvents.

Java
SDKpara Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequest; import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequestEntry; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class PutEvents { public static void main(String[] args) { final String usage = """ Usage: <resourceArn> Where: resourceArn - An Amazon Resource Name (ARN) related to the events. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String resourceArn = args[0]; CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() .build(); putCWEvents(cwe, resourceArn); cwe.close(); } public static void putCWEvents(CloudWatchEventsClient cwe, String resourceArn) { try { final String EVENT_DETAILS = "{ \"key1\": \"value1\", \"key2\": \"value2\" }"; PutEventsRequestEntry requestEntry = PutEventsRequestEntry.builder() .detail(EVENT_DETAILS) .detailType("sampleSubmitted") .resources(resourceArn) .source("aws-sdk-java-cloudwatch-example") .build(); PutEventsRequest request = PutEventsRequest.builder() .entries(requestEntry) .build(); cwe.putEvents(request); System.out.println("Successfully put CloudWatch event"); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • Para API obter detalhes, consulte PutEventsem AWS SDK for Java 2.x APIReferência.

JavaScript
SDKpara JavaScript (v3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

Importe os módulos SDK e do cliente e chame API o.

import { PutEventsCommand } from "@aws-sdk/client-cloudwatch-events"; import { client } from "../libs/client.js"; const run = async () => { const command = new PutEventsCommand({ // The list of events to send to Amazon CloudWatch Events. Entries: [ { // The name of the application or service that is sending the event. Source: "my.app", // The name of the event that is being sent. DetailType: "My Custom Event", // The data that is sent with the event. Detail: JSON.stringify({ timeOfEvent: new Date().toISOString() }), }, ], }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();

Crie o cliente em um módulo separado e exporte-o.

import { CloudWatchEventsClient } from "@aws-sdk/client-cloudwatch-events"; export const client = new CloudWatchEventsClient({});
SDKpara JavaScript (v2)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

// 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 cwevents = new AWS.CloudWatchEvents({ apiVersion: "2015-10-07" }); var params = { Entries: [ { Detail: '{ "key1": "value1", "key2": "value2" }', DetailType: "appRequestSubmitted", Resources: ["RESOURCE_ARN"], Source: "com.company.app", }, ], }; cwevents.putEvents(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Entries); } });