How do I resolve the "Custom Named Resource already exists in stack" error in AWS CloudFormation?

Last updated: 2019-05-23

My AWS CloudFormation stack fails to create a resource, and I receive an error message telling me that my resource already exists in the stack. How can I resolve this error?

Short Description

You get this error from AWS CloudFormation when you have one or more custom-named resources with the same name set to the same value. Each custom-named resource has a unique Physical ID. You can't reuse the Physical ID for most resources defined in AWS CloudFormation.

You can resolve this error by changing the name of the repeating or failing resource to a unique name. Or, you can choose to not define the custom name for that resource. If you don't set a custom name, then AWS CloudFormation generates a unique name when the resource is created. This unique name won't conflict with your existing resources.

Resolution

1.    In the AWS CloudFormation template that contains your failing resource, check whether 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 custom name (FinalS3WritePolicy).

# This stack will fail
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:
........
........

2.    Update the name of any resource that has a duplicate name. For example, change the first instance of FinalS3WritePolicy in the preceding example to FinalS3DeletePolicy. Or, you can remove the custom name.

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

Stack A:

# This stack will Succeed as unique names are set:
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:

# This stack will Succeed as no names are set     
S3DeletePolicy:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      PolicyDocument:
........
........
S3WritePolicy:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      PolicyDocument:
........
........

Note: You can use the resolution in this article for related errors involving resources existing in a different stack or resources created outside of AWS CloudFormation.


Did this article help you?

Anything we could improve?


Need more help?