Skip to content

Commit

Permalink
feat: use vars for common configs, move ouptuts to sep file
Browse files Browse the repository at this point in the history
  • Loading branch information
ekdevdes committed Jun 6, 2024
1 parent 401ab05 commit bfeb257
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 23 deletions.
4 changes: 2 additions & 2 deletions infra/certs.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
### Generating SSL certs to verify ownership and security of the ethankr.me domain
# Generating the cert for ethankr.me and *.ethankr.me
resource "aws_acm_certificate" "site_ssl_cert" {
domain_name = "ethankr.me"
subject_alternative_names = ["*.ethankr.me"]
domain_name = var.domain_name
subject_alternative_names = ["*.${var.domain_name}"]
validation_method = "DNS"

lifecycle {
Expand Down
17 changes: 4 additions & 13 deletions infra/cloudfront.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
### Setup the CloudFront distro serving the compile react app
# Setup the origin access policy for the cloudfront distro
resource "aws_cloudfront_origin_access_identity" "site_origin_access_identity" {
comment = "ethankr.me Origin Access Identity"
comment = "${var.domain_name} Origin Access Identity"
}

resource "aws_cloudfront_distribution" "site_s3_cf_distro" {
enabled = true
default_root_object = "index.html"
aliases = ["ethankr.me", "*.ethankr.me"] # This works because of the SSL cert we have from ACM listed below
aliases = [var.domain_name, "*.${var.domain_name}"] # This works because of the SSL cert we have from ACM listed below

# For now we'll only allow GET, HEAD and OPTIONS requests, we can combe back and modify this later if we want to allow more
default_cache_behavior {
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD", "OPTIONS"]
allowed_methods = var.allowed_methods_cloudfront
cached_methods = var.allowed_methods_cloudfront
target_origin_id = "${aws_s3_bucket.site_bucket.bucket}-origin"
viewer_protocol_policy = "redirect-to-https"
compress = true
Expand Down Expand Up @@ -59,12 +59,3 @@ resource "aws_cloudfront_distribution" "site_s3_cf_distro" {
# Only use CDN servers in North America and Europe since our site won't be highly accessed from other locations
price_class = "PriceClass_100"
}

# Output the final cloudfront url for informative purposes
output "cloudfront_distro_id" {
value = aws_cloudfront_distribution.site_s3_cf_distro.id
}

output "cloudfront_url" {
value = aws_cloudfront_distribution.site_s3_cf_distro.domain_name
}
13 changes: 13 additions & 0 deletions infra/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Various bits of helpful info about our infrastructure so you don't have to go hunting for it in AWS
output "cloudfront_distro_id" {
value = aws_cloudfront_distribution.site_s3_cf_distro.id
}

output "cloudfront_url" {
value = aws_cloudfront_distribution.site_s3_cf_distro.domain_name
}

# The name servers associated with our domain
output "route53_name_servers" {
value = aws_route53_zone.site_dns_zone.name_servers
}
7 changes: 1 addition & 6 deletions infra/route53.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
### Seting up the ethankr.me domain
# Setting up the top-level domain with a fully-qualified domain name
resource "aws_route53_zone" "site_dns_zone" {
name = "ethankr.me."
name = "${var.domain_name}."
}

# Setting *.ethankr.me to point to the cloudfront distro serving our compiled react app from an s3 bucket
Expand Down Expand Up @@ -29,8 +29,3 @@ resource "aws_route53_record" "site_tld" {
evaluate_target_health = false
}
}

# Output the name servers for the route53 domain name in case we need to use them to update DNS registrars
output "route53_name_servers" {
value = aws_route53_zone.site_dns_zone.name_servers
}
5 changes: 3 additions & 2 deletions infra/s3.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
### Create S3 buckets for compiled react app and the server logs
# Compiled React app bucket
resource "aws_s3_bucket" "site_bucket" {
bucket = "ethankr.me"
# Bucket is named the same thing as the domain
bucket = var.domain_name
}

# Server logs bucket
resource "aws_s3_bucket" "site_logs_bucket" {
bucket = "logs-ethankr.me"
bucket = "logs-${var.domain_name}"
}

### Setup Access Controls for compiled react app bucket
Expand Down
9 changes: 9 additions & 0 deletions infra/vars.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "domain_name" {
type = string
default = "ethankr.me"
}

variable "allowed_methods_cloudfront" {
type = list(string)
default = ["GET", "HEAD", "OPTIONS"]
}

0 comments on commit bfeb257

Please sign in to comment.