How do I resolve the CloudFormation error "the resource already exists in the stack"?

3 minute read
2

I want to learn how to resolve the CloudFormation error "the resource already exists in the stack" by changing the resource name or generating a unique name.

Short description

When you create a resource that has the same name and is set to the same value as another resource, AWS CloudFormation can't differentiate between them. You then receive the error message, "Resource already exists in stack." Each resource has a unique physical ID. You can't reuse the physical ID for most resources that are defined in CloudFormation.

To resolve this issue, change the name of the failed resource to a unique name. Or, you can choose not to define the name for that resource. If you don't set a name, then CloudFormation generates a unique name when you create the resource. This unique name doesn't conflict with your existing resources.

Resolution

Note: You can use the following resolution for related errors with resources that exist in a different stack or that you created with other AWS resources. For example, you might receive this error with Amazon Simple Queue Service (Amazon SQS) queues that have an identifier that already exists.

To change the name of the failed resource or generate a unique name, complete the following steps:

  1. In the CloudFormation template that contains the failed resource, check if other explicitly declared resources have the same name as your failed resource.

    In the following example, the stack fails because each AWS Identity and Access Management (IAM) ManagedPolicy resource (ManagedPolicyName) has the same name (FinalS3WritePolicy):

    S3DeletePolicy:
        Type: AWS::IAM::ManagedPolicy
        Properties:
          ManagedPolicyName:
            Fn::Join:
            - _
            - - FinalS3DeletePolicy
              - Ref: EnvType
          PolicyDocument:
    ........
    ........
    S3WritePolicy:
        Type: AWS::IAM::ManagedPolicy
        Properties:
          ManagedPolicyName:
            Fn::Join:
            - _
            - - FinalS3WritePolicy
              - Ref: EnvType
          PolicyDocument:
    ........
    ........
  2. Update the name of any resource that has a duplicate name. For example, change the first occurrence of FinalS3WritePolicy to FinalS3DeletePolicy. Or, remove the name.

    In the following examples, Stack A succeeds because each IAM ManagedPolicy resource has the unique names FinalS3DeletePolicy and FinalS3WritePolicy. Stack B succeeds because no name values are set for either ManagedPolicyName properties. When the resource is created, CloudFormation automatically generates a unique name for each IAM ManagedPolicy resource in Stack B.

    Stack A:

    S3DeletePolicy:  
        Type: AWS::IAM::ManagedPolicy  
        Properties:  
          ManagedPolicyName:  
            Fn::Join:  
            - _  
            - - FinalS3DeletePolicy  
              - Ref: EnvType  
          PolicyDocument:  
    ........  
    ........  
    S3WritePolicy:  
        Type: AWS::IAM::ManagedPolicy  
        Properties:  
          ManagedPolicyName:  
            Fn::Join:  
            - _  
            - - FinalS3WritePolicy  
              - Ref: EnvType  
          PolicyDocument:  
    ........  
    ........

    Stack B:

    S3DeletePolicy:
        Type: AWS::IAM::ManagedPolicy
        Properties:
          PolicyDocument:
    ........
    ........
    S3WritePolicy:
        Type: AWS::IAM::ManagedPolicy
        Properties:
          PolicyDocument:
    ........
    ........
AWS OFFICIAL
AWS OFFICIALUpdated 6 months ago