> ## Documentation Index
> Fetch the complete documentation index at: https://cloudanix.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# CloudFormation Stacks Should Not Have A Failed Status

### More Info:

None of your Amazon CloudFormation stacks should be in Failed mode for more than 6 hours. Any failed CloudFormation stacks that are not fixed on time can lead to application downtime, security issues or unexpected costs on your AWS bill.

### Risk Level

Informational

### Address

Operational Maturity

### Compliance Standards

CBP

### Triage and Remediation

<Tabs>
  <Tab title="Remediation">
    ### Remediation

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate a CloudFormation stack with a failed status in AWS using the AWS console, follow these steps:

        1. Open the AWS Management Console and navigate to the CloudFormation service.
        2. Select the CloudFormation stack with a failed status that you want to remediate.
        3. Click on the "Events" tab to view the events associated with the stack.
        4. Review the events to identify the root cause of the failure. The event details will provide information on the resource that failed and the reason for the failure.
        5. Once you have identified the root cause of the failure, take the appropriate action to remediate the issue. This may involve updating the CloudFormation template, modifying the resource configuration, or resolving any dependencies or permissions issues.
        6. After making the necessary changes, update the CloudFormation stack by clicking on the "Update Stack" button.
        7. Follow the prompts to upload the updated template and apply the changes to the stack.
        8. Monitor the stack events to ensure that the update is successful and the stack status changes to "CREATE\_COMPLETE" or "UPDATE\_COMPLETE".

        By following these steps, you can remediate a CloudFormation stack with a failed status in AWS using the AWS console.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the CloudFormation Stack failed status in AWS using AWS CLI, follow these steps:

        1. Identify the CloudFormation Stack that has a failed status by running the following command in the AWS CLI:

        ```
        aws cloudformation describe-stacks --stack-name <stack-name>
        ```

        Replace `<stack-name>` with the name of the CloudFormation Stack that has a failed status.

        2. Check the events associated with the failed stack by running the following command:

        ```
        aws cloudformation describe-stack-events --stack-name <stack-name>
        ```

        This will provide information on the events that led to the failed status of the stack.

        3. Fix any issues that caused the stack to fail. This may involve updating the CloudFormation template or fixing any resource dependencies.

        4. Once the issues are fixed, update the stack by running the following command:

        ```
        aws cloudformation update-stack --stack-name <stack-name> --template-body file://<path-to-template-file> --parameters file://<path-to-parameters-file>
        ```

        Replace `<stack-name>` with the name of the CloudFormation Stack that has a failed status, `<path-to-template-file>` with the path to the updated CloudFormation template file, and `<path-to-parameters-file>` with the path to the updated parameters file.

        5. Wait for the stack to update and check its status by running the following command:

        ```
        aws cloudformation describe-stacks --stack-name <stack-name>
        ```

        If the stack status is CREATE\_COMPLETE or UPDATE\_COMPLETE, then the remediation is successful. If not, repeat steps 2 to 4 until the stack status is successful.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the "CloudFormation Stacks Should Not Have A Failed Status" misconfiguration in AWS using Python, you can follow the below steps:

        1. First, you need to identify the CloudFormation stack(s) that have a failed status. You can use the `boto3` library in Python to get the list of CloudFormation stacks and their status.

        ```python theme={null}
        import boto3

        # Create a CloudFormation client
        cf_client = boto3.client('cloudformation')

        # Get the list of stacks
        stacks = cf_client.list_stacks(StackStatusFilter=['CREATE_FAILED', 'ROLLBACK_COMPLETE'])

        # Print the stack names and their status
        for stack in stacks['StackSummaries']:
            print(stack['StackName'], stack['StackStatus'])
        ```

        2. Once you have identified the failed stacks, you can delete them using the `delete_stack` method of the CloudFormation client.

        ```python theme={null}
        # Delete the failed stacks
        for stack in stacks['StackSummaries']:
            cf_client.delete_stack(StackName=stack['StackName'])
        ```

        3. You can also update the CloudFormation template and re-create the stack(s) to remediate the misconfiguration.

        ```python theme={null}
        # Update the CloudFormation template
        with open('template.yaml', 'r') as file:
            template_body = file.read()

        # Update the stack(s)
        for stack in stacks['StackSummaries']:
            cf_client.update_stack(StackName=stack['StackName'], TemplateBody=template_body)
        ```

        Note: Make sure to test the CloudFormation template before updating the stack(s) to avoid any further misconfigurations.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>

### Additional Reading:

* [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/troubleshooting.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/troubleshooting.html)
