Overview
In this post we will try to solve one of the issue during CloudFormation stack creation or update.
Most of the time we can get this issue during CloudFormation stack update. Because the error message is clearly saying that the currently used subnet’s availability zone and the availability zone defined in AutoScalingGroup is not matching.
Let’s take an example so that it will be easier to understand the problem.
Example 1: Stack is already created and we are trying to update the stack where we have changed the AZs or Subnets.
- Suppose, we have already created an AutoScalingGroup and that is using the subnet-12345 that is created inside the Availability Zone us-west-2a.
- Now, we change AZ to us-west-2b and try to update the stack then we may see this error.
Because, the subnet-12345 was created in AZ us-west-2a and subnet does not matches with the new Availability Zone.
Example 2: Creating a new stack
The possibility of this error while creating a new stack:
- Suppose, we have created our subnet-12345 in availability zone us-west-2a but we set the availability zone us-west-2b.
- Or, we set value for subnet to subnet-12346 (which was created in availability zone us-west-2b) with us-west-2a.
Solution
Auto Scaling Group will try to launch an instance randomly choosing any one availability zone from the available list. So, from the above example, it may choose an Availability Zone that might not have subnet created. See the code snippet:
MyASG:
Type: "AWS::AutoScaling::AutoScalingGroup"
Properties:
LaunchConfigurationName: !Ref MyLC
AvailabilityZones:
Fn::GetAZs: "" # Thhis will assign all the availability zones from the region where this stack is going to be created/updated
VPCZoneIdentifier:
- subnet-12345 # This subnet is created in us-west-2a
In the above code snippet, If the stack is going to be created in us-west-2 region then, the availability zones would be:
- us-west-2a
- us-west-2b
- us-west-2c
- us-west-2d
Now, the ASG can pick Availability Zone us-west-2b and we have set subnet subnet-12345. In this case, stack creation/update will fail and shows the error:
The availability zones of the specified subnets and the AutoScalingGroup do not match
Because, Availability Zone and subnet does not match.
So, the solution is: Create at least one subnet inside each availability zone and define the multiple value for VPC zone identifier.
See the updated code:
MyASG:
Type: "AWS::AutoScaling::AutoScalingGroup"
Properties:
LaunchConfigurationName: !Ref MyLC
AvailabilityZones:
Fn::GetAZs: ""
VPCZoneIdentifier:
- subnet-12345 # This subnet is created in us-west-2a
- subnet-12346 # This subnet is created in us-west-2b
- subnet-12347 # This subnet is created in us-west-2c
- subnet-12348 # This subnet is created in us-west-2d
Conclusion
In this post, we learn the possibility of showing the error: The availability zones of the specified subnets and the AutoScalingGroup do not match.
We also learn a possible solution for this.