Hay más AWS SDK ejemplos disponibles en el GitHub repositorio de AWS Doc SDK Examples
Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.
EventBridge ejemplos que utilizan SDK para Kotlin
En los siguientes ejemplos de código, se muestra cómo realizar acciones e implementar escenarios comunes mediante el uso de AWS SDK for Kotlin with. EventBridge
Los conceptos básicos son ejemplos de código que muestran cómo realizar las operaciones esenciales dentro de un servicio.
Las acciones son extractos de código de programas más grandes y deben ejecutarse en contexto. Mientras las acciones muestran cómo llamar a las funciones de servicio individuales, es posible ver las acciones en contexto en los escenarios relacionados.
Cada ejemplo incluye un enlace al código fuente completo, donde encontrarás instrucciones sobre cómo configurar y ejecutar el código en su contexto.
Introducción
Los siguientes ejemplos de código muestran cómo empezar a usarlo EventBridge.
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. import aws.sdk.kotlin.services.eventbridge.EventBridgeClient import aws.sdk.kotlin.services.eventbridge.model.ListEventBusesRequest import aws.sdk.kotlin.services.eventbridge.model.ListEventBusesResponse suspend fun main() { listBusesHello() } suspend fun listBusesHello() { val request = ListEventBusesRequest { limit = 10 } EventBridgeClient { region = "us-west-2" }.use { eventBrClient -> val response: ListEventBusesResponse = eventBrClient.listEventBuses(request) response.eventBuses?.forEach { bus -> println("The name of the event bus is ${bus.name}") println("The ARN of the event bus is ${bus.arn}") } } }
-
Para API obtener más información, consulta ListEventBuses
la AWS SDKAPIreferencia sobre Kotlin.
-
Conceptos básicos
En el siguiente ejemplo de código, se muestra cómo:
Crear una regla y agregarle un destino
Habilitar y deshabilitar reglas.
Enumerar y actualizar reglas y destinos.
Enviar eventos y, después, limpiar los recursos
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. /* Before running this Kotlin 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-kotlin/latest/developer-guide/setup.html This Kotlin example performs the following tasks with Amazon EventBridge: 1. Creates an AWS Identity and Access Management (IAM) role to use with Amazon EventBridge. 2. Creates an Amazon Simple Storage Service (Amazon S3) bucket with EventBridge events enabled. 3. Creates a rule that triggers when an object is uploaded to Amazon S3. 4. Lists rules on the event bus. 5. Creates a new Amazon Simple Notification Service (Amazon SNS) topic and lets the user subscribe to it. 6. Adds a target to the rule that sends an email to the specified topic. 7. Creates an EventBridge event that sends an email when an Amazon S3 object is created. 8. Lists targets. 9. Lists the rules for the same target. 10. Triggers the rule by uploading a file to the S3 bucket. 11. Disables a specific rule. 12. Checks and prints the state of the rule. 13. Adds a transform to the rule to change the text of the email. 14. Enables a specific rule. 15. Triggers the updated rule by uploading a file to the S3 bucket. 16. Updates the rule to a custom rule pattern. 17. Sends an event to trigger the rule. 18. Cleans up resources. */ val DASHES: String = String(CharArray(80)).replace("\u0000", "-") suspend fun main(args: Array<String>) { val usage = """ Usage: <roleName> <bucketName> <topicName> <eventRuleName> Where: roleName - The name of the role to create. bucketName - The Amazon Simple Storage Service (Amazon S3) bucket name to create. topicName - The name of the Amazon Simple Notification Service (Amazon SNS) topic to create. eventRuleName - The Amazon EventBridge rule name to create. """ val polJSON = "{" + "\"Version\": \"2012-10-17\"," + "\"Statement\": [{" + "\"Effect\": \"Allow\"," + "\"Principal\": {" + "\"Service\": \"events.amazonaws.com\"" + "}," + "\"Action\": \"sts:AssumeRole\"" + "}]" + "}" if (args.size != 4) { println(usage) exitProcess(1) } val sc = Scanner(System.`in`) val roleName = args[0] val bucketName = args[1] val topicName = args[2] val eventRuleName = args[3] println(DASHES) println("Welcome to the Amazon EventBridge example scenario.") println(DASHES) println(DASHES) println("1. Create an AWS Identity and Access Management (IAM) role to use with Amazon EventBridge.") val roleArn = createIAMRole(roleName, polJSON) println(DASHES) println(DASHES) println("2. Create an S3 bucket with EventBridge events enabled.") if (checkBucket(bucketName)) { println("$bucketName already exists. Ending this scenario.") exitProcess(1) } createBucket(bucketName) delay(3000) setBucketNotification(bucketName) println(DASHES) println(DASHES) println("3. Create a rule that triggers when an object is uploaded to Amazon S3.") delay(10000) addEventRule(roleArn, bucketName, eventRuleName) println(DASHES) println(DASHES) println("4. List rules on the event bus.") listRules() println(DASHES) println(DASHES) println("5. Create a new SNS topic for testing and let the user subscribe to the topic.") val topicArn = createSnsTopic(topicName) println(DASHES) println(DASHES) println("6. Add a target to the rule that sends an email to the specified topic.") println("Enter your email to subscribe to the Amazon SNS topic:") val email = sc.nextLine() subEmail(topicArn, email) println("Use the link in the email you received to confirm your subscription. Then press Enter to continue.") sc.nextLine() println(DASHES) println(DASHES) println("7. Create an EventBridge event that sends an email when an Amazon S3 object is created.") addSnsEventRule(eventRuleName, topicArn, topicName, eventRuleName, bucketName) println(DASHES) println(DASHES) println("8. List targets.") listTargets(eventRuleName) println(DASHES) println(DASHES) println(" 9. List the rules for the same target.") listTargetRules(topicArn) println(DASHES) println(DASHES) println("10. Trigger the rule by uploading a file to the S3 bucket.") println("Press Enter to continue.") sc.nextLine() uploadTextFiletoS3(bucketName) println(DASHES) println(DASHES) println("11. Disable a specific rule.") changeRuleState(eventRuleName, false) println(DASHES) println(DASHES) println("12. Check and print the state of the rule.") checkRule(eventRuleName) println(DASHES) println(DASHES) println("13. Add a transform to the rule to change the text of the email.") updateSnsEventRule(topicArn, eventRuleName) println(DASHES) println(DASHES) println("14. Enable a specific rule.") changeRuleState(eventRuleName, true) println(DASHES) println(DASHES) println("15. Trigger the updated rule by uploading a file to the S3 bucket.") println("Press Enter to continue.") sc.nextLine() uploadTextFiletoS3(bucketName) println(DASHES) println(DASHES) println("16. Update the rule to a custom rule pattern.") updateToCustomRule(eventRuleName) println("Updated event rule $eventRuleName to use a custom pattern.") updateCustomRuleTargetWithTransform(topicArn, eventRuleName) println("Updated event target $topicArn.") println(DASHES) println(DASHES) println("17. Send an event to trigger the rule. This will trigger a subscription email.") triggerCustomRule(email) println("Events have been sent. Press Enter to continue.") sc.nextLine() println(DASHES) println(DASHES) println("18. Clean up resources.") println("Do you want to clean up resources (y/n)") val ans = sc.nextLine() if (ans.compareTo("y") == 0) { cleanupResources(topicArn, eventRuleName, bucketName, roleName) } else { println("The resources will not be cleaned up. ") } println(DASHES) println(DASHES) println("The Amazon EventBridge example scenario has successfully completed.") println(DASHES) } suspend fun cleanupResources( topicArn: String?, eventRuleName: String?, bucketName: String?, roleName: String?, ) { println("Removing all targets from the event rule.") deleteTargetsFromRule(eventRuleName) deleteRuleByName(eventRuleName) deleteSNSTopic(topicArn) deleteS3Bucket(bucketName) deleteRole(roleName) } suspend fun deleteRole(roleNameVal: String?) { val policyArnVal = "arn:aws:iam::aws:policy/AmazonEventBridgeFullAccess" val policyRequest = DetachRolePolicyRequest { policyArn = policyArnVal roleName = roleNameVal } IamClient { region = "us-east-1" }.use { iam -> iam.detachRolePolicy(policyRequest) println("Successfully detached policy $policyArnVal from role $roleNameVal") // Delete the role. val roleRequest = DeleteRoleRequest { roleName = roleNameVal } iam.deleteRole(roleRequest) println("*** Successfully deleted $roleNameVal") } } suspend fun deleteS3Bucket(bucketName: String?) { // Remove all the objects from the S3 bucket. val listObjects = ListObjectsRequest { bucket = bucketName } S3Client { region = "us-east-1" }.use { s3Client -> val res = s3Client.listObjects(listObjects) val myObjects = res.contents val toDelete = mutableListOf<ObjectIdentifier>() if (myObjects != null) { for (myValue in myObjects) { toDelete.add( ObjectIdentifier { key = myValue.key }, ) } } val delOb = Delete { objects = toDelete } val dor = DeleteObjectsRequest { bucket = bucketName delete = delOb } s3Client.deleteObjects(dor) // Delete the S3 bucket. val deleteBucketRequest = DeleteBucketRequest { bucket = bucketName } s3Client.deleteBucket(deleteBucketRequest) println("You have deleted the bucket and the objects") } } // Delete the SNS topic. suspend fun deleteSNSTopic(topicArnVal: String?) { val request = DeleteTopicRequest { topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> snsClient.deleteTopic(request) println(" $topicArnVal was deleted.") } } suspend fun deleteRuleByName(ruleName: String?) { val ruleRequest = DeleteRuleRequest { name = ruleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.deleteRule(ruleRequest) println("Successfully deleted the rule") } } suspend fun deleteTargetsFromRule(eventRuleName: String?) { // First, get all targets that will be deleted. val request = ListTargetsByRuleRequest { rule = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listTargetsByRule(request) val allTargets = response.targets // Get all targets and delete them. if (allTargets != null) { for (myTarget in allTargets) { val removeTargetsRequest = RemoveTargetsRequest { rule = eventRuleName ids = listOf(myTarget.id.toString()) } eventBrClient.removeTargets(removeTargetsRequest) println("Successfully removed the target") } } } } suspend fun triggerCustomRule(email: String) { val json = "{" + "\"UserEmail\": \"" + email + "\"," + "\"Message\": \"This event was generated by example code.\"" + "\"UtcTime\": \"Now.\"" + "}" val entry = PutEventsRequestEntry { source = "ExampleSource" detail = json detailType = "ExampleType" } val eventsRequest = PutEventsRequest { this.entries = listOf(entry) } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.putEvents(eventsRequest) } } 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) } } suspend fun updateToCustomRule(ruleName: String?) { val customEventsPattern = "{" + "\"source\": [\"ExampleSource\"]," + "\"detail-type\": [\"ExampleType\"]" + "}" val request = PutRuleRequest { name = ruleName description = "Custom test rule" eventPattern = customEventsPattern } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.putRule(request) } } // Update an Amazon S3 object created rule with a transform on the target. suspend fun updateSnsEventRule( topicArn: String?, ruleName: String?, ) { val targetId = UUID.randomUUID().toString() val myMap = mutableMapOf<String, String>() myMap["bucket"] = "$.detail.bucket.name" myMap["time"] = "$.time" val inputTransOb = InputTransformer { inputTemplate = "\"Notification: an object was uploaded to bucket <bucket> at <time>.\"" inputPathsMap = myMap } val targetOb = Target { id = targetId arn = topicArn inputTransformer = inputTransOb } val targetsRequest = PutTargetsRequest { rule = ruleName targets = listOf(targetOb) eventBusName = null } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.putTargets(targetsRequest) } } suspend fun checkRule(eventRuleName: String?) { val ruleRequest = DescribeRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.describeRule(ruleRequest) println("The state of the rule is $response") } } suspend fun changeRuleState( eventRuleName: String, isEnabled: Boolean?, ) { if (!isEnabled!!) { println("Disabling the rule: $eventRuleName") val ruleRequest = DisableRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.disableRule(ruleRequest) } } else { println("Enabling the rule: $eventRuleName") val ruleRequest = EnableRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.enableRule(ruleRequest) } } } // Create and upload a file to an S3 bucket to trigger an event. @Throws(IOException::class) suspend fun uploadTextFiletoS3(bucketName: String?) { val fileSuffix = SimpleDateFormat("yyyyMMddHHmmss").format(Date()) val fileName = "TextFile$fileSuffix.txt" val myFile = File(fileName) val fw = FileWriter(myFile.absoluteFile) val bw = BufferedWriter(fw) bw.write("This is a sample file for testing uploads.") bw.close() val putOb = PutObjectRequest { bucket = bucketName key = fileName body = myFile.asByteStream() } S3Client { region = "us-east-1" }.use { s3Client -> s3Client.putObject(putOb) } } suspend fun listTargetRules(topicArnVal: String?) { val ruleNamesByTargetRequest = ListRuleNamesByTargetRequest { targetArn = topicArnVal } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listRuleNamesByTarget(ruleNamesByTargetRequest) response.ruleNames?.forEach { rule -> println("The rule name is $rule") } } } suspend fun listTargets(ruleName: String?) { val ruleRequest = ListTargetsByRuleRequest { rule = ruleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listTargetsByRule(ruleRequest) response.targets?.forEach { target -> println("Target ARN: ${target.arn}") } } } // 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.") } } suspend fun subEmail( topicArnVal: String?, email: String?, ) { val request = SubscribeRequest { protocol = "email" endpoint = email returnSubscriptionArn = true topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.subscribe(request) println(" Subscription ARN: ${result.subscriptionArn}") } } suspend fun createSnsTopic(topicName: String): String? { val topicPolicy = "{" + "\"Version\": \"2012-10-17\"," + "\"Statement\": [{" + "\"Sid\": \"EventBridgePublishTopic\"," + "\"Effect\": \"Allow\"," + "\"Principal\": {" + "\"Service\": \"events.amazonaws.com\"" + "}," + "\"Resource\": \"*\"," + "\"Action\": \"sns:Publish\"" + "}]" + "}" val topicAttributes = mutableMapOf<String, String>() topicAttributes["Policy"] = topicPolicy val topicRequest = CreateTopicRequest { name = topicName attributes = topicAttributes } SnsClient { region = "us-east-1" }.use { snsClient -> val response = snsClient.createTopic(topicRequest) println("Added topic $topicName for email subscriptions.") return response.topicArn } } suspend fun listRules() { val rulesRequest = ListRulesRequest { eventBusName = "default" limit = 10 } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listRules(rulesRequest) response.rules?.forEach { rule -> println("The rule name is ${rule.name}") println("The rule ARN is ${rule.arn}") } } } // Create a new event rule that triggers when an Amazon S3 object is created in a bucket. suspend fun addEventRule( roleArnVal: String?, bucketName: String, eventRuleName: String?, ) { val pattern = """{ "source": ["aws.s3"], "detail-type": ["Object Created"], "detail": { "bucket": { "name": ["$bucketName"] } } }""" val ruleRequest = PutRuleRequest { description = "Created by using the AWS SDK for Kotlin" name = eventRuleName eventPattern = pattern roleArn = roleArnVal } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val ruleResponse = eventBrClient.putRule(ruleRequest) println("The ARN of the new rule is ${ruleResponse.ruleArn}") } } // Set the Amazon S3 bucket notification configuration. suspend fun setBucketNotification(bucketName: String) { val eventBridgeConfig = EventBridgeConfiguration { } val configuration = NotificationConfiguration { eventBridgeConfiguration = eventBridgeConfig } val configurationRequest = PutBucketNotificationConfigurationRequest { bucket = bucketName notificationConfiguration = configuration skipDestinationValidation = true } S3Client { region = "us-east-1" }.use { s3Client -> s3Client.putBucketNotificationConfiguration(configurationRequest) println("Added bucket $bucketName with EventBridge events enabled.") } } // Create an S3 bucket using a waiter. suspend fun createBucket(bucketName: String) { val request = CreateBucketRequest { bucket = bucketName } S3Client { region = "us-east-1" }.use { s3 -> s3.createBucket(request) s3.waitUntilBucketExists { bucket = bucketName } println("$bucketName is ready") } } suspend fun checkBucket(bucketName: String?): Boolean { try { // Determine if the S3 bucket exists. val headBucketRequest = HeadBucketRequest { bucket = bucketName } S3Client { region = "us-east-1" }.use { s3Client -> s3Client.headBucket(headBucketRequest) return true } } catch (e: S3Exception) { System.err.println(e.message) } return false } suspend fun createIAMRole( rolenameVal: String?, polJSON: String?, ): String? { val request = CreateRoleRequest { roleName = rolenameVal assumeRolePolicyDocument = polJSON description = "Created using the AWS SDK for Kotlin" } val rolePolicyRequest = AttachRolePolicyRequest { roleName = rolenameVal policyArn = "arn:aws:iam::aws:policy/AmazonEventBridgeFullAccess" } IamClient { region = "us-east-1" }.use { iam -> val response = iam.createRole(request) iam.attachRolePolicy(rolePolicyRequest) return response.role?.arn } }
-
Para API obtener más información, consulta los siguientes temas en la sección AWS SDKde API referencia sobre Kotlin.
-
Acciones
En el siguiente ejemplo de código se muestra cómo usar DeleteRule
.
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. suspend fun deleteRuleByName(ruleName: String?) { val ruleRequest = DeleteRuleRequest { name = ruleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.deleteRule(ruleRequest) println("Successfully deleted the rule") } }
-
Para API obtener más información, consulta DeleteRule
la AWS SDKAPIreferencia sobre Kotlin.
-
En el siguiente ejemplo de código se muestra cómo usar DescribeRule
.
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. suspend fun checkRule(eventRuleName: String?) { val ruleRequest = DescribeRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.describeRule(ruleRequest) println("The state of the rule is $response") } }
-
Para API obtener más información, consulta DescribeRule
la AWS SDKAPIreferencia sobre Kotlin.
-
En el siguiente ejemplo de código se muestra cómo usar DisableRule
.
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. suspend fun changeRuleState( eventRuleName: String, isEnabled: Boolean?, ) { if (!isEnabled!!) { println("Disabling the rule: $eventRuleName") val ruleRequest = DisableRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.disableRule(ruleRequest) } } else { println("Enabling the rule: $eventRuleName") val ruleRequest = EnableRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.enableRule(ruleRequest) } } }
-
Para API obtener más información, consulta DisableRule
la AWS SDKAPIreferencia sobre Kotlin.
-
En el siguiente ejemplo de código se muestra cómo usar EnableRule
.
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. suspend fun changeRuleState( eventRuleName: String, isEnabled: Boolean?, ) { if (!isEnabled!!) { println("Disabling the rule: $eventRuleName") val ruleRequest = DisableRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.disableRule(ruleRequest) } } else { println("Enabling the rule: $eventRuleName") val ruleRequest = EnableRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.enableRule(ruleRequest) } } }
-
Para API obtener más información, consulta EnableRule
la AWS SDKAPIreferencia sobre Kotlin.
-
En el siguiente ejemplo de código se muestra cómo usar ListRuleNamesByTarget
.
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. suspend fun listTargetRules(topicArnVal: String?) { val ruleNamesByTargetRequest = ListRuleNamesByTargetRequest { targetArn = topicArnVal } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listRuleNamesByTarget(ruleNamesByTargetRequest) response.ruleNames?.forEach { rule -> println("The rule name is $rule") } } }
-
Para API obtener más información, consulta ListRuleNamesByTarget
la AWS SDKAPIreferencia sobre Kotlin.
-
En el siguiente ejemplo de código se muestra cómo usar ListRules
.
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. suspend fun listRules() { val rulesRequest = ListRulesRequest { eventBusName = "default" limit = 10 } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listRules(rulesRequest) response.rules?.forEach { rule -> println("The rule name is ${rule.name}") println("The rule ARN is ${rule.arn}") } } }
-
Para API obtener más información, consulta ListRules
la AWS SDKAPIreferencia sobre Kotlin.
-
En el siguiente ejemplo de código se muestra cómo usar ListTargetsByRule
.
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. suspend fun listTargets(ruleName: String?) { val ruleRequest = ListTargetsByRuleRequest { rule = ruleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listTargetsByRule(ruleRequest) response.targets?.forEach { target -> println("Target ARN: ${target.arn}") } } }
-
Para API obtener más información, consulta ListTargetsByRule
la AWS SDKAPIreferencia sobre Kotlin.
-
En el siguiente ejemplo de código se muestra cómo usar PutEvents
.
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. suspend fun triggerCustomRule(email: String) { val json = "{" + "\"UserEmail\": \"" + email + "\"," + "\"Message\": \"This event was generated by example code.\"" + "\"UtcTime\": \"Now.\"" + "}" val entry = PutEventsRequestEntry { source = "ExampleSource" detail = json detailType = "ExampleType" } val eventsRequest = PutEventsRequest { this.entries = listOf(entry) } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.putEvents(eventsRequest) } }
-
Para API obtener más información, consulta PutEvents
la AWS SDKAPIreferencia sobre Kotlin.
-
En el siguiente ejemplo de código se muestra cómo usar PutRule
.
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Crear una regla programada.
suspend fun createScRule( ruleName: String?, cronExpression: String?, ) { val ruleRequest = PutRuleRequest { name = ruleName eventBusName = "default" scheduleExpression = cronExpression state = RuleState.Enabled description = "A test rule that runs on a schedule created by the Kotlin API" } EventBridgeClient { region = "us-west-2" }.use { eventBrClient -> val ruleResponse = eventBrClient.putRule(ruleRequest) println("The ARN of the new rule is ${ruleResponse.ruleArn}") } }
Crear una regla que se active cuando se agregue un objeto a un bucket de Amazon Simple Storage Service.
// Create a new event rule that triggers when an Amazon S3 object is created in a bucket. suspend fun addEventRule( roleArnVal: String?, bucketName: String, eventRuleName: String?, ) { val pattern = """{ "source": ["aws.s3"], "detail-type": ["Object Created"], "detail": { "bucket": { "name": ["$bucketName"] } } }""" val ruleRequest = PutRuleRequest { description = "Created by using the AWS SDK for Kotlin" name = eventRuleName eventPattern = pattern roleArn = roleArnVal } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val ruleResponse = eventBrClient.putRule(ruleRequest) println("The ARN of the new rule is ${ruleResponse.ruleArn}") } }
-
Para API obtener más información, consulta PutRule
la AWS SDKAPIreferencia sobre Kotlin.
-
En el siguiente ejemplo de código se muestra cómo usar PutTargets
.
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. // 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.") } }
Agregue un transformador de entrada al destino de una regla.
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) } }
-
Para API obtener más información, consulta PutTargets
la AWS SDKAPIreferencia sobre Kotlin.
-
En el siguiente ejemplo de código se muestra cómo usar RemoveTargets
.
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. suspend fun deleteTargetsFromRule(eventRuleName: String?) { // First, get all targets that will be deleted. val request = ListTargetsByRuleRequest { rule = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listTargetsByRule(request) val allTargets = response.targets // Get all targets and delete them. if (allTargets != null) { for (myTarget in allTargets) { val removeTargetsRequest = RemoveTargetsRequest { rule = eventRuleName ids = listOf(myTarget.id.toString()) } eventBrClient.removeTargets(removeTargetsRequest) println("Successfully removed the target") } } } }
-
Para API obtener más información, consulta RemoveTargets
la AWS SDKAPIreferencia sobre Kotlin.
-