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
asked a year ago234 views
1 Answer
1
Accepted Answer

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
EXPERT
answered a year ago
  • 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.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions