Is in rollback_complete state and can not be updated
I am a DevOps engineer and I am responsible for maintaining and deploying updated to a cloud-based application. During a routine workflow related to the update of a cloud formation stack, I encountered an error message stating “This stack is in a ROLLBACK COMPLETE state and cannot be updated”. How can I troubleshoot and resolve this particular issue?
In the context of AWS, here are the steps given for how you can troubleshoot and resolve this particular issue:-
Investigate the failure
You should review the events in the Cloudformation stack event log to identify the cause of the initial failure. You should look for the specific error message and also the failed resources statuses.
Resolve the Configuration issue
You can fix any Configuration error or even missing dependencies that can cause the initial update to fail. This can involve correcting parameter values, adjusting resource properties or even necessary resources are properly defined.
Recreation of the stack
Once you have deleted the stack (If needed) then try to recreate it with the correct Configuration. You should try to ensure that all the resources are correctly defined and dependencies are met.
Update the stack
If the process if deletion is not an option then you can try to make fix it manually. You can alternatively use the “ContinueUpdateRollback” operations to recover the stack.
Import boto3
From botocore.exceptions import ClientError
# Initialize the CloudFormation client
Cf_client = boto3.client(‘cloudformation’, region_name=’us-west-2’)
Def describe_stack_events(stack_name):
Try:
Response = cf_client.describe_stack_events(StackName=stack_name)
Return response[‘StackEvents’]
Except ClientError as e:
Print(f”Error describing stack events: {e}”)
Return None
Def delete_stack(stack_name):
Try:
Cf_client.delete_stack(StackName=stack_name)
Print(f”Deletion initiated for stack: {stack_name}”)
Waiter = cf_client.get_waiter(‘stack_delete_complete’)
Waiter.wait(StackName=stack_name)
Print(f”Stack {stack_name} deleted successfully.”)
Except ClientError as e:
Print(f”Error deleting stack: {e}”)
Def create_stack(stack_name, template_body, parameters):
Try:
Response = cf_client.create_stack(
StackName=stack_name,
TemplateBody=template_body,
Parameters=parameters,
Capabilities=[‘CAPABILITY_NAMED_IAM’]
)
Print(f”Creation initiated for stack: {stack_name}”)
Waiter = cf_client.get_waiter(‘stack_create_complete’)
Waiter.wait(StackName=stack_name)
Print(f”Stack {stack_name} created successfully.”)
Except ClientError as e:
Print(f”Error creating stack: {e}”)
Def continue_update_rollback(stack_name):
Try:
Cf_client.continue_update_rollback(StackName=stack_name)
Print(f”Continue update rollback initiated for stack: {stack_name}”)
Waiter = cf_client.get_waiter(‘stack_rollback_complete’)
Waiter.wait(StackName=stack_name)
Print(f”Rollback complete for stack: {stack_name}.”)
Except ClientError as e:
Print(f”Error continuing update rollback: {e}”)
Def main():
Stack_name = ‘my-cloudformation-stack’
Template_body = ‘…’ # Replace with your CloudFormation template JSON/YAML as string
Parameters = [
{
‘ParameterKey’: ‘KeyName’,
‘ParameterValue’: ‘my-key-pair’
},
# Add other parameters as needed
]
# Check current stack status
Try:
Stack = cf_client.describe_stacks(StackName=stack_name)[‘Stacks’][0]
Stack_status = stack[‘StackStatus’]
Print(f”Current stack status: {stack_status}”)
If stack_status == ‘ROLLBACK_COMPLETE’:
Print(“Stack is in ROLLBACK_COMPLETE state.”)
Describe_stack_events(stack_name)
# After resolving the issue manually, you can decide to either delete or continue rollback
# Option 1: Delete and recreate the stack
Delete_stack(stack_name)
Create_stack(stack_name, template_body, parameters)
# Option 2: Continue the rollback process
# continue_update_rollback(stack_name)
Else:
Print(“Stack is not in ROLLBACK_COMPLETE state. No action needed.”)
Except ClientError as e:
Print(f”Error checking stack status: {e}”)
If __name__ == “__main__”:
Main()
This script would help in automating the handling of cloud formation stack in a ROLLBACK COMPLETE state. You can adjust the template and parameters as needed for your use case needs.