文件範例儲存庫中有更多 AWS SDK可用的範例。 AWS SDK
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
RequestSpotInstances
搭配 使用 CLI
下列程式碼範例示範如何使用 RequestSpotInstances
。
- CLI
-
- AWS CLI
-
請求 Spot 執行個體
此範例命令會為指定可用區域中的五個執行個體建立一次性 Spot 執行個體請求。如果您的帳戶EC2僅支援 VPC,Amazon 會在指定可用區域的預設子網路中EC2啟動執行個體。如果您的帳戶支援 EC2-Classic,Amazon 會在指定的可用區域中EC2啟動 EC2-Classic 中的執行個體。
命令:
aws ec2 request-spot-instances --spot-price
"0.03"
--instance-count5
--type"one-time"
--launch-specificationfile://specification.json
Specification.json:
{ "ImageId": "ami-1a2b3c4d", "KeyName": "my-key-pair", "SecurityGroupIds": [ "sg-1a2b3c4d" ], "InstanceType": "m3.medium", "Placement": { "AvailabilityZone": "us-west-2a" }, "IamInstanceProfile": { "Arn": "arn:aws:iam::123456789012:instance-profile/my-iam-role" } }
輸出:
{ "SpotInstanceRequests": [ { "Status": { "UpdateTime": "2014-03-25T20:54:21.000Z", "Code": "pending-evaluation", "Message": "Your Spot request has been submitted for review, and is pending evaluation." }, "ProductDescription": "Linux/UNIX", "SpotInstanceRequestId": "sir-df6f405d", "State": "open", "LaunchSpecification": { "Placement": { "AvailabilityZone": "us-west-2a" }, "ImageId": "ami-1a2b3c4d", "KeyName": "my-key-pair", "SecurityGroups": [ { "GroupName": "my-security-group", "GroupId": "sg-1a2b3c4d" } ], "Monitoring": { "Enabled": false }, "IamInstanceProfile": { "Arn": "arn:aws:iam::123456789012:instance-profile/my-iam-role" }, "InstanceType": "m3.medium" }, "Type": "one-time", "CreateTime": "2014-03-25T20:54:20.000Z", "SpotPrice": "0.050000" }, ... ] }
此範例命令會為指定子網路中的五個執行個體建立一次性 Spot 執行個體請求。Amazon 會在指定的子網路中EC2啟動執行個體。如果 VPC 是非預設 VPC,執行個體預設不會收到公有 IP 地址。
命令:
aws ec2 request-spot-instances --spot-price
"0.050"
--instance-count5
--type"one-time"
--launch-specificationfile://specification.json
Specification.json:
{ "ImageId": "ami-1a2b3c4d", "SecurityGroupIds": [ "sg-1a2b3c4d" ], "InstanceType": "m3.medium", "SubnetId": "subnet-1a2b3c4d", "IamInstanceProfile": { "Arn": "arn:aws:iam::123456789012:instance-profile/my-iam-role" } }
輸出:
{ "SpotInstanceRequests": [ { "Status": { "UpdateTime": "2014-03-25T22:21:58.000Z", "Code": "pending-evaluation", "Message": "Your Spot request has been submitted for review, and is pending evaluation." }, "ProductDescription": "Linux/UNIX", "SpotInstanceRequestId": "sir-df6f405d", "State": "open", "LaunchSpecification": { "Placement": { "AvailabilityZone": "us-west-2a" } "ImageId": "ami-1a2b3c4d" "SecurityGroups": [ { "GroupName": "my-security-group", "GroupID": "sg-1a2b3c4d" } ] "SubnetId": "subnet-1a2b3c4d", "Monitoring": { "Enabled": false }, "IamInstanceProfile": { "Arn": "arn:aws:iam::123456789012:instance-profile/my-iam-role" }, "InstanceType": "m3.medium", }, "Type": "one-time", "CreateTime": "2014-03-25T22:21:58.000Z", "SpotPrice": "0.050000" }, ... ] }
此範例會將公有 IP 地址指派給您在非預設 中啟動的 Spot 執行個體VPC。請注意,當您指定網路介面時,您必須使用網路介面包含子網路 ID 和安全群組 ID。
命令:
aws ec2 request-spot-instances --spot-price
"0.050"
--instance-count1
--type"one-time"
--launch-specificationfile://specification.json
Specification.json:
{ "ImageId": "ami-1a2b3c4d", "KeyName": "my-key-pair", "InstanceType": "m3.medium", "NetworkInterfaces": [ { "DeviceIndex": 0, "SubnetId": "subnet-1a2b3c4d", "Groups": [ "sg-1a2b3c4d" ], "AssociatePublicIpAddress": true } ], "IamInstanceProfile": { "Arn": "arn:aws:iam::123456789012:instance-profile/my-iam-role" } }
-
如需API詳細資訊,請參閱 命令參考 RequestSpotInstances
中的 。 AWS CLI
-
- PowerShell
-
- 適用於 的工具 PowerShell
-
範例 1:此範例會在指定的子網路中請求一次性 Spot 執行個體。請注意,必須針對包含指定子網路VPC的 建立安全群組,且必須使用網路介面透過 ID 指定。當您指定網路介面時,您必須使用網路介面包含子網路 ID。
$n = New-Object Amazon.EC2.Model.InstanceNetworkInterfaceSpecification $n.DeviceIndex = 0 $n.SubnetId = "subnet-12345678" $n.Groups.Add("sg-12345678") Request-EC2SpotInstance -InstanceCount 1 -SpotPrice 0.050 -Type one-time ` -IamInstanceProfile_Arn arn:aws:iam::123456789012:instance-profile/my-iam-role ` -LaunchSpecification_ImageId ami-12345678 ` -LaunchSpecification_InstanceType m3.medium ` -LaunchSpecification_NetworkInterface $n
輸出:
ActualBlockHourlyPrice : AvailabilityZoneGroup : BlockDurationMinutes : 0 CreateTime : 12/26/2015 7:44:10 AM Fault : InstanceId : LaunchedAvailabilityZone : LaunchGroup : LaunchSpecification : Amazon.EC2.Model.LaunchSpecification ProductDescription : Linux/UNIX SpotInstanceRequestId : sir-12345678 SpotPrice : 0.050000 State : open Status : Amazon.EC2.Model.SpotInstanceStatus Tags : {} Type : one-time
-
如需API詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 RequestSpotInstances中的 。
-