EventBridge exemplos de uso SDK para Kotlin - 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á.

EventBridge exemplos de uso SDK para Kotlin

Os exemplos de código a seguir mostram como realizar ações e implementar cenários comuns usando o AWS SDK for Kotlin with. EventBridge

As noções básicas são exemplos de código que mostram como realizar as operações essenciais em um serviço.

Ações são trechos de código de programas maiores e devem ser executadas em contexto. Embora as ações mostrem como chamar funções de serviço individuais, você pode ver as ações no contexto em seus cenários relacionados.

Cada exemplo inclui um link para o código-fonte completo, onde você pode encontrar instruções sobre como configurar e executar o código no contexto.

Conceitos básicos

Os exemplos de código a seguir mostram como começar a usar EventBridge.

SDKpara Kotlin
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 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 obter detalhes, consulte a ListEventBusesreferência AWS SDKdo Kotlin API.

Conceitos básicos

O exemplo de código a seguir mostra como:

  • Criar uma regra e adicionar um destino a ela.

  • Habilitar e desabilitar regras.

  • Listar e atualizar regras e destinos.

  • Enviar eventos e, em seguida, limpar os recursos.

SDKpara Kotlin
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.

/* 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 } }

Ações

O código de exemplo a seguir mostra como usar DeleteRule.

SDKpara Kotlin
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.

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 obter detalhes, consulte a DeleteRulereferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar DescribeRule.

SDKpara Kotlin
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.

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 obter detalhes, consulte a DescribeRulereferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar DisableRule.

SDKpara Kotlin
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.

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 obter detalhes, consulte a DisableRulereferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar EnableRule.

SDKpara Kotlin
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.

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 obter detalhes, consulte a EnableRulereferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar ListRuleNamesByTarget.

SDKpara Kotlin
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.

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") } } }

O código de exemplo a seguir mostra como usar ListRules.

SDKpara Kotlin
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.

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 obter detalhes, consulte a ListRulesreferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar ListTargetsByRule.

SDKpara Kotlin
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.

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 obter detalhes, consulte a ListTargetsByRulereferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar PutEvents.

SDKpara Kotlin
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.

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 obter detalhes, consulte a PutEventsreferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar PutRule.

SDKpara Kotlin
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.

Criar uma regra agendada

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}") } }

Crie uma regra que seja acionada quando um objeto é adicionado a um bucket do 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 obter detalhes, consulte a PutRulereferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar PutTargets.

SDKpara Kotlin
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.

// 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.") } }

Adicione um transformador de entrada a um destino para uma regra.

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 obter detalhes, consulte a PutTargetsreferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar RemoveTargets.

SDKpara Kotlin
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.

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 obter detalhes, consulte a RemoveTargetsreferência AWS SDKdo Kotlin API.