AWS Cloudformation stack creation failure

47 Views Asked by At

I am working on the CloudFormation template for creating the IAM role and multiple policies.

In stack1 I'm creating the new IAM role test-role and the policy test-iam-policy.

In stack2 I'm creating the new IAM policy test-iam-policy2 which again depends on the IAM role test-role.

When creating the CloudFormation stack2, the creation fails with an error

"test-role" role already exists in stack arn:aws:cloudformation:* " CREATE_FAILED

In the stack2 creation, how to ignore the creation of IAM role 'test-roleas it is already exists and create only the IAM policytest-iam-policy2`? enter image description here

enter image description here

1

There are 1 best solutions below

0
MisterSmith On

The error is pretty clear - you cant create 2 roles with the same name, so thats an expected error when creating those two stacks as described. From your question its unclear your exact intention here, so all i can do is point out some available features that some combination should suit your requirements. I would suggest you research the following topics, and then come back and edit your question with some more context to get more specific answers.

(Also please post text not images of code, theres no way im typing all that :).

  1. Sub stacks - are a parent/child relationship between 2 stacks. So your parent stack would define things like roles, then the individual child stacks can use that role to build upon.
  2. If you dont want to use sub-stacks you can still reference one stack from another if you know the name:
    • For stack 1 take look at stack output docs. You could create the role just once here, and include its Role Name or ARN that could be looked up from other stacks.
    • In stack 2 you could add add a parameter of type [AWS::IAM::ROLE], then remove the Role from stack 2, and replace it with the new parameter.
  3. Worst option - you could hard-code a value of the Role created by stack 1 into stack 2 (or some existing role created outside CF) that you know exists/will exist. You can achieve this with a parameter that includes a default value

I would encourage you to review the CloudFormation docs on template anatomy, the intrinsic functions and the pseudo variables. The docs really are your best source of info here.

Its worth pointing out you can cause your self difficulties with sub-stacks and references between stacks when making changes down the road - so try and think ahead when you design your template structure. Check out the CloudFront best practice guide in the AWS docs too