gpt4 book ai didi

aws-cloudformation - 如何在生成竞价实例时避免使用队列选项?

转载 作者:行者123 更新时间:2023-12-04 15:19:41 25 4
gpt4 key购买 nike

我正在使用此 cloudformation 模板来创建 Spot 实例,它按预期工作。

https://github.com/shantanuo/cloudformation/blob/master/updated/linux_training.tpl.txt

我想要的唯一更改是从此处的类型中删除“Fleet”部分...

Type: 'AWS::EC2::SpotFleet

原因:当我尝试停止实例时,收到一条消息“无法停止队列实例”。

如何使用“正常”或默认选项购买现货实例?

(AWS 用户界面)向导允许我生成可以根据需要停止和启动的现货实例。从 cloudformation 模板中寻找相同的功能。


根据评论中的建议,我已将模板修改为如下所示。它按预期工作。但它只会创建一个"template"。我仍然需要登录控制台才能使用模板启动新实例。我需要自动化端到端流程。

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

当我删除 cloudformation 模板时,它不会删除它创建的 Spot 请求。


更新:

答案中提到的代码仅适用于Linux服务器。当我尝试启动 Windows 服务器时,出现错误:

Property validation failure: [Value of property {/LaunchTemplateData} does not match type {Object}]

我使用了这个模板:

Parameters:
1InstanceType:
Type: String
Default: t2.small
AllowedValues:
- t2.small
- m3.medium
- m3.xlarge
- i3.xlarge
2SecurityGroup:
Type: 'AWS::EC2::SecurityGroup::Id'
3KeyName:
Type: 'AWS::EC2::KeyPair::KeyName'
4LatestAmiId:
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: /aws/service/ami-windows-latest/Windows_Server-2016-English-Full-Base

Resources:
Ec2LaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: WindowsDesktop
LaunchTemplateData:
- ImageId: !Ref 4LatestAmiId
InstanceType: !Ref 1InstanceType
SecurityGroups:
- GroupId: !Ref 2SecurityGroup
KeyName: !Ref 3KeyName
InstanceMarketOptions:
MarketType: spot
SpotOptions:
SpotInstanceType: persistent
InstanceInterruptionBehavior: stop

最佳答案

以下是使用持久现货请求创建 LaunchTemplate 的工作示例,该示例基于模板创建实例。在持久现货请求上运行的实例可以停止,但不能终止(一旦有现货,就会启动替代实例):

Parameters:
LatestAmiId:
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: /aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-ebs
OksoftSG:
Type: 'AWS::EC2::SecurityGroup::Id'
KeyName:
Type: 'AWS::EC2::KeyPair::KeyName'
MyInstanceType:
Type: String
Default: m3.medium
AllowedValues:
- t2.small
- m3.medium
- m3.xlarge
- m3.2xlarge
- i3.xlarge
MyIpAddress:
Type: String

Resources:

SpotPersistantLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
InstanceType: !Ref MyInstanceType
ImageId: !Ref LatestAmiId
SecurityGroupIds:
- !Ref OksoftSG
KeyName: !Ref KeyName
InstanceMarketOptions:
MarketType: spot
SpotOptions:
InstanceInterruptionBehavior: stop
SpotInstanceType: persistent
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
DeleteOnTermination: true
VolumeType: standard
VolumeSize: 400
UserData: !Base64
'Fn::Join':
- ''
- - |
#!/bin/bash -xe
- >
yum install -y docker mysql git python-pip >
/tmp/line1_succ.txt 2> /tmp/line1_err.txt
- >
service docker start > /tmp/line2_succ.txt 2>
/tmp/line2_err.txt
- >
echo 'india' | passwd ec2-user --stdin >
/tmp/line4a_succ.txt 2> /tmp/line4a_err.txt
- >
sed -i 's|[#]*PasswordAuthentication
no|PasswordAuthentication yes|g' /etc/ssh/sshd_config >
/tmp/line4b_succ.txt 2> /tmp/line4b_err.txt
- >
/etc/init.d/sshd restart > /tmp/line4c_succ.txt 2>
/tmp/line4c_err.txt
- >
docker run -d -p 8887:8888 -v /tmp:/tmp shantanuo/notebook >
/tmp/line3_succ.txt 2> /tmp/line3_err.txt
- >-
pip install aws-ec2-assign-elastic-ip > /tmp/line4_succ.txt
2> /tmp/line4_err.txt

- 'sudo sh -c "echo '
- !ImportValue secretKey
- |2
>> /home/ec2-user/mysecret.txt"
- 'sudo sh -c "echo '
- !ImportValue accessKey
- |2
>> /home/ec2-user/myaccesskey.txt"
- >-
/usr/local/bin/aws-ec2-assign-elastic-ip --access-key ''`cat
/home/ec2-user/myaccesskey.txt`'' --secret-key ''`cat
/home/ec2-user/mysecret.txt`'' --valid-ips '
- !Ref MyIpAddress
- |
'

MyPersistantSpotInstance:
Type: AWS::EC2::Instance
Properties:
LaunchTemplate:
LaunchTemplateId: !Ref SpotPersistantLaunchTemplate
Version: 1

关于aws-cloudformation - 如何在生成竞价实例时避免使用队列选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63596488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com