Skip to content

How do I resolve the "Resource already exists in the stack" error for my CloudFormation stack?

3 minute read
2

I want to troubleshoot the "Resource already exists in the stack" error for my stack in AWS CloudFormation.

Short description

Each resource has a unique physical ID, and you can't reuse the physical ID for most resources that are defined in CloudFormation.

If you create a stack and the resources have the same name and the same value as another resource, then CloudFormation can't differentiate between the resources. Then, you receive the "Resource already exists in stack" error message. Also, you might receive this error if resources, that CloudFormation doesn't define or manage, with the same physical ID exist in the AWS account or AWS Region.

To resolve this issue, change the name of the failed resource to a unique name, or don't define the name for that resource. If you don't define 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 services. For example, you might receive this error with Amazon Simple Queue Service (Amazon SQS) queues that have an identifier that already exists.

Check the name of your resources

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 has the same FinalS3WritePolicy name:

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

Update the name of your duplicate resource

Update the name of a 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.

Example for 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:  
........  
........

Example for Stack B:

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