-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCreate-Spot.ps1
28 lines (23 loc) · 1.18 KB
/
Create-Spot.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Param(
[parameter(Mandatory=$true)]
[string]
$ami,
[parameter(Mandatory=$true)]
[string]
$type
)
Set-DefaultAWSRegion $env:AWSRegion
if($env:EC2UserData) { $userdata64 = [System.Convert]::ToBase64String( [System.Text.Encoding]::UTF8.GetBytes( $env:EC2UserData ) ) }
if($env:EC2KeyName -eq $null) { $env:EC2KeyName = (Get-EC2KeyPair)[0].KeyName }
$role = Get-IAMInstanceProfileForRole $env:IAMRole
if($env:SubnetId)
{
$sg = new-object Amazon.EC2.Model.GroupIdentifier
$sg.GroupId = $env:SecurityGroup
$spot = Request-EC2SpotInstance -SpotPrice $env:SpotPrice -LaunchSpecification_InstanceType $type -LaunchSpecification_ImageId $ami -LaunchSpecification_AllSecurityGroup $sg -IamInstanceProfile_Arn $role.Arn -LaunchSpecification_UserData $userdata64 -LaunchSpecification_SubnetId $env:SubnetId -LaunchSpecification_KeyName $env:EC2KeyName
}
else
{
$spot = Request-EC2SpotInstance -SpotPrice $env:SpotPrice -LaunchSpecification_InstanceType $type -LaunchSpecification_ImageId $ami -LaunchSpecification_SecurityGroup $env:SecurityGroup -IamInstanceProfile_Arn $role.Arn -LaunchSpecification_UserData $userdata64 -LaunchSpecification_KeyName $env:EC2KeyName
}
$spot.SpotInstanceRequestId