0

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

arya b raj
  • 13
  • 2

2 Answers2

1

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'
Anis Smail
  • 707
  • 7
  • 22
1

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

Tapan Hegde
  • 1,222
  • 1
  • 8
  • 25
  • Thank you ! but in azure we can make spot instance with 3 paramerter like – arya b raj Oct 22 '21 at 04:50
  • "priority": "Spot", "evictionPolicy": "Deallocate", "billingProfile": { "maxPrice": -1 }https://learn.microsoft.com/en-us/azure/virtual-machines/linux/spot-template same way in aws we can make it? – arya b raj Oct 22 '21 at 04:51
  • in aws have ec2 fleet option is there may i know the advantage https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-ec2fleet-spotoptionsrequest.html – arya b raj Oct 22 '21 at 04:53
  • @aryabraj, I guess those 3 are additional properties which can be added. Complete json sample format is given in same link just below those 3 parameters (big one!!).I guess you need that sample format to create spot instances in azure. Moreover, it all depends on cloud platform which you are using. Respective template for cloud platform will differ from other platform. – Tapan Hegde Oct 22 '21 at 05:33
  • from this request i am getting spot request as fleet, nut i need to create spot instance means spot request type as instance, through console we can enable spot request type while provision a instance, same way have any parameter available in cloudformation template – arya b raj Oct 23 '21 at 13:35