Attaching an EBS to an EC2 instance from a Spot Fleet

I am looking to create a Spot Fleet in Cloudformation which runs a single game server at a time; if prices spike and the server needs to be terminated it will use the 2 minute heads-up to gracefully shutdown and store anything to be persisted on an EBS Volume. The next instance started by the fleet will then mount the volume and restart the game server from where the previous one left off.

  SpotFleet:
    Type: "AWS::EC2::SpotFleet"
    Properties:
      SpotFleetRequestConfigData:
        IamFleetRole: !Sub arn:aws:iam::${AWS::AccountId}:role/aws-ec2-spot-fleet-tagging-role
        TargetCapacity: 1
        LaunchSpecifications:
          - InstanceType: "m5.large"
            ImageId: "ami-abcd1234"
            IamInstanceProfile: !GetAtt InstanceProfile.Arn
            WeightedCapacity: 1

Now I'm stuck on defining the persisted volume in the cf template. Initially I would just add it as a resource:

  Volume:
    Type: "AWS::EC2::Volume"
    Properties:
      Size: 10
      AvailabilityZone: !Ref AWS::Region

But then how do I reference it in the fleet? You can define BlockDeviceMappings on LaunchSpeficiations within a fleet as per http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-spotfleet-spotfleetrequestconfigdata-launchspecifications-blockdevicemappings.html but from the attributes available I can't seem to reference existing volumes and as such am getting the idea that these volumes are not persisted.

Alternatively I thought of attaching the volume to the spot instance via a VolumeAttachment:

  VolumeAttachment:
    Type: "AWS::EC2::VolumeAttachment"
    Properties:
      Device: "dev/server"
      InstanceId: !Ref SpotFleet
      VolumeId: !Ref Volume

but obviously the SpotFleet reference here returns the fleet name, not the id of any created instances. And neither !Ref nor !GetAtt seem to be able to extract those ids from a fleet.

Am I overlooking anything cruicial as to how to accomplish the above in CloudFormation or should I be looking at adding the EC2:AttachVolume and EC2:DetachVolume permissions to the InstanceProfile and simply attaching the volume manually from within the EC2 instance?

Many thanks,


EC2 Spot instances now support the option of setting the "Interruption behavior" to stop instead of terminate .

When this option is selected, a spot instance retains its instance ID, EBS volumes, its private and elastic IP address, and its EBS volumes, which remain in place and attached.

Some instance types also support a "hybernate" option that writes a snapshot of the entire system state to EBS to allow the instance to "resume" rather than reboot when capacity becomes available again.

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-interruptions.html


What you are looking for is the BlockDeviceMappings property, found in the SpotFleet SpotFleetRequestConfigData LaunchSpecifications, which is a property of SpotFleetRequestConfigData, which is a property of the AWS::EC2::SpotFleet resource type.

The BlockDeviceMappings property will allow you to define additional EBS volumes to attach to your launch specification. This is the specification that controls device mappings at launch time.

For exmample:

"BlockDeviceMappings" : [{
  "DeviceName" : "/dev/sdf",
  "Ebs" : {"VolumeSize": "10", "VolumeType" : "gp2", "DeleteOnTermination" : "true"}
}],

will specify a 10GB volume on the /dev/sdf device of your spot fleet instance.

链接地址: http://www.djcxy.com/p/67176.html

上一篇: 将图像转换为HTML / CSS中的灰度

下一篇: 将EBS附加到Spot Fleet的EC2实例