AWS IoT 策略未插入策略变量 (iot:ClientId)

AWS IoT policy not inserting policy variables (iot:ClientId)

我正在尝试按照本文档 https://aws.amazon.com/blogs/iot/setting-up-just-in-time-provisioning-with-aws-iot-core/ 中的说明设置即时供应 (JITP)。我设法创建了一个脚本来创建所有 CA 证书,将它们注册到 AWS CLI 并将配置模板附加到证书。当我第一次尝试连接设备时,证书和设备是正常创建的,但我创建的附加到证书的策略没有插入 {iot:clientID}。我也尝试使用其他变量,例如 {iot:Connection.Thing.ThingName} 但似乎没有任何效果。 我能够通过将 clientID 硬编码到我的策略模板中来验证代码和其他一切是否正常工作,所以唯一的问题似乎是没有插入变量。

我想这可能是因为 IAM 角色没有足够的权限?目前它只有上面文档中提到的 AWSIoTThingsRegistration、AWSIOTLogging 和 AWSIOTRulesAction 权限。或者可能只是在自动配置设备时策略不能使用这些变量,尽管配置模板文档中没有任何建议?

我的政策供参考:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:Publish",
      "Resource": "arn:aws:iot:eu-west-1:ACCOUNTID:topic/PROJECTTOPIC/"
    },
    {
      "Effect": "Allow",
      "Action": [
        "iot:Connect"
      ],
      "Resource": [
        "arn:aws:iot:eu-west-1:ACCOUNTID:client/${iot:ClientId}"
      ]
    },
    {
      "Effect": "Allow",
      "Action": "iot:Receive",
      "Resource": [
        "arn:aws:iot:eu-west-1:ACCOUNTID:topic/PROJECTTOPIC/${iot:ClientId}"
      ]
    },
    {
      "Effect": "Allow",
      "Action": "iot:Subscribe",
      "Resource": [
        "arn:aws:iot:eu-west-1:ACCOUNTID:topicfilter/PROJECTTOPIC/${iot:ClientId}"
      ]
    }
  ]
}

我不知道它是否相关,但这也是我的配置策略:

{
 "templateBody":"{ \"Parameters\" : { \"AWS::IoT::Certificate::Country\" : { \"Type\" : \"String\" }, \"AWS::IoT::Certificate::Id\" : { \"Type\" : \"String\" }, \"AWS::IoT::Certificate::CommonName\" : { \"Type\" : \"String\" } }, \"Resources\" : { \"thing\" : { \"Type\" : \"AWS::IoT::Thing\", \"Properties\" : { \"ThingName\" : {\"Ref\" : \"AWS::IoT::Certificate::CommonName\"}, \"AttributePayload\" : { \"version\" : \"v1\", \"country\" : \"IE\"} } }, \"certificate\" : { \"Type\" : \"AWS::IoT::Certificate\", \"Properties\" : { \"CertificateId\": {\"Ref\" : \"AWS::IoT::Certificate::Id\"}, \"Status\" : \"ACTIVE\" } }, \"policy\" : {\"Type\" : \"AWS::IoT::Policy\", \"Properties\" : { \"PolicyName\" :  \"MYPOLICYNAME\"} } } }",
 "roleArn":"arn:aws:iam::ACCOUNTID:role/MYROLENAME"
}

我对使用 IAM 策略还很陌生,因此非常感谢任何建议或帮助。

原来是权限问题。对于尝试使用 JITP 策略模板的任何人,请尝试将 AWSIoTConfigReadOnlyAccess 添加到您的 IAM 角色。这不是通常的 JITP 操作所必需的,但如果您正在尝试访问事物数据,则它是必需的。