skip to Main Content

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


  1. Chosen as BEST ANSWER

    It is a bit of a hack, but I did find a solution.

    Changing any of the following properties force replacement of the instance:

    AvailabilityZone
    CpuOptions
    ElasticGpuSpecifications
    ElasticInferenceAccelerators
    EnclaveOptions
    HibernationOptions
    HostResourceGroupArn
    ImageId
    Ipv6AddressCount
    Ipv6Addresses
    KeyName
    LaunchTemplate
    LicenseSpecifications
    NetworkInterfaces
    PlacementGroupName
    PrivateIpAddress
    SecurityGroups
    SubnetId
    

    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.


  2. Sadly its not possible as explained in the docs:

    During a stack update, you cannot update the Metadata section by itself. You can update it only when you include changes that add, modify, or delete resources.

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search