本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
搭配使用 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.jsonSpecification.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.jsonSpecification.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 地址指派給您在非預設 VPC 中啟動的 Spot 執行個體。請注意,當您指定網路界面時,您必須使用網路界面包含子網路 ID 和安全群組 ID。 命令: aws ec2 request-spot-instances --spot-price"0.050"--instance-count1--type"one-time"--launch-specificationfile://specification.jsonSpecification.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 詳細資訊,請參閱《 AWS CLI 命令參考》中的 RequestSpotInstances 。 
 
- 
                    
 
- PowerShell
- 
            - PowerShell V4 的工具
- 
             
                    範例 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 Reference (V4) 中的 RequestSpotInstances。 
 
- 
                    
- PowerShell V5 的工具
- 
             
                    範例 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 Reference (V5) 中的 RequestSpotInstances。 
 
- 
                    
 
如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱 使用 SDK 建立 Amazon EC2 資源 AWS。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。