Skip to content

Instantly share code, notes, and snippets.

@Pelirrojo
Last active June 7, 2024 21:01
Show Gist options
  • Save Pelirrojo/b0d7db761758100f4b98e020c1f3e4f5 to your computer and use it in GitHub Desktop.
Save Pelirrojo/b0d7db761758100f4b98e020c1f3e4f5 to your computer and use it in GitHub Desktop.
Snippets for the blog entry - How to Create a Sustainable Business Card with AWS CloudFront
provider "aws" {
region = "us-east-1"
}
locals {
s3_origin_id = "S3-business-card"
}
variable "zone_id" {
type = string
}
resource "aws_s3_bucket" "business_card" {
bucket = "my-business-card-bucket"
tags = {
Name = "My business card bucket"
}
}
resource "aws_s3_bucket_acl" "business_card_acl" {
bucket = aws_s3_bucket.business_card.id
acl = "private"
}
resource "aws_s3_object" "vcf_file" {
bucket = aws_s3_bucket.business_card.bucket
key = "john_doe.vcf"
source = "path/to/john_doe.vcf"
acl = "private"
}
resource "aws_cloudfront_origin_access_control" "default" {
name = "example-oac"
#origin_type = "s3"
signing_behavior = "always"
signing_protocol = "sigv4"
origin_access_control_origin_type = "s3"
}
resource "aws_cloudfront_distribution" "business_card" {
origin {
domain_name = aws_s3_bucket.business_card.bucket_regional_domain_name
origin_access_control_id = aws_cloudfront_origin_access_control.default.id
origin_id = local.s3_origin_id
}
enabled = true
is_ipv6_enabled = true
default_root_object = "index.html"
default_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
target_origin_id = local.s3_origin_id
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "allow-all"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
ordered_cache_behavior {
path_pattern = "/content/*"
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD"]
target_origin_id = local.s3_origin_id
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
compress = true
viewer_protocol_policy = "redirect-to-https"
}
price_class = "PriceClass_200"
restrictions {
geo_restriction {
restriction_type = "none"
locations = []
}
}
viewer_certificate {
cloudfront_default_certificate = true
}
}
resource "aws_route53_record" "business_card" {
zone_id = var.zone_id
name = "business-card.example.com"
type = "A"
alias {
name = aws_cloudfront_distribution.business_card.domain_name
zone_id = aws_cloudfront_distribution.business_card.hosted_zone_id
evaluate_target_health = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment