I am using this cloudformation template to create spot instances and it is working as expected.
https://github.com/shantanuo/cloudformation/blob/master/updated/linux_training.tpl.txt
The only change I would like is to remove "Fleet" part from the type here...
Type: 'AWS::EC2::SpotFleet
The reason: When I try to stop an instance, I get a message that says "Fleet instances can not be stopped".
How do I buy spot instances using "normal" or default option?
The (AWS User interface) wizard allows me to generate spot instances those can be stopped and started as and when required. Looking for the same functionality from cloudformation template.
As suggested in the comments, I have modified the template to look like this. It works as expected. But it will create only a "template". I will still need to login to console to initiate a new instance using the template. I need to automate end-to-end process.
Parameters:
LatestAmiId:
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-minimal-hvm-arm64-ebs
OksoftSG:
Type: 'AWS::EC2::SecurityGroup::Id'
KeyName:
Type: 'AWS::EC2::KeyPair::KeyName'
MyInstanceType:
Type: String
Default: r6g.medium
AllowedValues:
- r6g.medium
- r6g.4xlarge
- r6g.12xlarge
MyIpAddress:
Type: String
Resources:
MySpotInstance:
Type: 'AWS::EC2::LaunchTemplate'
Properties:
LaunchTemplateData:
ImageId: !Ref LatestAmiId
KeyName: !Ref KeyName
SecurityGroupIds:
- !Ref OksoftSG
InstanceType: !Ref MyInstanceType
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
DeleteOnTermination: true
VolumeType: standard
VolumeSize: 400
InstanceMarketOptions:
MarketType: spot
SpotOptions:
InstanceInterruptionBehavior: stop
SpotInstanceType: persistent
MyInstance:
Type: 'AWS::EC2::Instance'
Properties:
"LaunchTemplate" :
LaunchTemplateId: !Ref MySpotInstance
Version: !GetAtt MySpotInstance.LatestVersionNumber
When I delete the cloudformation template, it does not remove the spot request that it has created.