# Stack

LLMS index: [llms.txt](https://docs.e.samsungsdscloud.com/en/userguide/management/llms.txt)

---

# Stack {#stack}

A stack is a logical grouping of resources that are created and managed together based on a template. Resource creation, modification, and deletion are performed on a per-stack basis.

When a stack is deleted, the associated resources are also deleted by default. (Note: Resources with a DeletionPolicy set may persist even after the stack is deleted.) Therefore, it is recommended to group resources with the same lifecycle into a single stack, and separate resources that need to be maintained independently into another stack.

# Stack status {#status}

The stack has the following states depending on the progress of the task. The tasks you can perform vary depending on the status.


| Status | Status Group | Executable tasks |
|---------|--------|--------|
| Creating | In Progress | - |
| Create Failed | Failed | Edit stack, Delete stack |
| Create Completed | Completed | Drift detection, change set creation, stack update, stack deletion |
| Rollback In Progress | In Progress | - |
| Rollback Failed | Failed | Delete stack |
| Rollback Completed | Failed | Delete stack |
| Deleting | In Progress | - |
| Delete Completed | Deleted | - |
| Delete Failed | Failed | Drift detection, stack deletion |
| Updating | In Progress | Undo stack edit |
| Update Complete Cleanup In Progress | In Progress | - |
| Update Completed | Completed | Drift detection, create change set, update stack, delete stack |
| Update Failed | Failed | Edit stack, Delete stack |
| Update Rollback In Progress | In Progress | - |
| Update Rollback Failed | Failed | Drift detection, stack deletion, update rollback continue |
| Update Rollback Cleanup In Progress | In Progress | - |
| Update Rollback Completed | Completed | Drift detection, create change set, update stack, delete stack |
| Reviewing | In Progress | Create change set, delete stack |

_Table: Stack state and possible operations_


# Automatic rollback {#rollback}

If an error occurs while creating or updating a stack, it automatically recovers to the last successful stable state. This prevents a deployment from failing midway and leaving resources partially created.

When creating a stack, you can specify the failure handling method as follows.


| Category | Explanation |
|---------|--------|
| Rollback all stack resources | Rollback to the state before the stack is executed<ul><li>Delete resources created up to the point of failure and restore to the previous state</li></ul> |
| Preserve successfully created resources | Preserve resources that have been successfully provisioned and roll back failed resources to their last state<ul><li> Use when checking the cause of failure and then continuing deployment</li></ul> |

_Table. Stack failure option_


During a rollback, you can specify how newly created resources are handled as follows.


| Category | Explanation |
|---------|--------|
| Use deletion policy | Retain or delete the created resources according to the deletion policy set in the template. |
| Delete all newly created resources | Delete resources created during rollback regardless of the deletion policy set in the template. |

_Table. Resource handling options during rollback_


If you select **Preserve successfully created resources**, successful resources are not deleted, so the resource handling option does not operate during rollback.

A stack with a **Update Failed** status cannot be rolled back to a previous state, so when modifying the stack you can only select **preserve successfully created resources**.

# Resource protection {#protection}

We provide the following three methods to protect resources from unintended changes or deletions. Since the protection scope and setting location differ, use it according to your purpose.


| Category | Protection scope | Setting location |
|---------|--------|--------|
| Deletion policy | Decide whether to delete or retain individual resources when deleting a stack. | template |
| Stack policy | Restrict changes to specific resources when updating the stack | Console |
| Prevent deletion | Prevent deletion of the entire stack | Console |

_Table. Comparison of Resource Protection Methods_


## Stack policy {#stack-policy}

A stack policy is a JSON document that protects specific resources from being modified or deleted while updating a stack. Define the resources to protect and the actions to restrict. Stack policies are applied only during stack updates and do not control user access like IAM permissions. Use it as a safety mechanism to prevent updates caused by mistakes.

The stack policy consists of the following elements.


| Category | Explanation |
|---------|--------|
| Effect | Specify whether to allow (Allow) or deny (Deny) the designated action |
| Action | Update actions to allow or deny<ul><li>**Update:Modify**: An update that changes the resource without interruption or with partial interruption. The physical ID is retained.</li></ul><ul><li>**Update:Replace**: An update that creates a new resource and deletes the existing one. The physical ID may change.</li></ul><ul><li>**Update:Delete**: An update that removes the resource from the template.</li></ul><ul><li>**Update:\***: All update actions</li></ul><ul><li>Multiple actions can be specified as an array (e.g., `["Update:Replace", "Update:Delete"]`)</li></ul> |
| Principal | Policy application target <ul><li> is a required element, and only `*` representing all users can be specified </li></ul> |
| Resource | Logical ID of the resource to which the policy will be applied<ul><li>`LogicalResourceId/{Logical ID}` format (e.g., `LogicalResourceId/MyBucket`)</li></ul><ul><li>Wildcards are allowed (e.g., `LogicalResourceId/Critical*`, all resources are `*`)</li></ul> |
| Condition | Specify the target to which the policy is applied by resource type<ul><li>Specify the resource types as an array in the `ResourceType` key</li></ul><ul><li>`StringEquals` specifies an exact match type, `StringLike` specifies a type using wildcards</li></ul> |

_Table. Stack policy components_


### Stack Policy Evaluation Rules {#stack-policy-rules}

Stack policies are evaluated according to the following rules.

* If you do not set a stack policy, all updates to all resources are allowed.
* If you set a stack policy, all updates that are not explicitly allowed (Allow) are rejected. Even if only some resources are protected, an Allow statement is required to update the remaining resources.
* If both Allow and Deny are applied to a single resource, Deny always takes precedence.
* A stack can have only one stack policy, and the stack policy applies uniformly to all users who update the stack.
* The stack policy set when creating the stack continues to apply to subsequent stack updates.
* When modifying a stack, if you provide a stack policy, only the entered stack policy will be applied to that modification instead of the existing stack policy. The entered stack policy is applied only once to that modification, and if you do not enter a stack policy when making subsequent changes, the original stack policy will be applied again. If you need to modify a resource protected by a stack policy, update the stack by entering a stack policy that allows updates to that resource.
* If you do not set a stack policy when creating the stack, you cannot set a stack policy on the stack later.

### Write a stack policy {#stack-policy-write}

The stack policy is written in the following order.

1. Define the resources to protect and the actions to restrict. Check the logical ID of the resource defined in the template.
2. Write an Allow statement that permits all updates to all resources.
3. Add a Deny statement for the resource to be protected. Since Deny takes precedence over Allow, a Deny statement is applied to the resources to be protected.
4. Enter in JSON format or upload a file. For input methods and size limits, refer to the **Stack Options Configuration** step of [스택 생성하기](/en/userguide/management/infrastructure_builder/how_to_guides/#create).

> **Reference**: It is recommended to specify protected resources with an explicit Deny statement rather than excluding them from the Allow set.

The following is an example.

**Prevent all updates to a specific resource**

Reject all modifications, replacements, and deletions of the `MyDatabase` resource, and allow all other resources.


```json
{
  "Statement" : [
    {
      "Effect" : "Deny"
      "Action" : "Update:*"
      "Principal" : "*"
      "Resource" : "LogicalResourceId/MyDatabase"
    },
    {
      "Effect" : "Allow"
      "Action" : "Update:*"
      "Principal" : "*"
      "Resource" : "*"
    }
  ]
}
```

_Code block. Example of a stack policy that prevents updates to a specific resource_

**Prevent replacement and deletion of specific resources**

Only replacement and deletion of the `MyBucket` resource are denied, while all other updates, including modifications, are allowed.


```json
{
  "Statement" : [
    {
      "Effect" : "Deny"
      "Action" : ["Update:Replace", "Update:Delete"],
      "Principal" : "*"
      "Resource" : "LogicalResourceId/MyBucket"
    },
    {
      "Effect" : "Allow"
      "Action" : "Update:*"
      "Principal" : "*"
      "Resource" : "*"
    }
  ]
}
```

_Code block. Example of a stack policy that prevents replacement and deletion._

**Prevent updates to all resources**

Reject updates to all resources in the stack.


```json
{
  "Statement" : [
    {
      "Effect" : "Deny"
      "Action" : "Update:*"
      "Principal" : "*"
      "Resource" : "*"
    }
  ]
}
```

_Code block. Example of a stack policy that prevents updates to all resources_

> **Caution**: If you write only a Deny statement without an Allow statement in a stack policy, all updates that are not explicitly allowed are rejected, preventing updates to resources other than the ones you intend to protect. To protect only specific resources, include an Allow statement that permits all resources.
