Home > Software design >  aws cloudformation template for spot request in ec2 instance
aws cloudformation template for spot request in ec2 instance

Time:10-22

I need a sample cloud formation template to add spot requests while provisioning the ec2 instance in AWS.I have tried with console to provision spot instances but I couldn't find any exact template for add spot request in ec2

CodePudding user response:

You Can create a SpotFleet resource, here's an example

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'

CodePudding user response:

You need to create Spot-fleet resource.

Example :

"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"
         }
         ]
     }
   }
}

More details can be found in this link : https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-spotfleet.html

  • Related