-
Notifications
You must be signed in to change notification settings - Fork 14
/
Add-EC2.ps1
105 lines (89 loc) · 8.11 KB
/
Add-EC2.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function Add-EC2
{
<#
.Synopsis
Adds an EC2 instance to Amazon Web Services
.Description
Adds EC2 instances to Amazon Web Services.
.Example
Add-EC2 -ImageId ami-e31ccb8a #2k8R2
.Example
Add-EC2 -ImageId ami-41a37528 #2k8
.Example
Add-EC2 -ImageId ami-ddad7bb4 # Server Core
.Example
Add-Ec2 -ImageId ami-ddad7bb4 -InstanceType m1.medium
.Link
Get-Ami
.Link
Get-EC2
#>
param(
# The AMI ImageID
[Parameter(Mandatory=$true,Position=0,ValueFromPipelineByPropertyName=$true)]
[string]$ImageId,
# The name of the instance
[Parameter(ValueFromPipelineByPropertyName=$true,Position=1)]
[string]$Name,
# The Instance Type (or size) to create. The default is t1.micro
[Parameter(ValueFromPipelineByPropertyName=$true)]
[ValidateSet("m1.small", "m1.medium", "m1.large", "m1.xlarge", "t1.micro",
"m2.xlarge", "m2.2xlarge", "m2.4xlarge",
"c1.medium", "c1.xlarge", "cc1.4xlarge", "cc2.8xlarge", "cg1.4xlarge")]
[string]$InstanceType = "t1.micro",
# The minimum CPU Count
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Uint32]$MinCPUCount = 1,
# The maximum CPU Count
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Uint32]$MaxCPUCount = 1,
# The securityGroup that will contain the EC2 image
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string]$SecurityGroup = "RemoteDesktop",
# If set, returns the created item
[Switch]$PassThru
)
begin {
# Look up the existing instances
$existingInstances = Get-EC2
}
process {
# Look up the existing instances
$instanceCountWithThisAMI = @($existingInstances |
Where-Object { $_.ImageId -eq $imageId }).Count
$instanceCountWithThisAMI = "{0:x}" -f (Get-Random)
$riq = (New-Object Amazon.EC2.Model.RunInstancesRequest)
if (-not $?) {
Write-Error "EC2 c# SDK is not installed"
return
}
if (-not $psBoundParameters.Name) {
$name = "EC2_${ImageId}_${instanceCountWithThisAMI}"
}
if ($psBoundParameters.SecurityGroup) {
# They've selected an existing security group, so find it
$securityGroupId =
Get-EC2SecurityGroup|
Where-Object {
$_.GroupName -eq $SecurityGroup
} |
Select-Object -ExpandProperty GroupId
} else {
# Create a security group on their behalf
$securityGroup = "EC2_${ImageId}_${instanceCountWithThisAMI}_SecurityGroup"
$securityGroupRequest = (New-Object Amazon.EC2.Model.CreateSecurityGroupRequest).WithGroupName($SecurityGroup)
$securityGroupRequest = $securityGroupRequest.WithGroupDescription("SecurityGroup for EC2 ${ImageId}_${instanceCountWithThisAMI}")
$securityGroupResult = $AwsConnections.EC2.CreateSecurityGroup($securityGroupRequest)
$securityGroupId = $securityGroupResult.CreateSecurityGroupResult.GroupId
}
$keyPairRequest = (New-Object Amazon.EC2.Model.CreateKeyPairRequest).WithKeyName($name)
$keyPairOutput = $AwsConnections.EC2.CreateKeyPair($keyPairRequest)
# Store the key material encrypted in the registry, so we can decrypt the password later.
Add-SecureSetting -Name $keyPairOutput.CreateKeyPairResult.KeyPair.KeyName -String $keyPairOutput.CreateKeyPairResult.KeyPair.KeyMaterial
$riq = $riq.WithInstanceType($InstanceType).WithMaxCount($MaxCPUCount).WithMinCount($MinCPUCount).WithImageId("$ImageId").WithKeyName($name).WithSecurityGroupId($SecurityGroupId)
$null = $AwsConnections.EC2.RunInstances($riq)
if ($PassThru) {
Get-EC2 -Name $name
}
}
}