Skip to content

Instantly share code, notes, and snippets.

@pcgeek86
Created January 19, 2020 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcgeek86/8e82c68a5fbf99d28726f547c8d32e55 to your computer and use it in GitHub Desktop.
Save pcgeek86/8e82c68a5fbf99d28726f547c8d32e55 to your computer and use it in GitHub Desktop.
Create VPC, AWS Directory Service, and Amazon WorkSpace, using AWS PowerShell module
$ErrorActionPreference = 'Stop'
Install-Module -Name AWS.Tools.DirectoryService, AWS.Tools.EC2, AWS.Tools.WorkSpaces -Scope CurrentUser -Force
Update-AWSToolsModule
$VPC = New-EC2Vpc -CidrBlock 10.5.0.0/16
$VPC
$PrivateSubnet1 = New-EC2Subnet -VpcId $VPC.VpcId -CidrBlock 10.5.5.0/24 -AvailabilityZone us-west-2a
$PrivateSubnet2 = New-EC2Subnet -VpcId $VPC.VpcId -CidrBlock 10.5.6.0/24 -AvailabilityZone us-west-2b
$PublicSubnet1 = New-EC2Subnet -VpcId $VPC.VpcId -CidrBlock 10.5.10.0/24 -AvailabilityZone us-west-2b
Edit-EC2SubnetAttribute -MapPublicIpOnLaunch $true -SubnetId $PublicSubnet1.SubnetId
# Create an Elastic IP Address
$NATGatewayEIP = New-EC2Address -Domain vpc
# Create a NAT Gateway, using the Elastic IP Address
$NATGateway = New-EC2NatGateway -SubnetId $PublicSubnet1.SubnetId -AllocationId $NATGatewayEIP.AllocationId
# Create an Internet Gateway
$InternetGateway = New-EC2InternetGateway
# Attach the Internet Gateway to the VPC
Add-EC2InternetGateway -InternetGatewayId $InternetGateway.InternetGatewayId -VpcId $VPC.VpcId
$RouteTable = New-EC2RouteTable -VpcId $VPC.VpcId
New-EC2Route -RouteTableId $RouteTable.RouteTableId -DestinationCidrBlock 0.0.0.0/0 -NatGatewayId $NATGateway.NatGateway.NatGatewayId
Register-EC2RouteTable -RouteTableId $RouteTable.RouteTableId -SubnetId $PrivateSubnet1.SubnetId
Register-EC2RouteTable -RouteTableId $RouteTable.RouteTableId -SubnetId $PrivateSubnet2.SubnetId
function Get-EC2MainRouteTable {
[CmdletBinding()]
[OutputType([Amazon.EC2.Model.RouteTable])]
param (
[Parameter(Mandatory = $true)]
[string] $VpcId
)
$FilterList = @(
[Amazon.EC2.Model.Filter]::new('association.main', 'true')
[Amazon.EC2.Model.Filter]::new('vpc-id', $VpcId)
)
Get-EC2RouteTable -Filter $FilterList
}
$MainRouteTable = Get-EC2MainRouteTable -VpcId $VPC.VpcId
New-EC2Route -RouteTableId $MainRouteTable.RouteTableId -DestinationCidrBlock 0.0.0.0/0 -GatewayId $InternetGateway.InternetGatewayId
$DirectoryParams = @{
VpcSettings_SubnetId = @($PrivateSubnet1.SubnetId, $PrivateSubnet2.SubnetId)
VpcSettings_VpcId = $VPC.VpcId
Name = 'workspaces.local'
Password = 'SuperSecret(())\\!08df44da994561768'
Description = 'AWS WorkSpaces'
Size = 'small'
}
$Directory = New-DSDirectory @DirectoryParams
Get-DSDirectory -DirectoryId $Directory
function Wait-DSDirectory {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $DirectoryId
)
while ((Get-DSDirectory).Stage -eq 'Creating') {
Write-Host -Object 'Waiting for Directory Service creation ...'
Start-Sleep -Seconds 30
}
}
# Wait for the Directory Service to finish creating
Wait-DSDirectory -DirectoryId $Directory
# Register the Directory with AWS WorkSpaces
Register-WKSWorkspaceDirectory -DirectoryId ([string]$Directory) -Tenancy SHARED -EnableWorkDoc $false
Get-WKSWorkspaceDirectories -DirectoryId $Directory
$WorkSpace = [Amazon.WorkSpaces.Model.WorkspaceRequest]::new()
$WorkSpace.BundleId = 'wsb-8pmj7b7pq'
$WorkSpace.DirectoryId = $Directory
$WorkSpace.UserName = 'trevor'
$WorkSpace.WorkspaceProperties = [Amazon.WorkSpaces.Model.WorkspaceProperties]::new()
$WorkSpace.WorkspaceProperties.RunningModeAutoStopTimeoutInMinutes = 60
$WorkSpace.WorkspaceProperties.RunningMode = [Amazon.WorkSpaces.RunningMode]::AUTO_STOP
$Result = New-WKSWorkspace -Workspace $WorkSpace
$Result.FailedRequests
(Get-WKSWorkspaceBundles).Count
(Get-WKSWorkspaceBundle).Count
New-EC2Instance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment