The page has been translated by Gen AI.

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.

StatusStatus GroupExecutable tasks
CreatingIn Progress-
Create FailedFailedEdit stack, Delete stack
Create CompletedCompletedDrift detection, change set creation, stack update, stack deletion
Rollback In ProgressIn Progress-
Rollback FailedFailedDelete stack
Rollback CompletedFailedDelete stack
DeletingIn Progress-
Delete CompletedDeleted-
Delete FailedFailedDrift detection, stack deletion
UpdatingIn ProgressUndo stack edit
Update Complete Cleanup In ProgressIn Progress-
Update CompletedCompletedDrift detection, create change set, update stack, delete stack
Update FailedFailedEdit stack, Delete stack
Update Rollback In ProgressIn Progress-
Update Rollback FailedFailedDrift detection, stack deletion, update rollback continue
Update Rollback Cleanup In ProgressIn Progress-
Update Rollback CompletedCompletedDrift detection, create change set, update stack, delete stack
ReviewingIn ProgressCreate change set, delete stack
Table: Stack state and possible operations

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.

CategoryExplanation
Rollback all stack resourcesRollback to the state before the stack is executed
  • Delete resources created up to the point of failure and restore to the previous state
Preserve successfully created resourcesPreserve resources that have been successfully provisioned and roll back failed resources to their last state
  • Use when checking the cause of failure and then continuing deployment
Table. Stack failure option

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

CategoryExplanation
Use deletion policyRetain or delete the created resources according to the deletion policy set in the template.
Delete all newly created resourcesDelete 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

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.

CategoryProtection scopeSetting location
Deletion policyDecide whether to delete or retain individual resources when deleting a stack.template
Stack policyRestrict changes to specific resources when updating the stackConsole
Prevent deletionPrevent deletion of the entire stackConsole
Table. Comparison of Resource Protection Methods

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.

CategoryExplanation
EffectSpecify whether to allow (Allow) or deny (Deny) the designated action
ActionUpdate actions to allow or deny
  • Update:Modify: An update that changes the resource without interruption or with partial interruption. The physical ID is retained.
  • Update:Replace: An update that creates a new resource and deletes the existing one. The physical ID may change.
  • Update:Delete: An update that removes the resource from the template.
  • Update:*: All update actions
  • Multiple actions can be specified as an array (e.g., ["Update:Replace", "Update:Delete"])
PrincipalPolicy application target
  • is a required element, and only * representing all users can be specified
ResourceLogical ID of the resource to which the policy will be applied
  • LogicalResourceId/{Logical ID} format (e.g., LogicalResourceId/MyBucket)
  • Wildcards are allowed (e.g., LogicalResourceId/Critical*, all resources are *)
ConditionSpecify the target to which the policy is applied by resource type
  • Specify the resource types as an array in the ResourceType key
  • StringEquals specifies an exact match type, StringLike specifies a type using wildcards
Table. Stack policy components

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.

  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 스택 생성하기.
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.

Color mode
{
  "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" : "*"
    }
  ]
}
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.

Color mode
{
  "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" : "*"
    }
  ]
}
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.

Color mode
{
  "Statement" : [
    {
      "Effect" : "Deny"
      "Action" : "Update:*"
      "Principal" : "*"
      "Resource" : "*"
    }
  ]
}
{
  "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.
template
Change Set