Skip to content

Commit

Permalink
[fix] aws-single-page-static site to work outside us-east-1 (#280)
Browse files Browse the repository at this point in the history
Allow this module to work across regions by configuring providers specifically for us-east-1 where needed.

Also fix the tests to work.

### Test Plan
* tests work now
  • Loading branch information
ryanking authored Feb 1, 2021
1 parent ecdcc6f commit 9be9388
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 46 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.14.3
go-version: 1.15.5
- run: make check-mod
lint:
name: lint
Expand All @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v2
- uses: hashicorp/setup-terraform@v1
with:
terraform_version: 0.12.24
terraform_version: 0.12.30
terraform_wrapper: "false"
- name: setup
run: make setup
Expand All @@ -37,7 +37,7 @@ jobs:
- if: github.event == 'push' || steps.filter.outputs.module == 'true'
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 0.12.28
terraform_version: 0.12.30
terraform_wrapper: "false"
- if: github.event == 'push' || steps.filter.outputs.module == 'true'
uses: actions/setup-go@v2
Expand Down
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ check-mod:
.PHONY: check-mod

clean:
rm **/*.tfstate*; true
rm -rf **/.terraform; true
rm -rf **/.test-data; true
rm -rf */*.tfstate*; true
rm -rf */.terraform; true
rm -rf */.test-data; true
rm -rf */*/*.tfstate*; true
rm -rf */*/.terraform; true
rm -rf */*/.test-data; true
.PHONY: clean

test:
Expand Down
1 change: 0 additions & 1 deletion aws-s3-public-bucket/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func TestPublicBucketDefaults(t *testing.T) {
fmt.Println("Testing ", test.action, " with https enabled=", test.secureTransport)
r.Equal(test.result, *resp.EvalDecision)
}

},
}

Expand Down
4 changes: 4 additions & 0 deletions aws-single-page-static-site/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ module "security_headers_lambda" {
owner = var.owner
env = var.env
service = var.service

providers = {
aws = aws.us-east-1
}
}

resource "aws_cloudfront_distribution" "s3_distribution" {
Expand Down
58 changes: 19 additions & 39 deletions aws-single-page-static-site/module_test.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,33 @@
package test

import (
"fmt"
"testing"

"github.com/chanzuckerberg/go-misc/tftest"
"github.com/gruntwork-io/terratest/modules/terraform"
)

func TestAwsSinglePageStaticSiteInit(t *testing.T) {
options := &terraform.Options{
TerraformDir: ".",
}
terraform.Init(t, options)
}

func TestAwsSinglePageStaticSiteInitAndApply(t *testing.T) {
t.Skip("Skipping because destroy is painfully slow (>30m on average) - consider running destroy out of band")

func TestAwsSinglePageStaticSite(t *testing.T) {
t.Parallel()
project := tftest.UniqueID()
env := tftest.UniqueID()
service := tftest.UniqueID()
owner := tftest.UniqueID()

subdomain := tftest.UniqueID()
awsACMCert := tftest.EnvVar(tftest.EnvWildcardCloudfrontCertARN)
route53ZoneID := tftest.EnvVar(tftest.EnvRoute53ZoneID)

aliases := []string{fmt.Sprintf(
"%s.%s",
tftest.UniqueID(),
tftest.EnvVar(tftest.EnvRoute53ZoneName))}

options := tftest.Options(
tftest.IAMRegion, // us-east-1
map[string]interface{}{
"project": project,
"env": env,
"service": service,
"owner": owner,

"subdomain": subdomain,
"aws_acm_cert_arn": awsACMCert,
"aws_route53_zone_id": route53ZoneID,
"aliases": aliases,
test := tftest.Test{
SkipDestroy: true,
Setup: func(t *testing.T) *terraform.Options {
subdomain := tftest.UniqueID()
route53ZoneID := tftest.EnvVar(tftest.EnvRoute53ZoneID)

options := tftest.Options(
tftest.DefaultRegion, // us-east-1
map[string]interface{}{
"subdomain": subdomain,
"aws_route53_zone_id": route53ZoneID,
},
)
options.TerraformDir = "./test"
return options
},
)
Validate: func(t *testing.T, options *terraform.Options) {},
}

defer tftest.Destroy(t, options, 5)
tftest.Run(t, options)
test.Run(t)
}
6 changes: 6 additions & 0 deletions aws-single-page-static-site/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
provider aws {}

provider aws {
alias = "us-east-1"
region = "us-east-1"
}
70 changes: 70 additions & 0 deletions aws-single-page-static-site/test/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
variable project {
type = string
}
variable env {
type = string
}
variable service {
type = string
}
variable owner {
type = string
}
variable subdomain {
type = string
}
variable aws_route53_zone_id {
type = string
}

data aws_route53_zone zone {
zone_id = var.aws_route53_zone_id
}

locals {
domain = replace(data.aws_route53_zone.zone.name, "/\\.$/", "")
website_fqdn = "${var.subdomain}.${local.domain}"
aliases = [
"www.${local.website_fqdn}",
]
}

# these will be inherited in the modules
provider aws {
}

provider aws {
alias = "us-east-1"
region = "us-east-1"
}

module cert {
source = "../../aws-acm-cert"

cert_domain_name = local.website_fqdn
aws_route53_zone_id = var.aws_route53_zone_id
cert_subject_alternative_names = { for a in local.aliases : a => var.aws_route53_zone_id }
cert_subject_alternative_names_count = length(local.aliases)

project = var.project
env = var.env
service = var.service
owner = var.owner

providers = {
aws = aws.us-east-1
}
}

module site {
source = "../."

subdomain = var.subdomain
aws_acm_cert_arn = module.cert.arn
aws_route53_zone_id = var.aws_route53_zone_id

project = var.project
env = var.env
service = var.service
owner = var.owner
}

0 comments on commit 9be9388

Please sign in to comment.