Word for Kotlin을 사용한 SDK EventBridge 예제 - AWS SDK 코드 예제

AWS Doc SDK ExamplesWord AWS SDK 리포지토리에는 더 많은 GitHub 예제가 있습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

Word for Kotlin을 사용한 SDK EventBridge 예제

다음 코드 예제에서는 AWS SDK EventBridge를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.

기본 사항은 서비스 내에서 필수 작업을 수행하는 방법을 보여주는 코드 예제입니다.

작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 직접적으로 호출하는 방법을 보여주며 관련 시나리오의 컨텍스트에 맞는 작업을 볼 수 있습니다.

각 예제에는 컨텍스트에서 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있는 전체 소스 코드에 대한 링크가 포함되어 있습니다.

시작

다음 코드 예제에서는 EventBridge 사용을 시작하는 방법을 보여줍니다.

Kotlin용 SDK
참고

더 많은 on GitHub가 있습니다. 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}") } } }
  • API 세부 정보는 Word for Kotlin ListEventBuses 참조의 Word를 참조하세요. AWS SDK API

기본 사항

다음 코드 예시는 다음과 같은 작업을 수행하는 방법을 보여줍니다.

  • 규칙을 만들고 여기에 대상을 추가하세요.

  • 규칙을 활성화 및 비활성화합니다.

  • 규칙과 대상을 나열하고 업데이트합니다.

  • 이벤트를 전송하고 리소스를 정리합니다.

Kotlin용 SDK
참고

더 많은 on GitHub가 있습니다. 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 } }

작업

다음 코드 예시에서는 DeleteRule을 사용하는 방법을 보여 줍니다.

Kotlin용 SDK
참고

더 많은 on GitHub가 있습니다. 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") } }
  • API 세부 정보는 Word for Kotlin DeleteRule 참조의 Word를 참조하세요. AWS SDK API

다음 코드 예시에서는 DescribeRule을 사용하는 방법을 보여 줍니다.

Kotlin용 SDK
참고

더 많은 on GitHub가 있습니다. 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") } }
  • API 세부 정보는 Word for Kotlin DescribeRule 참조의 Word를 참조하세요. AWS SDK API

다음 코드 예시에서는 DisableRule을 사용하는 방법을 보여 줍니다.

Kotlin용 SDK
참고

더 많은 on GitHub가 있습니다. 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) } } }
  • API 세부 정보는 Word for Kotlin DisableRule 참조의 Word를 참조하세요. AWS SDK API

다음 코드 예시에서는 EnableRule을 사용하는 방법을 보여 줍니다.

Kotlin용 SDK
참고

더 많은 on GitHub가 있습니다. 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) } } }
  • API 세부 정보는 Word for Kotlin EnableRule 참조의 Word를 참조하세요. AWS SDK API

다음 코드 예시에서는 ListRuleNamesByTarget을 사용하는 방법을 보여 줍니다.

Kotlin용 SDK
참고

더 많은 on GitHub가 있습니다. 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") } } }
  • API 세부 정보는 Word for Kotlin ListRuleNamesByTarget 참조의 Word를 참조하세요. AWS SDK API

다음 코드 예시에서는 ListRules을 사용하는 방법을 보여 줍니다.

Kotlin용 SDK
참고

더 많은 on GitHub가 있습니다. 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}") } } }
  • API 세부 정보는 Word for Kotlin ListRules 참조의 Word를 참조하세요. AWS SDK API

다음 코드 예시에서는 ListTargetsByRule을 사용하는 방법을 보여 줍니다.

Kotlin용 SDK
참고

더 많은 on GitHub가 있습니다. 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}") } } }
  • API 세부 정보는 Word for Kotlin ListTargetsByRule 참조의 Word를 참조하세요. AWS SDK API

다음 코드 예시에서는 PutEvents을 사용하는 방법을 보여 줍니다.

Kotlin용 SDK
참고

더 많은 on GitHub가 있습니다. 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) } }
  • API 세부 정보는 Word for Kotlin PutEvents 참조의 Word를 참조하세요. AWS SDK API

다음 코드 예시에서는 PutRule을 사용하는 방법을 보여 줍니다.

Kotlin용 SDK
참고

더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

예약된 규칙을 생성합니다.

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

객체가 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}") } }
  • API 세부 정보는 Word for Kotlin PutRule 참조의 Word를 참조하세요. AWS SDK API

다음 코드 예시에서는 PutTargets을 사용하는 방법을 보여 줍니다.

Kotlin용 SDK
참고

더 많은 on GitHub가 있습니다. 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.") } }

규칙의 대상에 입력 변환기를 추가합니다.

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) } }
  • API 세부 정보는 Word for Kotlin PutTargets 참조의 Word를 참조하세요. AWS SDK API

다음 코드 예시에서는 RemoveTargets을 사용하는 방법을 보여 줍니다.

Kotlin용 SDK
참고

더 많은 on GitHub가 있습니다. 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") } } } }
  • API 세부 정보는 Word for Kotlin RemoveTargets 참조의 Word를 참조하세요. AWS SDK API