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
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 |
Automatic 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
|
| Preserve successfully created resources | Preserve resources that have been successfully provisioned and roll back failed resources to their last state
|
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. |
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
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 |
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
|
| Principal | Policy application target
|
| Resource | Logical ID of the resource to which the policy will be applied
|
| Condition | Specify the target to which the policy is applied by resource type
|
Stack Policy Evaluation 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
The stack policy is written in the following order.
- Define the resources to protect and the actions to restrict. Check the logical ID of the resource defined in the template.
- Write an Allow statement that permits all updates to all resources.
- 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.
- Enter in JSON format or upload a file. For input methods and size limits, refer to the Stack Options Configuration step of 스택 생성하기.
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.
{
"Statement" : [
{
"Effect" : "Deny"
"Action" : "Update:*"
"Principal" : "*"
"Resource" : "LogicalResourceId/MyDatabase"
},
{
"Effect" : "Allow"
"Action" : "Update:*"
"Principal" : "*"
"Resource" : "*"
}
]
}{
"Statement" : [
{
"Effect" : "Deny"
"Action" : "Update:*"
"Principal" : "*"
"Resource" : "LogicalResourceId/MyDatabase"
},
{
"Effect" : "Allow"
"Action" : "Update:*"
"Principal" : "*"
"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.
{
"Statement" : [
{
"Effect" : "Deny"
"Action" : ["Update:Replace", "Update:Delete"],
"Principal" : "*"
"Resource" : "LogicalResourceId/MyBucket"
},
{
"Effect" : "Allow"
"Action" : "Update:*"
"Principal" : "*"
"Resource" : "*"
}
]
}{
"Statement" : [
{
"Effect" : "Deny"
"Action" : ["Update:Replace", "Update:Delete"],
"Principal" : "*"
"Resource" : "LogicalResourceId/MyBucket"
},
{
"Effect" : "Allow"
"Action" : "Update:*"
"Principal" : "*"
"Resource" : "*"
}
]
}Prevent updates to all resources
Reject updates to all resources in the stack.
{
"Statement" : [
{
"Effect" : "Deny"
"Action" : "Update:*"
"Principal" : "*"
"Resource" : "*"
}
]
}{
"Statement" : [
{
"Effect" : "Deny"
"Action" : "Update:*"
"Principal" : "*"
"Resource" : "*"
}
]
}