Publish/Subscribe policy examples
The policy you use depends on how you're connecting to AWS IoT Core. You can connect to AWS IoT Core by using an MQTT client, HTTP, or WebSocket. When you connect with an MQTT client, you're authenticating with an X.509 certificate. When you connect over HTTP or the WebSocket protocol, you're authenticating with Signature Version 4 and Amazon Cognito.
Note
For registered devices, we recommend that you use thing policy variables for
Connect actions and attach the thing to the principal
that's used for the connection.
In this section:
Policies to publish, subscribe and receive messages to/from specific topics
Policies to publish, subscribe and receive messages to/from topics with a specific prefix
Policies to publish, subscribe and receive messages to/from topics specific to each device
Policies to deny publishing messages to subtopics of a topic name
Policies to deny receiving messages from subtopics of a topic name
Policies to subscribe to topics using MQTT wildcard characters
Using wildcard characters in MQTT and AWS IoT Core policies
MQTT and AWS IoT Core policies have different wildcard characters and you
should choose them after careful consideration. In MQTT, the wildcard
characters + and # are used in MQTT topic
filters to subscribe to multiple topic names. AWS IoT Core policies
use * and ? as wildcard characters and follow the
conventions of IAM policies. In a policy document, the *
represents any combination of characters and a question mark ?
represents any single character. In policy documents, the MQTT wildcard
characters, + and # are treated as those
characters with no special meaning. To describe multiple topic names and
topic filters in the resource attribute of a policy, use the
* and ? wildcard characters in place of the
MQTT wildcard characters.
When you choose the wildcard characters to use in a policy document,
consider that the * character is not confined to a single topic
level. The + character is confined to a single topic level in
an MQTT topic filter. To help constrain a wildcard specification to a single
MQTT topic filter level, consider using multiple ? characters.
For more information about using wildcard characters in a policy resource
and more examples of what they match, see Using wildcards in resource ARNs.
The table below shows the different wildcard characters used in MQTT and AWS IoT Core policies for MQTT clients.
| Wildcard character | Is MQTT wildcard character | Example in MQTT | Is AWS IoT Core policy wildcard character | Example in AWS IoT Core policies for MQTT clients |
|---|---|---|---|---|
# |
Yes | some/# |
No | N/A |
+ |
Yes | some/+/topic |
No | N/A |
* |
No | N/A | Yes |
|
? |
No | N/A | Yes |
|
Policies to publish, subscribe and receive messages to/from specific topics
The following shows examples for registered and unregistered devices to
publish, subscribe and receive messages to/from the topic named
"some_specific_topic". The examples also highlight that Publish
and Receive use "topic" as the resource, and
Subscribe uses "topicfilter" as the resource.
Policies to publish, subscribe and receive messages to/from topics with a specific prefix
The following shows examples for registered and unregistered devices to publish, subscribe and receive messages to/from topics prefixed with "topic_prefix".