Using CDK/Cfn to add multiple iot policies to a principal

0

Hi, is there an "array" way to add multiple iot policies to a principal? At the moment, I'm doing the following...

const policyPrincipalAttachment1 = new iot.CfnPolicyPrincipalAttachment(this, "policy principal attachment 1", {
  policyName: "iot-policy-1",
  principal: iotCertificate.attrArn,
});

const policyPrincipalAttachment2 = new iot.CfnPolicyPrincipalAttachment(this, "policy principal attachment 2", {
  policyName: "iot-policy-1",
  principal: iotCertificate.attrArn,
});

Thanks Gary

gary
已提問 1 年前檢視次數 244 次
1 個回答
1
已接受的答案

Hi gary,

There is no construct that accept an array of policies to attach to a principal, but, the beauty of CDK is that is programmatic, so that you can loop as

for (var i = 1, i<= N; i++) {
    new iot.CfnPolicyPrincipalAttachment(this, "policy principal attachment " + i, {
      policyName: "iot-policy-" + i,
      principal: iotCertificate.attrArn,
   });
}

Hope it helps ;)

profile picture
專家
已回答 1 年前
  • Thanks alatech, my policy names are actually not incremental but that's ok, I can put them in an array and iterate through it. I only have 4 policies at them moment, so something to consider if things get bigger.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南