I'm experimenting with CloudFormation right now. I deployed a stack that included an AutoScalingGroup (and LaunchConfig and CloudFormation::Init), LoadBalancer, a single EC2 instance (and CloudFormation::Init), and a couple security groups.
If I terminate EC2 instances created by the AutoScalingGroup, they get recreated as expected. This is actually quite useful for testing changes to my LaunchConfiguration and CloudFormation::Init settings (basically making sure it works from scratch).
I also have an EC2 instance (named DnsServer1
) that has a public IP. While working on its CloudFormation::Init data, I also manually terminated its instance. I know CloudFormation doesn't restore it like an AutoScalingGroup does, but I was expecting it would be recreated when I ran aws cloudformation update-stack
.
Instead, I get the error: AWS::EC2::Instance DnsServer1 i-014eee8720c4fb542 does not exist
.
Full update log:
11:03:40 UTC-0500 UPDATE_ROLLBACK_COMPLETE AWS::CloudFormation::Stack GregCFTest1
11:03:38 UTC-0500 UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS AWS::CloudFormation::Stack GregCFTest1
11:03:36 UTC-0500 UPDATE_COMPLETE AWS::AutoScaling::LaunchConfiguration WebServerLaunchConfig
11:03:36 UTC-0500 UPDATE_COMPLETE AWS::EC2::Instance DnsServer1
11:03:13 UTC-0500 UPDATE_ROLLBACK_IN_PROGRESS AWS::CloudFormation::Stack GregCFTest1 The following resource(s) failed to update: [DnsServer1].
11:03:11 UTC-0500 UPDATE_COMPLETE AWS::AutoScaling::LaunchConfiguration WebServerLaunchConfig
11:03:10 UTC-0500 UPDATE_FAILED AWS::EC2::Instance DnsServer1 i-014eee8720c4fb542 does not exist
11:03:00 UTC-0500 UPDATE_IN_PROGRESS AWS::CloudFormation::Stack GregCFTest1 User Initiated
What can I do to get CloudFormation to recreate this resource, short of deleting the stack and recreating it?
The easiest way is to give the EC2 instance a different resource name and update the stack. Cloudformation will interpret that as you creating an entirely new instance and create it.
Another alternative is to change a configuration value that forces the creation of a new instance, such as changing the subnet of the instance.
I faced similar issue related to unique ID, this link helped me to resolve the issue.
https://aws.amazon.com/premiumsupport/knowledge-center/failing-stack-updates-deleted/