MelleD
MelleD

Reputation: 791

Add redirect rule for Azure Appplication Gateway with terraform

how can i add a redirect rule for the azurerm_application_gateway? Over the azure portal there is a checkbox "redirect configure", but i didn't find an terraform element therefore.

Upvotes: 1

Views: 3419

Answers (2)

Miguel Zablah
Miguel Zablah

Reputation: 55

So this is now possible from terraform you can use the redirect_configuration block here are the docs: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/application_gateway#redirect_configuration

Here is a list of things you will need to add azurerm_application_gateway resource:

  • 2 frontend_port (One for 80 and 443)
  • 2 http_listener (One for Http and Https)
  • 2 request_routing_rule (One for Http and Https)
  • 1 redirect_configuration (To redirect from http to https)

Note: If you do not set host_name on the http_listener you will have to create multiple frontend_port blocks.

It will look something like this:

resource "azurerm_application_gateway" "example" {
  ...

  # Https Port
  frontend_port {
    name = "port-https"
    port = 443
  }

  # Http Port
  frontend_port {
    name = "port-http"
    port = 80
  }

  # Https Listener
  http_listener {
    name                           = "https-listener"
    frontend_ip_configuration_name = "<frontend_ip_configuration_name>"
    frontend_port_name             = "port-https"
    host_name                      = "<host_name>"
    protocol                       = "Https"
    require_sni                    = true
    ssl_certificate_name           = "<ssl_certificate_name>"
  }

  # Http Listener
   http_listener {
    name                           = "http-listener"
    frontend_ip_configuration_name = "<frontend_ip_configuration_name>"
    frontend_port_name             = "port-http"
    host_name                      = "<host_name>"
    protocol                       = "Http"
  }

  # Request routing rule for Https
  request_routing_rule {
    name                       = "routing-https"
    http_listener_name         = "https-listener"
    backend_address_pool_name  = "<backend_address_pool_name>"
    backend_http_settings_name = "<backend_http_settings_name>"
    rule_type                  = "Basic"
  }

  # Request routing rule for Http
  request_routing_rule {
    name                        = "routing-http"
    http_listener_name          = "http-listener"
    redirect_configuration_name = "redirect-config-https"
    rule_type                   = "Basic"
  }

  # Request Configuration for Https
  redirect_configuration {
    name                 = "redirect-config-https"
    target_listener_name = "https-listener"
    redirect_type        = "Permanent"
    include_path         = true
    include_query_string = true
  }

...
}

Upvotes: 2

4c74356b41
4c74356b41

Reputation: 72191

according to this template there should be a redirect rule property which is missing in terraform. so my guess is - you cant.

thanks to a link posted in the comments here's an issue you should track: https://github.com/terraform-providers/terraform-provider-azurerm/issues/1576

Upvotes: 0

Related Questions