AR8
AR8

Reputation: 1

AWS CloudFormation: Resource handler returned message: "The CIDR '10.240.20.128/25' conflicts with another subnet

I have created a subnet Az1 Sync Subnet (10.240.20.128/25) and deployed it. Now I am trying to change the name of the subnet from "Sync" to "HA" subnet - "Az1 HA Subnet"

So i changed the name of the subnet

Mappings: VPCaddressPrefix: values: vpcOctet: '10.240'

Before: (This code is already successfully deployed and a subnet exists in AWS )

Az1SyncSubnet: Properties: AvailabilityZone: !FindInMap [ VPCaddressPrefix,values,AvailabilityZone1 ] CidrBlock: !Join - . - - !FindInMap [ VPCaddressPrefix,values,vpcOctet ] - '20' - 128/25 Tags: - Key: Name Value: Az1 SyncSubnet - Key: Owner Value: !FindInMap [ VPCaddressPrefix,values,Owner ] VpcId: !Ref Vpc Type: 'AWS::EC2::Subnet'

After change: Az1HASubnet: Properties: AvailabilityZone: !FindInMap [ VPCaddressPrefix,values,AvailabilityZone1 ] CidrBlock: !Join - . - - !FindInMap [ VPCaddressPrefix,values,vpcOctet ] - '20' - 128/25 Tags: - Key: Name Value: Az1 HA Subnet - Key: Owner Value: !FindInMap [ VPCaddressPrefix,values,Owner ] VpcId: !Ref Vpc Type: 'AWS::EC2::Subnet'

But getting error as "Az1HASubnet - CREATE_FAILED: Resource handler returned message: "The CIDR '10.240.20.128/25' conflicts with another subnet (Service: Ec2, Status Code: 400, Request ID: 788f2216-1f79-4596-8430-7d45e8bbee1c)" (RequestToken: f3a4525d-3f44-5273-196a-764012876a33, HandlerErrorCode: AlreadyExists)

Is it not possible to change the name of subnet after deployed ? Any work around hwo to change the name of the subnet ?

Upvotes: 0

Views: 218

Answers (1)

BrianV
BrianV

Reputation: 1496

You are trying to change the logical ID of the resource in CloudFormation. That is not possible, as CloudFormation does not know that the old resource is the same as the new resource. When it sees this update, it believes you simultaneously added a new subnet with the new name while deleting the old one.

The error is happening because CloudFormation is trying to create a new subnet with the existing configuration, thus the conflicting CIDR. (It is trying to add the newly discovered Subnet before deleting the old one.)

If you really want to define a new logical ID for a CloudFormation resource, you must do the following:

  1. Set the DeletionPolicy to Retain the resource upon deletion.
  2. Next, remove the resource from the stack. (It will not be deleted in your AWS environment due to the DeletionPolicy.)
  3. Import the existing resource into your CloudFormation stack with the desired logical ID.

Upvotes: 0

Related Questions