# Terraform-Cloudflare-Maintenance

Terraform module to create a responsive **Maintenance Page** using
Cloudflare Workers.

## Usage

Export cloudflare credentials

```bash
TF_VAR_cloudflare_email=xxx
TF_VAR_cloudflare_api_key=xxx
TF_VAR_cloudflare_account_id=xxx
```

If using a token, make sure it has all the necessary permissions

Simple maintenance page with your logo, fav icon, company name, font and email with multiple routes on the same domain:

```terraform
module "theorchard_maintenance" {
  source          = "git::git@github.com:theorchard/terraform-cloudflare-maintenance.git?ref=v1.0.0"
  cloudflare_zone = "theorchard.com"
  patterns        = ["theorchard.com/*", "www.theorchard.com/*"]
}
```

Use the enabled flag to enable/disable the Cloudflare route when
maintenance starts/ends:

```terraform
module "theorchard_maintenance" {
  source          = "git::git@github.com:theorchard/terraform-cloudflare-maintenance.git?ref=v1.0.0"
  enabled         = false
  cloudflare_zone = "theorchard.com"
  patterns        = ["theorchard.com/*", "www.theorchard.com/*"]
}
```

Example can be found in examples/root-example.

<!-- BEGIN_TF_DOCS -->

## Requirements

| Name       | Version |
| ---------- | ------- |
| terraform  | >= 0.13 |
| cloudflare | > 2.0.0 |

## Providers

| Name       | Version |
| ---------- | ------- |
| cloudflare | > 2.0.0 |

## Resources

| Name                                                                                                                               | Type        |
| ---------------------------------------------------------------------------------------------------------------------------------- | ----------- |
| [cloudflare_worker_route.this](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/worker_route)   | resource    |
| [cloudflare_worker_script.this](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/worker_script) | resource    |
| [cloudflare_zones.this](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/data-sources/zones)              | data source |

## Inputs

| Name            | Description                                                                                  | Type           | Default  | Required |
| --------------- | -------------------------------------------------------------------------------------------- | -------------- | -------- | :------: |
| cloudflare_zone | n/a                                                                                          | `string`       | n/a      |   yes    |
| patterns        | The DNS pattern list to deploy the maintenance page to.                                      | `list(string)` | n/a      |   yes    |
| allowed_ips     | The IPs that are allowed to bypass the maintenance page.                                     | `string`       | `"null"` |    no    |
| allowed_path    | The paths that are allowed (defined with a regex expression) to bypass the maintenance page. | `string`       | `"null"` |    no    |

<!-- END_TF_DOCS -->

## Running the maintenance page locally

```shell
# use the right node version
nvm use

# install dependencies
npm install

# start dev server on 8080
npm run start
```

To show the maintenance page in the different brands, use the `domain` query parameter.
e.g http://localhost:8080?domain=awal.com

## Notes

A detailed explanation of [the original implementation can be found here.](https://hodovi.cc/blog/quick-pretty-and-easy-maintenance-page-using-cloudflare-workers-terraform/)
