用于 ec2 实例中的现场请求的 aws cloudformation 模板

aws cloudformation template for spot request in ec2 instance

我需要一个示例云形成模板来添加 spot 请求,同时在 AWS.I 中配置 ec2 实例已尝试使用控制台来配置 spot 实例,但我找不到任何用于在 ec2 中添加 spot 请求的确切模板

您可以创建一个 SpotFleet 资源,这里是一个示例

SpotFleet:
 Type: AWS::EC2::SpotFleet
 Properties:
   SpotFleetRequestConfigData:
     IamFleetRole: !GetAtt [IAMFleetRole, Arn]
     SpotPrice: '1000'
     TargetCapacity:
       Ref: TargetCapacity
     LaunchSpecifications:
     - EbsOptimized: 'false'
       InstanceType:
         Ref: InstanceType
     ImageId:
       Fn::FindInMap:
       - AWSRegionArch2AMI
       - Ref: AWS::Region
       - Fn::FindInMap:
         - AWSInstanceType2Arch
         - Ref: InstanceType
         - Arch
     SubnetId:
       Ref: Subnet1
     WeightedCapacity: '8'
   - EbsOptimized: 'true'
     InstanceType:
       Ref: InstanceType
     ImageId:
       Fn::FindInMap:
       - AWSRegionArch2AMI
       - Ref: AWS::Region
       - Fn::FindInMap:
       - AWSInstanceType2Arch
       - Ref: InstanceType
       - Arch
     Monitoring:
       Enabled: 'true'
       SecurityGroups:
        - GroupId:
            Fn::GetAtt:
            - SG0
            - GroupId
      SubnetId:
         Ref: Subnet0
      IamInstanceProfile:
        Arn:
        Fn::GetAtt:
        - RootInstanceProfile
        - Arn
      WeightedCapacity: '8'

您需要创建 Spot 队列资源。

示例:

"SpotFleet": {
  "Type": "AWS::EC2::SpotFleet",
  "Properties": {
    "SpotFleetRequestConfigData": {
     "IamFleetRole": { "Fn::GetAtt": [ "IAMFleetRole", "Arn"] },
     "SpotPrice": "1000",
     "TargetCapacity": { "Ref": "TargetCapacity" },
     "LaunchSpecifications": [
     {
         "EbsOptimized": "false",
         "InstanceType": { "Ref": "InstanceType" },
         "ImageId": { "Fn::FindInMap": [ "AWSRegionArch2AMI", { "Ref": "AWS::Region" },
                      { "Fn::FindInMap": [ "AWSInstanceType2Arch", { "Ref": "InstanceType" }, "Arch" ] }
                     ]},
          "SubnetId": { "Ref": "Subnet1" },
          "WeightedCapacity": "8"
     },
     {
         "EbsOptimized": "true",
         "InstanceType": { "Ref": "InstanceType" },
         "ImageId": { "Fn::FindInMap": [ "AWSRegionArch2AMI", { "Ref": "AWS::Region" },
                      { "Fn::FindInMap": [ "AWSInstanceType2Arch", { "Ref": "InstanceType" }, "Arch" ] }
                     ]},
          "Monitoring": { "Enabled": "true" },
          "SecurityGroups": [ { "GroupId": { "Fn::GetAtt": [ "SG0", "GroupId" ] } } ],
          "SubnetId": { "Ref": "Subnet0" },
          "IamInstanceProfile": { "Arn": { "Fn::GetAtt": [ "RootInstanceProfile", "Arn" ] } },
          "WeightedCapacity": "8"
         }
         ]
     }
   }
}

可以在 link 中找到更多详细信息: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-spotfleet.html