Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get a volume unmounted when I call CloudFormation delete-stack?

I am using CloudFormation to create my environment. Part of the stack includes creating a volume from a snapshot, associating it with an EC2 instance, and then mounting it.

"Resources" : {
    "EBSData" : {
        "Type" : "AWS::EC2::Volume",
     ...snip...
    },

    "MountPoint" : {
        "Type" : "AWS::EC2::VolumeAttachment",
     ...snip...
    },

    "myTestInstance" : {
        "Type" : "AWS::EC2::Instance",
     ...snip...
    }
 },

When I try to call delete-stack, it fails because the volume is still mounted:

  "StackStatusReason": "The following resource(s) failed to delete: [EBSData, MountPoint].", 
  "CreationTime": "2013-12-03T13:40:58.646Z", 
  "StackName": "myTestStack", 
  "StackStatus": "DELETE_FAILED", 
  "DisableRollback": false

Calling delete-stack a 2nd time succeeds, because the instance has been destroyed already.

Are there any hooks into the running instance that get invoked by cloudformation delete-stack where I could un-mount the volume? Any other way to do this?

like image 642
chris Avatar asked Dec 03 '13 16:12

chris


1 Answers

To get this working, I had to change the way I attached the volume. Instead of using a AWS::EC2::VolumeAttachment, you can specify EBS volumes to attach to an instance within the properties of that instance:

"Resources" : {
    "EBSData" : {
        "Type" : "AWS::EC2::Volume",
     ...snip...
    },

    "myTestInstance" : {
        "Type" : "AWS::EC2::Instance",
        "Properties": {
            "Volumes": [ { "VolumeId": { "Ref": "EBSData" }, "Device": "<device mount point>" }]
        ...snip...
        },
     ...snip...
    }
 },

Attaching the volume this way appears to make CloudFormation delete the instance and volume in the correct order. That is, the instance is shut down before the volume is deleted.

Be sure that EBSData does not use any references to myTestInstance or you'll get a circular dependency.

like image 195
Dermah Avatar answered Oct 18 '22 20:10

Dermah