Is it possible to force the "Replace" update behavior on changes to the Metadata property for AWS::EC2::Instance resources?
I have an EC2 instance that does not have any important state (so I don’t care about its disks and other state getting deleted). The instance has a number of AWS::CloudFormation::Init scripts and files which are used to set up the instance upon creation. However, these scripts do not run again on update and nor would it make sense for them to run again on update.
The problem I am having is that sometimes I want to change something about the instance, which usually consists of modifying the AWS::CloudFormation::Init scripts and/or files. What I’d like to have happen is when I update the CloudFormation stack with a change to these Metadata properties, then I’d like the instance to be deleted and recreated (i.e. the "Replace" update behavior with UpdateReplacePolicy: Delete
).
Here is what my resource looks like:
GatewayInstance:
Type: 'AWS::EC2::Instance'
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
# ...
Metadata:
AWS::CloudFormation::Init:
# When this changes I want GatewayInstance to be deleted and recreated
Is this possible?
2
Answers
It is a bit of a hack, but I did find a solution.
Changing any of the following properties force replacement of the instance:
So, I simply created two identical key pairs and when I toggle
KeyName
between them and update the stack this forces the recreation of the physical resources. Et viola.Sadly its not possible as explained in the docs:
You maybe could create your own custom resource to apply the changes that you make in
Metadata
.The alternative is not use use
Metadata
at all.