本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
連接政策範例
下列政策拒絕用戶端 IDsclient1
和 連線client2
的許可 AWS IoT Core,同時允許裝置使用用戶端 ID 連線。用戶端 ID 符合在 AWS IoT Core 登錄檔中註冊並連接到用於連線之主體的物件名稱:
對於已註冊的裝置,建議您使用 Connect
動作的物件政策變數,並將物件附加至用於連線的主體。
- JSON
-
-
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Action": [
"iot:Connect"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:client/client1",
"arn:aws:iot:us-east-1:123456789012:client/client2"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:client/${iot:Connection.Thing.ThingName}"
],
"Condition": {
"Bool": {
"iot:Connection.Thing.IsAttached": "true"
}
}
}
]
}
下列政策會授予使用 AWS IoT Core 用戶端 ID 連線至 的許可client1
。此政策範例適用於未註冊的裝置。
- JSON
-
-
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:client/client1"
]
}
]
}
MQTT 持續工作階段政策範例
connectAttributes
可讓您在 IAM 政策中指定要在連線訊息中使用的屬性,例如 PersistentConnect
和 LastWill
。如需詳細資訊,請參閱使用 connectAttributes。
下列政策允許與 PersistentConnect
功能連接:
- JSON
-
-
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": "arn:aws:iot:us-east-1:123456789012:client/client1",
"Condition": {
"ForAllValues:StringEquals": {
"iot:ConnectAttributes": [
"PersistentConnect"
]
}
}
}
]
}
下列政策不允許 PersistentConnect
,允許其他功能:
- JSON
-
-
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": "arn:aws:iot:us-east-1:123456789012:client/client1",
"Condition": {
"ForAllValues:StringNotEquals": {
"iot:ConnectAttributes": [
"PersistentConnect"
]
}
}
}
]
}
上述政策也可以使用 StringEquals
表達,並允許任何其他功能,包括新功能:
- JSON
-
-
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": "arn:aws:iot:us-east-1:123456789012:client/client1",
},
{
"Effect": "Deny",
"Action": [
"iot:Connect"
],
"Resource": "*",
"Condition": {
"ForAnyValue:StringEquals": {
"iot:ConnectAttributes": [
"PersistentConnect"
]
}
}
}
]
}
下列政策允許透過 PersistentConnect
和 LastWill
連接,但不允許任何其他新功能:
- JSON
-
-
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": "arn:aws:iot:us-east-1:123456789012:client/client1",
"Condition": {
"ForAllValues:StringEquals": {
"iot:ConnectAttributes": [
"PersistentConnect",
"LastWill"
]
}
}
}
]
}
下列政策允許透過具有或沒有 LastWill
的用戶端進行全新連接,但不允許任何其他功能:
- JSON
-
-
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": "arn:aws:iot:us-east-1:123456789012:client/client1",
"Condition": {
"ForAllValues:StringEquals": {
"iot:ConnectAttributes": [
"LastWill"
]
}
}
}
]
}
下列政策只允許使用預設功能進行連接:
- JSON
-
-
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": "arn:aws:iot:us-east-1:123456789012:client/client1",
"Condition": {
"ForAllValues:StringEquals": {
"iot:ConnectAttributes": []
}
}
}
]
}
下列政策僅允許與 PersistentConnect
連接,而且只要連線使用 PersistentConnect
,就允許任何新功能:
- JSON
-
-
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": "arn:aws:iot:us-east-1:123456789012:client/client1",
"Condition": {
"ForAnyValue:StringEquals": {
"iot:ConnectAttributes": [
"PersistentConnect"
]
}
}
}
]
}
下列政策指出連接必須同時使用 PersistentConnect
和 LastWill
,而且不允許任何新功能:
- JSON
-
-
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": "arn:aws:iot:us-east-1:123456789012:client/client1",
"Condition": {
"ForAllValues:StringEquals": {
"iot:ConnectAttributes": [
"PersistentConnect",
"LastWill"
]
}
}
},
{
"Effect": "Deny",
"Action": [
"iot:Connect"
],
"Resource": "*",
"Condition": {
"ForAllValues:StringEquals": {
"iot:ConnectAttributes": [
"PersistentConnect"
]
}
}
},
{
"Effect": "Deny",
"Action": [
"iot:Connect"
],
"Resource": "*",
"Condition": {
"ForAllValues:StringEquals": {
"iot:ConnectAttributes": [
"LastWill"
]
}
}
},
{
"Effect": "Deny",
"Action": [
"iot:Connect"
],
"Resource": "*",
"Condition": {
"ForAllValues:StringEquals": {
"iot:ConnectAttributes": []
}
}
}
]
}
下列政策不得具有 PersistentConnect
,但可以具有 LastWill
,而且不允許任何其他新功能:
- JSON
-
-
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Action": [
"iot:Connect"
],
"Resource": "*",
"Condition": {
"ForAnyValue:StringEquals": {
"iot:ConnectAttributes": [
"PersistentConnect"
]
}
}
},
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": "arn:aws:iot:us-east-1:123456789012:client/client1",
"Condition": {
"ForAllValues:StringEquals": {
"iot:ConnectAttributes": [
"LastWill"
]
}
}
}
]
}
下列政策只允許透過具有 LastWill
與主題 "my/lastwill/topicName"
的用戶端連接,而且只要其使用 LastWill
主題,就允許任何功能:
- JSON
-
-
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": "arn:aws:iot:us-east-1:123456789012:client/client1",
"Condition": {
"ArnEquals": {
"iot:LastWillTopic": "arn:aws:iot:region:account-id:topic/my/lastwill/topicName"
}
}
}
]
}
下列政策僅允許使用特定的 LastWillTopic
進行全新連接,而且只要其使用 LastWillTopic
,就允許任何功能:
- JSON
-
-
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": "arn:aws:iot:us-east-1:123456789012:client/client1",
"Condition": {
"ArnEquals": {
"iot:LastWillTopic": "arn:aws:iot:region:account-id:topic/my/lastwill/topicName"
}
}
},
{
"Effect": "Deny",
"Action": [
"iot:Connect"
],
"Resource": "*",
"Condition": {
"ForAnyValue:StringEquals": {
"iot:ConnectAttributes": [
"PersistentConnect"
]
}
}
}
]
}