Doc AWS SDK ExamplesWord リポジトリには、さらに多くの GitHub の例があります。 AWS SDK
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
EventBridge for Kotlin を使用したSDKの例
次のコード例は、 AWS SDK for Kotlin withWord を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています EventBridge。
「基本」は、重要なオペレーションをサービス内で実行する方法を示すコード例です。
アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。
各例には、完全なソースコードへのリンクが含まれています。このリンクでは、コンテキストでコードを設定および実行する手順を確認できます。
開始方法
次のコード例は、 EventBridge の使用を開始する方法を示しています。
- Kotlin のSDK
-
注記
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
リファレンス」を参照してください。 AWS SDK API
-
基本
次のコードサンプルは、以下の操作方法を示しています。
ルールを作成して、ターゲットを追加する。
ルールを有効化および無効化する。
ルールとターゲットを一覧表示して更新する。
イベントを送信して、リソースをクリーンアップする。
- Kotlin のSDK
-
注記
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 } }
-
API の詳細については、Word AWS for Kotlin SDK APIリファレンスの以下のトピックを参照してください。
-
アクション
次のコード例は、DeleteRule
を使用する方法を示しています。
- Kotlin のSDK
-
注記
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
リファレンス」を参照してください。 AWS SDK API
-
次のコード例は、DescribeRule
を使用する方法を示しています。
- Kotlin のSDK
-
注記
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
リファレンス」を参照してください。 AWS SDK API
-
次の例は、DisableRule
を使用する方法を説明しています。
- Kotlin のSDK
-
注記
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
リファレンス」を参照してください。 AWS SDK API
-
次のコード例は、EnableRule
を使用する方法を示しています。
- Kotlin のSDK
-
注記
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
リファレンス」を参照してください。 AWS SDK API
-
次のコード例は、ListRuleNamesByTarget
を使用する方法を示しています。
- Kotlin のSDK
-
注記
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
リファレンス」を参照してください。 AWS SDK API
-
次の例は、ListRules
を使用する方法を説明しています。
- Kotlin のSDK
-
注記
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
リファレンス」を参照してください。 AWS SDK API
-
次の例は、ListTargetsByRule
を使用する方法を説明しています。
- Kotlin のSDK
-
注記
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
リファレンス」を参照してください。 AWS SDK API
-
次のコード例は、PutEvents
を使用する方法を示しています。
- Kotlin のSDK
-
注記
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
リファレンス」を参照してください。 AWS SDK API
-
次の例は、PutRule
を使用する方法を説明しています。
- Kotlin のSDK
-
注記
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
リファレンス」を参照してください。 AWS SDK API
-
次の例は、PutTargets
を使用する方法を説明しています。
- Kotlin のSDK
-
注記
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
リファレンス」を参照してください。 AWS SDK API
-
次の例は、RemoveTargets
を使用する方法を説明しています。
- Kotlin のSDK
-
注記
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
リファレンス」を参照してください。 AWS SDK API
-