Export/Public/New-ApplicationGateway.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
function New-ApplicationGateway {
    [CmdletBinding()]
    <#
    .SYNOPSIS
        ...
    .DESCRIPTION
        ...
    #>

    param(        
        [Parameter(Mandatory = $true)]
        [string]
        $ResourceGroupName,
        [Parameter(Mandatory = $true)]
        [string]
        $ResourceLocation,
        [Parameter(Mandatory = $true)]
        [string]
        $ApplicationGatewayName,
        [Parameter(Mandatory = $true)]
        [string]
        $VMScaleSetName,
        [Parameter(Mandatory = $false)]
        [string]
        $FrontEndIpConfigNamePrivate = "$($VMScaleSetName)FrontEnd-private",
        [Parameter(Mandatory = $false)]
        [string]
        $FrontEndIpConfigNamePublic = "$($VMScaleSetName)FrontEnd-public",
        [Parameter(Mandatory = $false)]
        [string]
        $BackendPoolName = "$($VMScaleSetName)BackEnd",
        [Parameter(Mandatory = $true)]
        [string]
        $VirtualNetworkName,
        [Parameter(Mandatory = $false)]
        [string]
        $VirtualNetworkResourceGroupName = $ResourceGroupName,        
        [Parameter(Mandatory = $true)]
        [string]
        $SubnetName,
        [Parameter(Mandatory = $true)]
        [string]
        $SubnetAddressPrefix = "10.0.1.0/24",
        [Parameter(Mandatory = $true)]
        [string]
        $PrivateIpAddress,
        [Parameter(Mandatory = $false)]
        [ValidateSet('IPv4', 'IPv6')]
        [string]
        $PrivateIpAddressVersion = 'IPv4',
        [Parameter(Mandatory = $true)]
        [string]
        $PublicIpAddressName,
        [Parameter(Mandatory = $true)]
        [string]
        $PublicIpAddressSku = "Standard",
        [Parameter(Mandatory = $false)]
        [ValidateSet('Dynamic', 'Static')]
        [string]
        $PublicIpAddressAllocation = "Static",
        [Parameter(Mandatory = $false)]
        [string]
        $DomainNameLabel,
        [Parameter(Mandatory = $false)]
        [string]
        $ApplicationGatewaySkuName = "Standard_v2",
        [Parameter(Mandatory = $false)]
        [string]
        $ApplicationGatewaySkuTier = "Standard_v2",
        [Parameter(Mandatory = $false)]
        [int]
        $ApplicationGatewaySkuCapacity = 2,
        [Parameter(Mandatory = $false)]
        [bool]
        $UpdateScaleSet = $true,
        [Parameter(Mandatory = $false)]
        [string]
        $StorageAccountResourceGroup = $ResourceGroupName,
        [Parameter(Mandatory = $true)]
        [string]
        $StorageAccountName,
        [Parameter(Mandatory = $true)]
        [string]
        $TableNameEnvironments,
        [Parameter(Mandatory = $true)]
        [string]
        $EnvironmentTypeFilter,
        [Parameter(Mandatory = $false)]
        [string]
        $KeyVaultResourceGroup = $ResourceGroupName,
        [Parameter(Mandatory = $false)]
        [string]
        $KeyVaultName,
        [Parameter(Mandatory = $false)]
        [string]
        $CertificateName = "ApplicationGateway",
        [HashTable]
        $Tags
    )
    process {        
        if (Get-AzApplicationGateway -ResourceGroupName $ResourceGroupName -Name $ApplicationGatewayName -ErrorAction SilentlyContinue) {
            Write-Verbose "Application Gateway $ApplicationGatewayName already exists."
            return
        }

        # Get environments from Storage; needed to create correct HttpSettings and Probes for Webclients
        $storageAccount = Get-AzStorageAccount -ResourceGroupName $StorageAccountResourceGroup -Name $StorageAccountName
        $storageAccountContext = $storageAccount.Context
        $environments = Get-EnvironmentsFromStorage -StorageAccountContext $storageAccountContext -TableNameEnvironments $TableNameEnvironments -TypeFilter $EnvironmentTypeFilter -EnvironmentsOnly -Verbose:$Verbose

        $params = @{
            ResourceGroupName      = $KeyVaultResourceGroup
            ResourceLocation       = $ResourceLocation
            ApplicationGatewayName = $ApplicationGatewayName
            KeyVaultName           = $KeyVaultName
            CertificateName        = $CertificateName
        }
        $SslSetup = Get-ApplicationGatewaySslSetupAndIdentity @params

        Write-Verbose "Setting up Application Gateway-configuration for $ApplicationGatewayName..."
        $params = @{
            VirtualNetworkResourceGroupName = $VirtualNetworkResourceGroupName
            VirtualNetworkName              = $VirtualNetworkName
            SubnetName                      = $SubnetName
            SubnetAddressPrefix             = $SubnetAddressPrefix
        }
        $NetworkSetup = New-ApplicationGatewayNetworkSetup @params -IncludeSsl:$SslSetup.HandleSSL -Verbose:$Verbose

        Write-Verbose "Adding BackendAddressPool..."
        $Pool = New-AzApplicationGatewayBackendAddressPool -Name $BackendPoolName

        $Probes = New-ApplicationGatewayProbeConfigs -Environments $environments -IncludeSsl:$SslSetup.HandleSSL -Verbose:$Verbose
        $HttpSettings = New-ApplicationGatewayHttpSettings  -Environments $environments -Probes $Probes -IncludeSsl:$SslSetup.HandleSSL

        # Create FrontendPorts
        $FrontEndPorts = New-ApplicationGatewayFrontendPorts -IncludeSsl:$SslSetup.HandleSSL -Verbose:$Verbose

        # Create FrontendIPConfigurations (incl. PublicIP if necessary)
        $params = @{
            ResourceGroupName           = $ResourceGroupName 
            ResourceLocation            = $ResourceLocation
            FrontEndIpConfigNamePrivate = $FrontEndIpConfigNamePrivate
            FrontEndIpConfigNamePublic  = $FrontEndIpConfigNamePublic
            PublicIpAddressName         = $PublicIpAddressName
            PublicIpAddressSku          = $PublicIpAddressSku
            PublicIpAddressAllocation   = $PublicIpAddressAllocation
            PrivateIpAddress            = $PrivateIpAddress
            PrivateIpAddressVersion     = $PrivateIpAddressVersion
            Subnet                      = $NetworkSetup.Subnet
        }
        $IpConfiguration = New-ApplicationGatewayIpConfigurations @params -Verbose:$Verbose

        # Create HttpListeners
        $params = @{
            IpConfiguration = $IpConfiguration
            FrontendPorts   = $FrontEndPorts            
        }
        if ($SslSetup.HandleSSL) {
            $params.Add("SslCertificate", $SslSetup.Certificate)
        }        
        $Listeners = New-ApplicationGatewayHttpListeners @params -IncludeSsl:$SslSetup.HandleSSL -Verbose:$Verbose

        # Create RoutingRules
        $Rules = New-ApplicationGatewayRoutingRules -HttpSettings $HttpSettings -Listeners $Listeners -BackendAddressPool $Pool -IncludeSsl:$SslSetup.HandleSSL

        # Create Sku
        $Sku = New-AzApplicationGatewaySku -Name $ApplicationGatewaySkuName -Tier $ApplicationGatewaySkuTier -Capacity $ApplicationGatewaySkuCapacity

        # Create Application Gateway
        $params = @{
            Name                          = $ApplicationGatewayName
            ResourceGroupName             = $ResourceGroupName
            Location                      = $ResourceLocation
            BackendAddressPools           = $Pool
            BackendHttpSettingsCollection = $HttpSettings.Collection
            FrontendIpConfigurations      = $IpConfiguration.Collection
            FrontendPorts                 = $FrontEndPorts.Collection
            Probes                        = $Probes.Collection
            GatewayIpConfigurations       = $NetworkSetup.GatewayIPConfiguration
            HttpListeners                 = $Listeners.Collection
            RequestRoutingRules           = $Rules.Collection
            Sku                           = $Sku
        }
        if ($SslSetup.Identity) {
            $params.Add("Identity", $SslSetup.Identity)
        }
        if ($SslSetup.Certificate) {
            $params.Add("SslCertificates", $SslSetup.Certificate)
        }
        if ($Rules.RedirectConfiguration) {
            $params.Add("RedirectConfigurations", $Rules.RedirectConfiguration)
        }
        Write-Verbose "Creating ApplicationGateway..."
        $appGateway = New-AzApplicationGateway @params
        Write-Verbose "Done."

        Set-TagsOnResource -ResourceGroupName $ResourceGroupName -ResourceName $ApplicationGatewayName -Tags $Tags

        # Setup association of gateway with Scale Set
        Set-ApplicationGatewayAssociationForScaleSet -ResourceGroupName $ResourceGroupName -ApplicationGatewayName $ApplicationGatewayName -BackendPoolName $BackendPoolName -ScaleSetName $VMScaleSetName
    }    
}