Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Green committed Jan 16, 2022
1 parent 19f27e0 commit 0d9d483
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ override.tf.json

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# macOS
.DS_Store
63 changes: 61 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,62 @@
# terraform-google-domain-redirect
# Terraform Module: Google Cloud DNS Domain Redirect

Terraform module to simplify domain redirect on Google Cloud DNS.
A Terraform module for [Google Cloud Platform](https://cloud.google.com) that simplifies the creation & configuration domain redirect for [Cloud DNS](https://cloud.google.com/dns/).

## Table of contents

* [Introduction](#introduction)
* [Requirements](#requirements)
* [Usage](#usage)
* [Inputs](#inputs)
* [Required](#required-inputs)
* [Outputs](#outputs)
* [Changelog](#changelog)
* [Roadmap](#roadmap)

## Introduction

Google Cloud DNS does not have built-in support for 30x redirects. There has been an open [feature request](https://issuetracker.google.com/issues/70980380?pli=1) since 2017 with no ETA. This module provides this functionality through the use of a Cloud Storage bucket, a managed certificate, a global IP address, and url mappings.

## Requirements

* Terraform 0.14 or higher.
* `google` provider 3.67.0 or higher.
* `google-beta` provider 3.67.0 or higher.

## Usage

```hcl-terraform
module "www_example" {
source = "simplycubed/domain-redirect/google"
hostname = "www.${var.base_domain}"
host_redirect = var.base_domain
}
```

## Inputs

### Required inputs

| Name | Description | Type |
|--------------- |-------------|--------|
| hostname | The name of the website and the Cloud Storage bucket to create (e.g. static.foo.com). | string |
| host_redirect | The name of the target domain to redirect | string |
| https_redirect | Issue TLS certificate and enable HTTPS | bool |
| strip_query | Strip URL query parameters | bool |

## Outputs

In addition to the inputs documented above, the following values are available as outputs:

| Name | Description | Type |
|------|-------------|------|
| load_balancer_ip_address | IP address of the HTTP Cloud Load Balancer. | string |

## Changelog

* **1.0.0**
* Release stable version.

## Roadmap

Issues containing possible future enhancements can be found [here](https://github.com/simplycubed/terraform-google-domain-redirect/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement).
62 changes: 62 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
locals {
safe_hostname = replace(var.hostname, ".", "-")
}

resource "google_compute_url_map" "https_url_map" {
name = "${local.safe_hostname}-https-url-map"

default_url_redirect {
host_redirect = var.host_redirect
strip_query = var.strip_query
}
}

resource "google_compute_managed_ssl_certificate" "certificate" {
provider = google-beta
name = "${local.safe_hostname}-managed-certificate"

managed {
domains = [var.hostname]
}
}

resource "google_compute_target_https_proxy" "https_proxy" {
name = "${local.safe_hostname}-https-proxy"
url_map = google_compute_url_map.https_url_map.self_link
ssl_certificates = [google_compute_managed_ssl_certificate.certificate.self_link]
}

resource "google_compute_url_map" "http_url_map" {
name = "${local.safe_hostname}-to-http-url-map"

default_url_redirect {
host_redirect = var.host_redirect
https_redirect = var.https_redirect
strip_query = var.strip_query
}
}

resource "google_compute_target_http_proxy" "http_proxy" {
name = "${local.safe_hostname}-http-proxy"
url_map = google_compute_url_map.http_url_map.self_link
}

resource "google_compute_global_address" "public_address" {
name = "${local.safe_hostname}-public-address"
ip_version = "IPV4"
address_type = "EXTERNAL"
}

resource "google_compute_global_forwarding_rule" "global_forwarding_https_rule" {
name = "${local.safe_hostname}-global-forwarding-https-rule"
target = google_compute_target_https_proxy.https_proxy.self_link
ip_address = google_compute_global_address.public_address.address
port_range = "443"
}

resource "google_compute_global_forwarding_rule" "global_forwarding_http_rule" {
name = "${local.safe_hostname}-global-forwarding-http-rule"
target = google_compute_target_http_proxy.http_proxy.self_link
ip_address = google_compute_global_address.public_address.address
port_range = "80"
}
4 changes: 4 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "load_balancer_ip_address" {
description = "IP address of the HTTP Cloud Load Balancer"
value = google_compute_global_address.public_address.address
}
21 changes: 21 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
variable "hostname" {
description = "The name of the website and the Cloud Storage bucket to create (e.g. static.foo.com)."
type = string
}

variable "host_redirect" {
description = "The name of the target domain to redirect"
type = string
}

variable "https_redirect" {
description = "Issue TLS certificate and enable HTTPS"
type = bool
default = true
}

variable "strip_query" {
description = "Strip URL query parameters"
type = bool
default = false
}

0 comments on commit 0d9d483

Please sign in to comment.