UpdatePolicy Attribute
Use the UpdatePolicy attribute to specify how AWS CloudFormation handles updates to the
AWS::AutoScaling::AutoScalingGroup, AWS::Lambda::Alias, or AWS::ElastiCache::ReplicationGroup resources.
-
For
AWS::AutoScaling::AutoScalingGroupresources, AWS CloudFormation invokes one of three update policies depending on the type of change you make or whether a scheduled action is associated with the Auto Scaling group.-
The
AutoScalingReplacingUpdateandAutoScalingRollingUpdatepolicies apply only when you do one or more of the following:-
Change the Auto Scaling group's
AWS::AutoScaling::LaunchConfiguration. -
Change the Auto Scaling group's
VPCZoneIdentifierproperty -
Change the Auto Scaling group's
LaunchTemplateproperty -
Update an Auto Scaling group that contains instances that don't match the current
LaunchConfiguration.
If both the
AutoScalingReplacingUpdateandAutoScalingRollingUpdatepolicies are specified, setting theWillReplaceproperty totruegivesAutoScalingReplacingUpdateprecedence. -
-
The
AutoScalingScheduledActionpolicy applies when you update a stack that includes an Auto Scaling group with an associated scheduled action.
-
-
For
AWS::Lambda::Aliasresources, AWS CloudFormation performs an AWS CodeDeploy deployment when the version changes on the alias. For more information, see CodeDeployLambdaAliasUpdate Policy. -
For
AWS::ElastiCache::ReplicationGroupresources, AWS CloudFormation can modify a replication group's shards by adding or removing shards, rather than replacing the entire resource. For more information, see UseOnlineResharding Policy.
AutoScalingReplacingUpdate Policy
To specify how AWS CloudFormation handles replacement updates for an Auto Scaling
group, use the AutoScalingReplacingUpdate policy. This policy enables you to specify whether AWS CloudFormation replaces an
Auto Scaling group with a new one or replaces only the instances in the Auto Scaling
group.
Important
Before attempting an update, ensure that you have sufficient Amazon EC2 capacity for both your old and new Auto Scaling groups.
Syntax
JSON
"UpdatePolicy" : { "AutoScalingReplacingUpdate" : { "WillReplace" :Boolean} }
YAML
UpdatePolicy: AutoScalingReplacingUpdate: WillReplace:Boolean
Properties
WillReplace-
Specifies whether an Auto Scaling group and the instances it contains are replaced during an update. During replacement, AWS CloudFormation retains the old group until it finishes creating the new one. If the update fails, AWS CloudFormation can roll back to the old Auto Scaling group and delete the new Auto Scaling group.
While AWS CloudFormation creates the new group, it doesn't detach or attach any instances. After successfully creating the new Auto Scaling group, AWS CloudFormation deletes the old Auto Scaling group during the cleanup process.
When you set the
WillReplaceparameter, remember to specify a matchingCreationPolicy. If the minimum number of instances (specified by theMinSuccessfulInstancesPercentproperty) don't signal success within theTimeoutperiod (specified in theCreationPolicypolicy), the replacement update fails and AWS CloudFormation rolls back to the old Auto Scaling group.Type: Boolean
Required: No
AutoScalingRollingUpdate Policy
To specify how AWS CloudFormation handles rolling updates for an Auto Scaling group,
use the AutoScalingRollingUpdate policy. Rolling updates enable you to specify whether AWS CloudFormation updates
instances that are in an Auto Scaling group in batches or all at once.
Important
During a rolling update, some Auto Scaling processes might make changes to the Auto
Scaling group before
AWS CloudFormation completes the rolling update. These changes might cause the rolling
update to fail. To
prevent Auto Scaling from running processes during a rolling update, use the
SuspendProcesses property. For more information, see What are some recommended best practices for performing Auto Scaling group rolling
updates?
Be aware that, during stack update rollback operations, CloudFormation uses the
UpdatePolicy configuration specified in the template before the current stack
update operation. For example, suppose you have updated the MaxBatchSize in your
stack template's UpdatePolicy from 1 to 10. You then perform a stack update, and
that update fails and CloudFormation initiates an update rollback operation. In such
a case,
CloudFormation will use 1 as the maximum batch size, rather than 10. For this reason,
we
recommend you make changes to the UpdatePolicy configuration in a stack update
separate from, and prior to, any updates to the
AWS::AutoScaling::AutoScalingGroup resource that are likely to trigger rolling
updates.
Syntax
JSON
"UpdatePolicy" : { "AutoScalingRollingUpdate" : { "MaxBatchSize" :Integer, "MinInstancesInService" :Integer, "MinSuccessfulInstancesPercent" :Integer"PauseTime" :String, "SuspendProcesses" : [List of processes], "WaitOnResourceSignals" :Boolean} }
YAML
UpdatePolicy: AutoScalingRollingUpdate: MaxBatchSize:IntegerMinInstancesInService:IntegerMinSuccessfulInstancesPercent:IntegerPauseTime:StringSuspendProcesses: -List of processesWaitOnResourceSignals:Boolean
Properties
MaxBatchSize-
Specifies the maximum number of instances that AWS CloudFormation updates.
Default:
1Type: Integer
Required: No
MinInstancesInService-
Specifies the minimum number of instances that must be in service within the Auto Scaling group while AWS CloudFormation updates old instances. This value must be less than the MaxSize of the Auto Scaling group.
Default:
0Type: Integer
Required: No
MinSuccessfulInstancesPercent-
Specifies the percentage of instances in an Auto Scaling rolling update that must signal success for an update to succeed. You can specify a value from
0to100. AWS CloudFormation rounds to the nearest tenth of a percent. For example, if you update five instances with a minimum successful percentage of50, three instances must signal success.If an instance doesn't send a signal within the time specified in the
PauseTimeproperty, AWS CloudFormation assumes that the instance wasn't updated.If you specify this property, you must also enable the
WaitOnResourceSignalsandPauseTimeproperties.The
MinSuccessfulInstancesPercentparameter applies only to instances only for signaling purpose. To specify the number of instances in your autoscaling group, see theMinSize,MaxSize, andDesiredCapacityproperties fo the AWS::AutoScaling::AutoScalingGroup resource.Default:
100Type: Integer
Required: No
PauseTime-
The amount of time that AWS CloudFormation pauses after making a change to a batch of instances to give those instances time to start software applications. For example, you might need to specify
PauseTimewhen scaling up the number of instances in an Auto Scaling group.If you enable the
WaitOnResourceSignalsproperty,PauseTimeis the amount of time that AWS CloudFormation should wait for the Auto Scaling group to receive the required number of valid signals from added or replaced instances. If thePauseTimeis exceeded before the Auto Scaling group receives the required number of signals, the update fails. For best results, specify a time period that gives your applications sufficient time to get started. If the update needs to be rolled back, a shortPauseTimecan cause the rollback to fail.Specify
PauseTimein the ISO8601 duration format (in the formatPT, where each#H#M#S#is the number of hours, minutes, and seconds, respectively). The maximumPauseTimeis one hour (PT1H).Default:
PT0S(zero seconds). If theWaitOnResourceSignalsproperty is set totrue, the default isPT5M.Type: String
Required: No
SuspendProcesses-
Specifies the Auto Scaling processes to suspend during a stack update. Suspending processes prevents Auto Scaling from interfering with a stack update. For example, you can suspend alarming so that Amazon EC2 Auto Scaling doesn't execute scaling policies associated with an alarm. For valid values, see the
ScalingProcesses.member.Nparameter for the SuspendProcesses action in the Amazon EC2 Auto Scaling API Reference.Default: Not specified
Type: List of Auto Scaling processes
Required: No
WaitOnResourceSignals-
Specifies whether the Auto Scaling group waits on signals from new instances during an update. Use this property to ensure that instances have completed installing and configuring applications before the Auto Scaling group update proceeds. AWS CloudFormation suspends the update of an Auto Scaling group after new EC2 instances are launched into the group. AWS CloudFormation must receive a signal from each new instance within the specified
PauseTimebefore continuing the update. To signal the Auto Scaling group, use the cfn-signal helper script orSignalResourceAPI.To have instances wait for an Elastic Load Balancing health check before they signal success, add a health-check verification by using the cfn-init helper script. For an example, see the
verify_instance_healthcommand in the Auto Scaling rolling updates sample template.Default:
falseType: Boolean
Required: Conditional. If you specify the
MinSuccessfulInstancesPercentproperty, you must also enable theWaitOnResourceSignalsandPauseTimeproperties.
AutoScalingScheduledAction Policy
To specify how AWS CloudFormation handles updates for the MinSize, MaxSize,
and DesiredCapacity properties when the
AWS::AutoScaling::AutoScalingGroup resource has an associated scheduled action,
use the AutoScalingScheduledAction policy.
With scheduled actions, the group size properties of an Auto Scaling group can change
at any time.
When you update a stack with an Auto Scaling group and scheduled action, AWS CloudFormation
always sets the group
size property values of your Auto Scaling group to the values that are defined in
the
AWS::AutoScaling::AutoScalingGroup resource of your template, even if a
scheduled action is in effect.
If you do not want AWS CloudFormation to change any of the group size property values
when you have a
scheduled action in effect, use the AutoScalingScheduledAction update policy to
prevent AWS CloudFormation from changing the MinSize, MaxSize, or
DesiredCapacity properties unless you have modified these values in your
template.
Syntax
JSON
"UpdatePolicy" : { "AutoScalingScheduledAction" : { "IgnoreUnmodifiedGroupSizeProperties" :Boolean} }
YAML
UpdatePolicy: AutoScalingScheduledAction: IgnoreUnmodifiedGroupSizeProperties:Boolean
Properties
IgnoreUnmodifiedGroupSizeProperties-
Specifies whether AWS CloudFormation ignores differences in group size properties between your current Auto Scaling group and the Auto Scaling group described in the
AWS::AutoScaling::AutoScalingGroupresource of your template during a stack update. If you modify any of the group size property values in your template, AWS CloudFormation uses the modified values and updates your Auto Scaling group.Default:
falseType: Boolean
Required: No
CodeDeployLambdaAliasUpdate Policy
To perform an AWS CodeDeploy deployment when the version changes on an AWS::Lambda::Alias resource, use the CodeDeployLambdaAliasUpdate update policy.
Syntax
JSON
"UpdatePolicy" : { "CodeDeployLambdaAliasUpdate" : { "AfterAllowTrafficHook" :String, "ApplicationName" :String, "BeforeAllowTrafficHook" :String, "DeploymentGroupName" :String} }
YAML
UpdatePolicy: CodeDeployLambdaAliasUpdate: AfterAllowTrafficHook:StringApplicationName:StringBeforeAllowTrafficHook:StringDeploymentGroupName:String
Properties
AfterAllowTrafficHook-
The name of the Lambda function to run after traffic routing completes.
Required: No
Type: String
ApplicationName-
The name of the AWS CodeDeploy application.
Required: Yes
Type: String
BeforeAllowTrafficHook-
The name of the Lambda function to run before traffic routing starts.
Required: No
Type: String
DeploymentGroupName-
The name of the AWS CodeDeploy deployment group. This is where the traffic-shifting policy is set.
Required: Yes
Type: String
For an example that specifies the UpdatePolicy attribute for an AWS::Lambda::Alias resource,
see Lambda Alias Update Policy.
UseOnlineResharding Policy
To modify a replication group's shards by adding or removing shards, rather than replacing
the entire AWS::ElastiCache::ReplicationGroup resource, use the
UseOnlineResharding update policy.
If UseOnlineResharding is set to true, you can update the
NumNodeGroups and NodeGroupConfiguration properties of the
AWS::ElastiCache::ReplicationGroup resource, and CloudFormation will update those
properties without interruption. When UseOnlineResharding is set to
false, or not specified, updating the NumNodeGroups and
NodeGroupConfiguration properties results in CloudFormation replacing the entire
AWS::ElastiCache::ReplicationGroup resource.
The UseOnlineResharding update policy has no properties.
Things to consider when setting the UseOnlineResharding update policy to
true:
-
We strongly recommend you perform updates to the
NumNodeGroupsandNodeGroupConfigurationproperties as the only updates in a given stack update operation.Updating the node group configuration of a replication group is a resource-intensive operation. If a stack update fails, CloudFormation does not roll back changes to the node group configuration of a replication group. However, CloudFormation will roll back any other properties that were changed as part of the failed update operation.
-
Any node group updates require identifying all node groups
If you specify the
NodeGroupConfigurationproperty, you must also specify the NodeGroupId for each node group configuration in order for CloudFormation to update the number of nodes without interruption.When creating a replication group, if you do not specify an ID for each node group, ElastiCache automatically generates an ID for each node group. To update the replication group without interruption, use the ElastiCache console (https://console.aws.amazon.com/elasticache/) or DescribeReplicationGroups to retrieve the IDs for all node groups in the replication group. Then specify the ID for each node group in your stack template before attempting to add or remove shards.
Note
As a best practice, when you create a replication group in a stack template, include an ID for each node group you specify.
In addition, updating the number of nodes without interruption requires that you have accurately specified the
PrimaryAvailabilityZone,ReplicaAvailabilityZones, andReplicaCountproperties for eachNodeGroupConfigurationas well. Again, you can use the ElastiCache console (https://console.aws.amazon.com/elasticache/) or DescribeReplicationGroups to retrieve the actual values for each node group and compare them to the values in your stack template. You can update the property values of the node groups as a separate stack update, or as part of the same stack update that changes the number of node groups.When you use an
UseOnlineReshardingupdate policy to update the number of node groups without interruption, ElastiCache evenly distributes the keyspaces between the specified number of slots. This cannot be updated later. Therefore, after updating the number of node groups in this way, you should remove the value specified for theSlotsproperty of eachNodeGroupConfigurationfrom the stack template, as it no longer reflects the actual values in each node group. -
Actual node group removal results may vary
When you specify a
NumNodeGroupsvalue that is less than the current number of node groups, CloudFormation instructs ElastiCache to remove as many node groups as necessary to reach the specified number of nodes. However, ElastiCache may not always be able to remove the desired number of node groups. In the event ElastiCache cannot remove the desired number of node groups, CloudFormation generates a stack event alerting you to this. In cases where ElastiCache cannot remove any node groups, the CloudFormation resource update fails.
For more information on modifying replication groups, see ModifyReplicationGroupShardConfiguration.html in the Amazon ElastiCache API Reference.
Syntax
JSON
"UpdatePolicy" : { "UseOnlineResharding" :Boolean}
YAML
UpdatePolicy: UseOnlineResharding:Boolean
Examples
The following examples show how to add an update policy to an Auto Scaling group and how to maintain availability when updating metadata.
Add an UpdatePolicy to an Auto Scaling Group
The following example shows how to add an update policy. During an update, the Auto
Scaling
group updates instances in batches of two and keeps a minimum of one instance in service.
Because the WaitOnResourceSignals flag is set, the Auto Scaling group waits for new
instances that are added to the group. The new instances must signal the Auto Scaling
group before
it updates the next batch of instances.
JSON
"ASG" : { "Type" : "AWS::AutoScaling::AutoScalingGroup", "Properties" : { "AvailabilityZones" : [ "us-east-1a", "us-east-1b" ], "DesiredCapacity" : "1", "LaunchConfigurationName" : { "Ref" : "LaunchConfig" }, "MaxSize" : "4", "MinSize" : "1" }, "UpdatePolicy" : { "AutoScalingScheduledAction" : { "IgnoreUnmodifiedGroupSizeProperties" : "true" }, "AutoScalingRollingUpdate" : { "MinInstancesInService" : "1", "MaxBatchSize" : "2", "WaitOnResourceSignals" : "true", "PauseTime" : "PT10M" } } }, "ScheduledAction" : { "Type" : "AWS::AutoScaling::ScheduledAction", "Properties" : { "AutoScalingGroupName" : { "Ref" : "ASG" }, "DesiredCapacity" : "2", "StartTime" : "2017-06-02T20 : 00 : 00Z" } }
YAML
ASG: Type: 'AWS::AutoScaling::AutoScalingGroup' Properties: AvailabilityZones: - us-east-1a - us-east-1b DesiredCapacity: '1' LaunchConfigurationName: Ref: LaunchConfig MaxSize: '4' MinSize: '1' UpdatePolicy: AutoScalingScheduledAction: IgnoreUnmodifiedGroupSizeProperties: 'true' AutoScalingRollingUpdate: MinInstancesInService: '1' MaxBatchSize: '2' WaitOnResourceSignals: 'true' PauseTime: PT10M ScheduledAction: Type: 'AWS::AutoScaling::ScheduledAction' Properties: AutoScalingGroupName: Ref: ASG DesiredCapacity: '2' StartTime: '2017-06-02T20 : 00 : 00Z'
AutoScalingReplacingUpdate Policy
The following example declares a policy that forces an associated Auto Scaling group
to be replaced during an update. For the update to succeed, a percentage of instances
(specified by the MinSuccessfulPercentParameter parameter) must signal success within the Timeout period.
JSON
"UpdatePolicy" : { "AutoScalingReplacingUpdate" : { "WillReplace" : "true" } }, "CreationPolicy" : { "ResourceSignal" : { "Count" : { "Ref" : "ResourceSignalsOnCreate"}, "Timeout" : "PT10M" }, "AutoScalingCreationPolicy" : { "MinSuccessfulInstancesPercent" : { "Ref" : "MinSuccessfulPercentParameter" } } }
YAML
UpdatePolicy: AutoScalingReplacingUpdate: WillReplace: 'true' CreationPolicy: ResourceSignal: Count: !Ref 'ResourceSignalsOnCreate' Timeout: PT10M AutoScalingCreationPolicy: MinSuccessfulInstancesPercent: !Ref 'MinSuccessfulPercentParameter'
Maintain Availability When Updating the Metadata for the cfn-init Helper Script
When you install software applications on your instances, you might use the AWS::CloudFormation::Init
metadata key and the cfn-init helper script to bootstrap the instances in your
Auto Scaling group. AWS CloudFormation installs the packages, runs the commands, and
performs other bootstrapping
actions described in the metadata.
When you update only the metadata (for example, when updating a package to another
version), you can use the cfn-hup helper daemon to detect and apply the updates. However, the
cfn-hup daemon runs independently on each instance. If the daemon
happens to runs at the same time on all instances, your application or service might
be
unavailable during the update. To guarantee availability, you can force a rolling
update so
that AWS CloudFormation updates your instances one batch at a time.
Important
Forcing a rolling update requires AWS CloudFormation to create a new instance and then delete the old one. Any information stored on the old instance is lost.
To force a rolling update, change the logical ID of the launch configuration resource, and then update the stack and any references pointing to the original logic ID (such as the associated Auto Scaling group). AWS CloudFormation triggers a rolling update on the Auto Scaling group, replacing all instances.
Original Template
"LaunchConfig": { "Type" : "AWS::AutoScaling::LaunchConfiguration", "Metadata" : { "Comment" : "Install a simple PHP application", "AWS::CloudFormation::Init" : { ... } } }
Updated Logical ID
"LaunchConfigUpdateRubygemsPkg": { "Type" : "AWS::AutoScaling::LaunchConfiguration", "Metadata" : { "Comment" : "Install a simple PHP application", "AWS::CloudFormation::Init" : { ... } } }
Lambda Alias Update Policy
The following example specifies the UpdatePolicy attribute for an AWS::Lambda::Alias resource.
All the details for the deployment are defined by the application and deployment
group that are passed into the policy.
JSON
"Alias": { "Type": "AWS::Lambda::Alias", "Properties": { "FunctionName": { "Ref": "LambdaFunction" }, "FunctionVersion": { "Fn::GetAtt": [ "FunctionVersionTwo", "Version" ] }, "Name": "MyAlias" }, "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "CodeDeployApplication" }, "DeploymentGroupName": { "Ref": "CodeDeployDeploymentGroup" }, "BeforeAllowTrafficHook": { "Ref": "PreHookLambdaFunction" }, "AfterAllowTrafficHook": { "Ref": "PreHookLambdaFunction" } } } }
YAML
Alias: Type: 'AWS::Lambda::Alias' Properties: FunctionName: !Ref LambdaFunction FunctionVersion: !GetAtt FunctionVersionTwo.Version Name: MyAlias UpdatePolicy: CodeDeployLambdaAliasUpdate: ApplicationName: !Ref CodeDeployApplication DeploymentGroupName: !Ref CodeDeployDeploymentGroup BeforeAllowTrafficHook: !Ref PreHookLambdaFunction AfterAllowTrafficHook: !Ref PreHookLambdaFunction
