Skip to content

Instantly share code, notes, and snippets.

@pcgeek86
Last active July 20, 2021 16:32
Show Gist options
  • Select an option

  • Save pcgeek86/523b3b7c80eb25bd7f993354509268d0 to your computer and use it in GitHub Desktop.

Select an option

Save pcgeek86/523b3b7c80eb25bd7f993354509268d0 to your computer and use it in GitHub Desktop.

Revisions

  1. pcgeek86 revised this gist Jul 20, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion build-aws-vpc.ps1
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ $ElasticIP = New-EC2Address
    $NATGateway = New-EC2NatGateway -Region $Region -SubnetId $PublicSubnet1.SubnetId -AllocationId $ElasticIP.AllocationId
    while ((Get-EC2NatGateway -NatGatewayId $NATGateway.NatGateway.NatGatewayId).State -ne 'Available') {
    Write-Host -Object ('Waiting for NAT Gateway {0} to be available ...' -f $NATGateway.NatGateway.NatGatewayId)
    Start-Sleep -Seconds 1
    Start-Sleep -Seconds 10
    }

    # We need a separate EC2 VPC Route Table to enable private subnets to route outbound to Internet
  2. pcgeek86 revised this gist Jul 20, 2021. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions build-aws-vpc.ps1
    Original file line number Diff line number Diff line change
    @@ -23,15 +23,18 @@ function Add-EC2NameTag {

    $Region = 'us-west-2'
    $VPC = New-EC2Vpc -CidrBlock 10.8.0.0/16
    Write-Host -Object ('Created new VPC {0}' -f $VPC.VpcId)

    $PublicSubnet1 = New-EC2Subnet -VpcId $VPC.VpcId -CidrBlock 10.8.1.0/24 -Region $Region -AvailabilityZone us-west-2a
    $PublicSubnet2 = New-EC2Subnet -VpcId $VPC.VpcId -CidrBlock 10.8.2.0/24 -Region $Region -AvailabilityZone us-west-2c
    $PrivateSubnet1 = New-EC2Subnet -VpcId $VPC.VpcId -CidrBlock 10.8.6.0/24 -Region $Region -AvailabilityZone us-west-2b
    $PrivateSubnet2 = New-EC2Subnet -VpcId $VPC.VpcId -CidrBlock 10.8.7.0/24 -Region $Region -AvailabilityZone us-west-2c
    Write-Host -Object ('Created VPC subnets: {0} {1} {2} {3}' -f $PublicSubnet1.SubnetId, $PublicSubnet2.SubnetId, $PrivateSubnet1.SubnetId, $PrivateSubnet2.SubnetId)

    # Create VPC Internet Gateway and attach to VPC
    $InternetGateway = New-EC2InternetGateway -Region $Region
    Add-EC2InternetGateway -InternetGatewayId $InternetGateway.InternetGatewayId -VpcId $VPC.VpcId
    Write-Host -Object ('Created Internet Gateway {0}' -f $InternetGateway.InternetGatewayId)

    # Add route to Internet to public subnets
    $MainRouteTableFilter = [Amazon.EC2.Model.Filter]::new()
  3. pcgeek86 revised this gist Jul 20, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions build-aws-vpc.ps1
    Original file line number Diff line number Diff line change
    @@ -43,8 +43,8 @@ $null = New-EC2Route -GatewayId $InternetGateway.InternetGatewayId -DestinationC
    # For private subnets to access Internet, create a NAT Gateway
    $ElasticIP = New-EC2Address
    $NATGateway = New-EC2NatGateway -Region $Region -SubnetId $PublicSubnet1.SubnetId -AllocationId $ElasticIP.AllocationId
    while (!(Get-EC2NatGateway -NatGatewayId $NATGateway.NatGateway.NatGatewayId)) {
    Write-Host -Object ('Waiting for NAT Gateway {0} to be found ...' -f $NATGateway.NatGateway.NatGatewayId)
    while ((Get-EC2NatGateway -NatGatewayId $NATGateway.NatGateway.NatGatewayId).State -ne 'Available') {
    Write-Host -Object ('Waiting for NAT Gateway {0} to be available ...' -f $NATGateway.NatGateway.NatGatewayId)
    Start-Sleep -Seconds 1
    }

  4. pcgeek86 revised this gist Jul 20, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion build-aws-vpc.ps1
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ $null = New-EC2Route -GatewayId $InternetGateway.InternetGatewayId -DestinationC
    # For private subnets to access Internet, create a NAT Gateway
    $ElasticIP = New-EC2Address
    $NATGateway = New-EC2NatGateway -Region $Region -SubnetId $PublicSubnet1.SubnetId -AllocationId $ElasticIP.AllocationId
    while (!(Get-EC2NatGateway -NatGatewayId $NATGateway.NatGateway.NatGatewayId) {
    while (!(Get-EC2NatGateway -NatGatewayId $NATGateway.NatGateway.NatGatewayId)) {
    Write-Host -Object ('Waiting for NAT Gateway {0} to be found ...' -f $NATGateway.NatGateway.NatGatewayId)
    Start-Sleep -Seconds 1
    }
  5. pcgeek86 revised this gist Jul 20, 2021. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion build-aws-vpc.ps1
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,10 @@ $null = New-EC2Route -GatewayId $InternetGateway.InternetGatewayId -DestinationC
    # For private subnets to access Internet, create a NAT Gateway
    $ElasticIP = New-EC2Address
    $NATGateway = New-EC2NatGateway -Region $Region -SubnetId $PublicSubnet1.SubnetId -AllocationId $ElasticIP.AllocationId
    $NATGateway.NatGateway.NatGatewayId
    while (!(Get-EC2NatGateway -NatGatewayId $NATGateway.NatGateway.NatGatewayId) {
    Write-Host -Object ('Waiting for NAT Gateway {0} to be found ...' -f $NATGateway.NatGateway.NatGatewayId)
    Start-Sleep -Seconds 1
    }

    # We need a separate EC2 VPC Route Table to enable private subnets to route outbound to Internet
    $PrivateRouteTable = New-EC2RouteTable -VpcId $VPC.VpcId
  6. pcgeek86 revised this gist Jul 20, 2021. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions build-aws-vpc.ps1
    Original file line number Diff line number Diff line change
    @@ -34,8 +34,10 @@ $InternetGateway = New-EC2InternetGateway -Region $Region
    Add-EC2InternetGateway -InternetGatewayId $InternetGateway.InternetGatewayId -VpcId $VPC.VpcId

    # Add route to Internet to public subnets
    $MainRouteTableFilter = [Filter]
    $RouteTable = Get-EC2RouteTable -Filter | ? VpcId -eq $VPC.VpcId -and
    $MainRouteTableFilter = [Amazon.EC2.Model.Filter]::new()
    $MainRouteTableFilter.Name = 'association.main'
    $MainRouteTableFilter.Values = @('true')
    $RouteTable = Get-EC2RouteTable -Filter $MainRouteTableFilter | ? VpcId -eq $VPC.VpcId
    $null = New-EC2Route -GatewayId $InternetGateway.InternetGatewayId -DestinationCidrBlock 0.0.0.0/0 -RouteTableId $RouteTable.RouteTableId

    # For private subnets to access Internet, create a NAT Gateway
  7. pcgeek86 revised this gist Jul 20, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion build-aws-vpc.ps1
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    - VPC contains two public subnets and two private subnets.
    - NAT Gateway is created to handle outbount Internet traffic from private subnets.
    - Internet Gateway is created to handle in/outbound Internet traffic for public subnets.
    - iex (iwr https://gist.github.com/pcgeek86/523b3b7c80eb25bd7f993354509268d0/raw/a1534860318869da9b7542c30ae5929069832048/build-aws-vpc.ps1).Content
    - iex (iwr https://gist.github.com/pcgeek86/523b3b7c80eb25bd7f993354509268d0/raw).Content
    #>
    function Add-EC2NameTag {
    <#
  8. pcgeek86 revised this gist Jul 20, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion build-aws-vpc.ps1
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    - VPC contains two public subnets and two private subnets.
    - NAT Gateway is created to handle outbount Internet traffic from private subnets.
    - Internet Gateway is created to handle in/outbound Internet traffic for public subnets.
    - iex (iwr https://gist.github.com/pcgeek86/523b3b7c80eb25bd7f993354509268d0/raw/a1534860318869da9b7542c30ae5929069832048/build-aws-vpc.ps1).Content
    #>
    function Add-EC2NameTag {
    <#
    @@ -21,7 +22,7 @@ function Add-EC2NameTag {
    }

    $Region = 'us-west-2'
    $VPC = New-EC2Vpc -CidrBlock 10.8.0.0/16 -TagSpecification
    $VPC = New-EC2Vpc -CidrBlock 10.8.0.0/16

    $PublicSubnet1 = New-EC2Subnet -VpcId $VPC.VpcId -CidrBlock 10.8.1.0/24 -Region $Region -AvailabilityZone us-west-2a
    $PublicSubnet2 = New-EC2Subnet -VpcId $VPC.VpcId -CidrBlock 10.8.2.0/24 -Region $Region -AvailabilityZone us-west-2c
  9. pcgeek86 revised this gist Jul 20, 2021. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion build-aws-vpc.ps1
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,9 @@

    <#
    - Creates an Amazon Web Services (AWS) Virtual Private Cloud (VPC) in the us-west-2 (Oregon) region.
    - VPC contains two public subnets and two private subnets.
    - NAT Gateway is created to handle outbount Internet traffic from private subnets.
    - Internet Gateway is created to handle in/outbound Internet traffic for public subnets.
    #>
    function Add-EC2NameTag {
    <#
    .Synopsis
  10. pcgeek86 renamed this gist Jul 20, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.txt → build-aws-vpc.ps1
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@

    function Add-EC2NameTag {
    <#
    .Synopsis
  11. pcgeek86 created this gist Jul 15, 2021.
    61 changes: 61 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    function Add-EC2NameTag {
    <#
    .Synopsis
    Helper function that adds a Name tag to an EC2 resource ID.
    #>
    [CmdletBinding()]
    param (
    [Parameter(Mandatory = $true)]
    [string] $Name,
    [Parameter(Mandatory = $true)]
    [string] $ResourceId
    )
    $Tag = [Amazon.EC2.Model.Tag]::new('Name', $Name)
    New-EC2Tag -Tag $Tag -Resource $ResourceId
    }

    $Region = 'us-west-2'
    $VPC = New-EC2Vpc -CidrBlock 10.8.0.0/16 -TagSpecification

    $PublicSubnet1 = New-EC2Subnet -VpcId $VPC.VpcId -CidrBlock 10.8.1.0/24 -Region $Region -AvailabilityZone us-west-2a
    $PublicSubnet2 = New-EC2Subnet -VpcId $VPC.VpcId -CidrBlock 10.8.2.0/24 -Region $Region -AvailabilityZone us-west-2c
    $PrivateSubnet1 = New-EC2Subnet -VpcId $VPC.VpcId -CidrBlock 10.8.6.0/24 -Region $Region -AvailabilityZone us-west-2b
    $PrivateSubnet2 = New-EC2Subnet -VpcId $VPC.VpcId -CidrBlock 10.8.7.0/24 -Region $Region -AvailabilityZone us-west-2c

    # Create VPC Internet Gateway and attach to VPC
    $InternetGateway = New-EC2InternetGateway -Region $Region
    Add-EC2InternetGateway -InternetGatewayId $InternetGateway.InternetGatewayId -VpcId $VPC.VpcId

    # Add route to Internet to public subnets
    $MainRouteTableFilter = [Filter]
    $RouteTable = Get-EC2RouteTable -Filter | ? VpcId -eq $VPC.VpcId -and
    $null = New-EC2Route -GatewayId $InternetGateway.InternetGatewayId -DestinationCidrBlock 0.0.0.0/0 -RouteTableId $RouteTable.RouteTableId

    # For private subnets to access Internet, create a NAT Gateway
    $ElasticIP = New-EC2Address
    $NATGateway = New-EC2NatGateway -Region $Region -SubnetId $PublicSubnet1.SubnetId -AllocationId $ElasticIP.AllocationId
    $NATGateway.NatGateway.NatGatewayId

    # We need a separate EC2 VPC Route Table to enable private subnets to route outbound to Internet
    $PrivateRouteTable = New-EC2RouteTable -VpcId $VPC.VpcId
    $null = New-EC2Route -DestinationCidrBlock 0.0.0.0/0 -NatGatewayId $NATGateway.NatGateway.NatGatewayId -RouteTableId $PrivateRouteTable.RouteTableId

    # Associate the private subnets with the route table pointing to the NAT Gateway
    Register-EC2RouteTable -SubnetId $PrivateSubnet1.SubnetId -RouteTableId $PrivateRouteTable.RouteTableId
    Register-EC2RouteTable -SubnetId $PrivateSubnet2.SubnetId -RouteTableId $PrivateRouteTable.RouteTableId

    # Add tags to VPC resources
    $TagName = 'Trevor'
    Add-EC2NameTag -Name $TagName -Resource $VPC.VpcId
    Add-EC2NameTag -Name $TagName -Resource $InternetGateway.InternetGatewayId
    Add-EC2NameTag -Name $TagName -Resource $RouteTable.RouteTableId
    Add-EC2NameTag -Name $TagName -Resource $NATGateway.NatGateway.NatGatewayId

    # Tag the subnets that were created earlier
    Add-EC2NameTag -Name Trevor-PublicSubnet1 -Resource $PublicSubnet1.SubnetId
    Add-EC2NameTag -Name Trevor-PublicSubnet2 -Resource $PublicSubnet2.SubnetId
    Add-EC2NameTag -Name Trevor-PrivateSubnet1 -Resource $PrivateSubnet1.SubnetId
    Add-EC2NameTag -Name Trevor-PrivateSubnet2 -Resource $PrivateSubnet2.SubnetId

    # Tag the private route table
    Add-EC2NameTag -Name PrivateRouteTable -Resource $PrivateRouteTable.RouteTableId