# terraform-spa

## Overview

This module provides functionality to create Cloudfront-based Single Page Applications (SPA) using a lightweight, tailored, and opinionated configuration. Projects requiring extensive routing configurations, ordered cache behaviors, or complex edge function configurations should not use this module.

## Conventions

### Brand
Unlike most modules, this requires specifying a brand variable in `var.brand`. This is primarily to distinguish between apps of the same type that are hosted and branded differently, e.g. `prod-orcd-settings` vs `prod-awal-settings`. It is also used by default to look up WAFs.

### Multiple AWS Providers
One aspect of this module is that it *requires* two AWS providers: one for the primary resources and origin S3 bucket, and one for the failover S3 bucket. Configure them in your main.tf with the appropriate regions and provide them to the module like this:

```
# This is the region of the primary bucket and most other components
provider "aws" {
  region = "us-east-1"
}

# This is the region of the failover bucket
provider "aws" {
  region = "us-west-2"
  alias  = "failover"
}

module "spa_environment" {
  source = "../../"
  providers = {
    aws.primary = aws
    aws.failover = aws.failover
  }

  environment                           = var.environment
  service_name                          = var.service_name
  application_family                    = var.application_family
  brand                                 = var.brand
}
```

Note that the providers must be named `aws.primary` and `aws.failover`, respectively.

### Cloudfront functions

Included by default is a small bit of javascript for use in the `viewer-request` Cloudfront Function. This does a uri rewrite of almost all paths to `/`, so that the request is served by the default root object. Specify your own code by setting `var.cloudfront_function_file_path` to the location of your code in the repo. 

## Resources
### The module manages the following resources:

  - Cloudfront function
  - Cloudfront origin access identity (OAI)
  - Cloudfront response headers policy
  - Cloudfront distribution
    - S3 origin group
    - Logging
    - Default cache behavior
    - Security and policy settings

### The module does not manage the following resources:

  - S3 buckets
  - DNS records
  - WAF (Web ACLs)
  - ACM certificates

  Of these, the ones provided to the module as input values are assumed to already exist.
