2
votes

Just updated some of our CF-templates and was going to update the stack it refers to. I've added some default values and added a CloudWatch alarm and I am also going to downgrade the instance from m4.xlarge to m4.large.

I've already downgraded the instance in the EC2-GUI and it went fine. I then reverted it to its default state as per the original template i.e. m4.xlarge. However when I modify the default value in the template for the instancetype it does not reflect when I upload the modified template to CloudFormation. Meaning the default value is still m4.xlarge and I have to use the drop-down menu to select m4.large as specified in my template.

If I don't change the instancetype I get: "Replacement: False" but if I update the instancetype I get "Replacement: Conditional".

If I read more under "Changeset Details" and then "Details" I see:

[
  {
    "resourceChange": {
      "logicalResourceId": "CPUAlarm",
      "action": "Add",
      "physicalResourceId": null,
      "resourceType": "AWS::CloudWatch::Alarm",
      "replacement": null,
      "details": [],
      "scope": []
    },
    "type": "Resource"
  },
  {
    "resourceChange": {
      "logicalResourceId": "myInstanceName",
      "action": "Modify",
      "physicalResourceId": "<masked>",
      "resourceType": "AWS::EC2::Instance",
      "replacement": "Conditional",
      "details": [
        {
          "target": {
            "name": null,
            "requiresRecreation": "Never",
            "attribute": "Tags"
          },
          "causingEntity": null,
          "evaluation": "Dynamic",
          "changeSource": "DirectModification"
        },
        {
          "target": {
            "name": null,
            "requiresRecreation": "Never",
            "attribute": "Tags"
          },
          "causingEntity": "Project",
          "evaluation": "Static",
          "changeSource": "ParameterReference"
        },
        {
          "target": {
            "name": null,
            "requiresRecreation": "Never",
            "attribute": "Tags"
          },
          "causingEntity": null,
          "evaluation": "Static",
          "changeSource": null
        },
        {
          "target": {
            "name": "InstanceType",
            "requiresRecreation": "Conditionally",
            "attribute": "Properties"
          },
          "causingEntity": "InstanceType",
          "evaluation": "Static",
          "changeSource": "ParameterReference"
        },
        {
          "target": {
            "name": "InstanceType",
            "requiresRecreation": "Conditionally",
            "attribute": "Properties"
          },
          "causingEntity": null,
          "evaluation": "Dynamic",
          "changeSource": "DirectModification"
        }
      ],
      "scope": [
        "Properties",
        "Tags"
      ]
    },
    "type": "Resource"
  },
  {

So what I can see is that: "name": "InstanceType","requiresRecreation": "Conditionally", is the only value that has a more restrictive value and therefore the entire stack gets "Replacement: Conditional".

As per AWS: "In some cases, AWS CloudFormation can determine a value only after you execute a change set. AWS CloudFormation labels those changes as Dynamic evaluations. For example, if you reference an updated resource that is conditionally replaced, AWS CloudFormation can't determine whether the reference to the updated resource will change."

Source: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-changesets-samples.html#using-cfn-updating-stacks-changesets-samples-directly-editing-a-template

AFAIK "Replacement: Conditional" 'might' replace the resource i.e. creating a new physicalResourceID which in turn forces me to change associated SGs etc. but it might also not do it, correct?

Grateful for any assistance!

1

1 Answers

2
votes

It's generally not recommended to modify resources created via CloudFormation, outside of CloudFormation. For your last question, Replacement: Conditional may or may not necessitate replacement of a resource based on what exactly you're trying to do. It's always helpful to look at the AWS CloudFormation docs whenever you have doubts though e.g. in your specific scenario of editing instance type of an EC2 instance, here's the what docs state:

  • Update requires: Some interruptions for Amazon EBS-backed instances
  • Update requires: Replacement for instance store-backed instances

Ref: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-instancetype