쿠키 기본 설정 선택

당사는 사이트와 서비스를 제공하는 데 필요한 필수 쿠키 및 유사한 도구를 사용합니다. 고객이 사이트를 어떻게 사용하는지 파악하고 개선할 수 있도록 성능 쿠키를 사용해 익명의 통계를 수집합니다. 필수 쿠키는 비활성화할 수 없지만 '사용자 지정' 또는 ‘거부’를 클릭하여 성능 쿠키를 거부할 수 있습니다.

사용자가 동의하는 경우 AWS와 승인된 제3자도 쿠키를 사용하여 유용한 사이트 기능을 제공하고, 사용자의 기본 설정을 기억하고, 관련 광고를 비롯한 관련 콘텐츠를 표시합니다. 필수가 아닌 모든 쿠키를 수락하거나 거부하려면 ‘수락’ 또는 ‘거부’를 클릭하세요. 더 자세한 내용을 선택하려면 ‘사용자 정의’를 클릭하세요.

또는와 CreateTopicRuleAWS SDK 함께 사용 CLI

포커스 모드
또는와 CreateTopicRuleAWS SDK 함께 사용 CLI - AWS IoT Core

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

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

다음 코드 예제는 CreateTopicRule의 사용 방법을 보여 줍니다.

C++
SDK C++용
참고

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

//! Create an AWS IoT rule with an SNS topic as the target. /*! \param ruleName: The name for the rule. \param snsTopic: The SNS topic ARN for the action. \param sql: The SQL statement used to query the topic. \param roleARN: The IAM role ARN for the action. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createTopicRule(const Aws::String &ruleName, const Aws::String &snsTopicARN, const Aws::String &sql, const Aws::String &roleARN, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::CreateTopicRuleRequest request; request.SetRuleName(ruleName); Aws::IoT::Model::SnsAction snsAction; snsAction.SetTargetArn(snsTopicARN); snsAction.SetRoleArn(roleARN); Aws::IoT::Model::Action action; action.SetSns(snsAction); Aws::IoT::Model::TopicRulePayload topicRulePayload; topicRulePayload.SetSql(sql); topicRulePayload.SetActions({action}); request.SetTopicRulePayload(topicRulePayload); auto outcome = iotClient.CreateTopicRule(request); if (outcome.IsSuccess()) { std::cout << "Successfully created topic rule " << ruleName << "." << std::endl; } else { std::cerr << "Error creating topic rule " << ruleName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • API 자세한 내용은 AWS SDK for C++ API 참조CreateTopicRule의 섹션을 참조하세요.

CLI
AWS CLI

Amazon SNS 알림을 보내는 규칙을 생성하려면

다음 create-topic-rule 예제에서는 디바이스 섀도우에서 볼 수 있듯이 토양 수분 수준 판독값이 낮을 때 Amazon SNS 메시지를 보내는 규칙을 생성합니다.

aws iot create-topic-rule \ --rule-name "LowMoistureRule" \ --topic-rule-payload file://plant-rule.json

이 예제에서는 다음 JSON 코드를 라는 파일에 저장해야 합니다plant-rule.json.

{ "sql": "SELECT * FROM '$aws/things/MyRPi/shadow/update/accepted' WHERE state.reported.moisture = 'low'\n", "description": "Sends an alert whenever soil moisture level readings are too low.", "ruleDisabled": false, "awsIotSqlVersion": "2016-03-23", "actions": [{ "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:MyRPiLowMoistureTopic", "roleArn": "arn:aws:iam::123456789012:role/service-role/MyRPiLowMoistureTopicRole", "messageFormat": "RAW" } }] }

이 명령은 출력을 생성하지 않습니다.

자세한 내용은 AWS IoT 개발자 안내서의 IoT 규칙 생성을 AWS 참조하세요 IoT.

  • 자세한 API 내용은 AWS CLI 명령 참조CreateTopicRule의 섹션을 참조하세요.

Java
SDK Java 2.x용
참고

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

/** * Creates an IoT rule asynchronously. * * @param roleARN The ARN of the IAM role that grants access to the rule's actions. * @param ruleName The name of the IoT rule. * @param action The ARN of the action to perform when the rule is triggered. * * This method initiates an asynchronous request to create an IoT rule. * If the request is successful, it prints a confirmation message. * If an exception occurs, it prints the error message. */ public void createIoTRule(String roleARN, String ruleName, String action) { String sql = "SELECT * FROM '" + TOPIC + "'"; SnsAction action1 = SnsAction.builder() .targetArn(action) .roleArn(roleARN) .build(); // Create the action. Action myAction = Action.builder() .sns(action1) .build(); // Create the topic rule payload. TopicRulePayload topicRulePayload = TopicRulePayload.builder() .sql(sql) .actions(myAction) .build(); // Create the topic rule request. CreateTopicRuleRequest topicRuleRequest = CreateTopicRuleRequest.builder() .ruleName(ruleName) .topicRulePayload(topicRulePayload) .build(); CompletableFuture<CreateTopicRuleResponse> future = getAsyncClient().createTopicRule(topicRuleRequest); future.whenComplete((response, ex) -> { if (response != null) { System.out.println("IoT Rule created successfully."); } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to create IoT Rule."); } } }); future.join(); }
  • 자세한 API 내용은 AWS SDK for Java 2.x API 참조CreateTopicRule의 섹션을 참조하세요.

Kotlin
SDK Kotlin용
참고

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

suspend fun createIoTRule( roleARNVal: String?, ruleNameVal: String?, action: String?, ) { val sqlVal = "SELECT * FROM '$TOPIC '" val action1 = SnsAction { targetArn = action roleArn = roleARNVal } val myAction = Action { sns = action1 } val topicRulePayloadVal = TopicRulePayload { sql = sqlVal actions = listOf(myAction) } val topicRuleRequest = CreateTopicRuleRequest { ruleName = ruleNameVal topicRulePayload = topicRulePayloadVal } IotClient { region = "us-east-1" }.use { iotClient -> iotClient.createTopicRule(topicRuleRequest) println("IoT rule created successfully.") } }
  • API 자세한 내용은 CreateTopicRuleAWS SDK Kotlin API 참조를 참조하세요.

SDK C++용
참고

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

//! Create an AWS IoT rule with an SNS topic as the target. /*! \param ruleName: The name for the rule. \param snsTopic: The SNS topic ARN for the action. \param sql: The SQL statement used to query the topic. \param roleARN: The IAM role ARN for the action. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createTopicRule(const Aws::String &ruleName, const Aws::String &snsTopicARN, const Aws::String &sql, const Aws::String &roleARN, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::CreateTopicRuleRequest request; request.SetRuleName(ruleName); Aws::IoT::Model::SnsAction snsAction; snsAction.SetTargetArn(snsTopicARN); snsAction.SetRoleArn(roleARN); Aws::IoT::Model::Action action; action.SetSns(snsAction); Aws::IoT::Model::TopicRulePayload topicRulePayload; topicRulePayload.SetSql(sql); topicRulePayload.SetActions({action}); request.SetTopicRulePayload(topicRulePayload); auto outcome = iotClient.CreateTopicRule(request); if (outcome.IsSuccess()) { std::cout << "Successfully created topic rule " << ruleName << "." << std::endl; } else { std::cerr << "Error creating topic rule " << ruleName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • API 자세한 내용은 AWS SDK for C++ API 참조CreateTopicRule의 섹션을 참조하세요.

개발자 안내서 및 코드 예제의 AWS SDK 전체 목록은 섹션을 참조하세요와 AWS IoT 함께 사용 AWS SDK. 이 주제에는 시작하기에 대한 정보와 이전 SDK 버전에 대한 세부 정보도 포함되어 있습니다.

프라이버시사이트 이용 약관쿠키 기본 설정
© 2025, Amazon Web Services, Inc. 또는 계열사. All rights reserved.