Skip to content

Instantly share code, notes, and snippets.

@mbaitelman
Created April 17, 2019 03:47
Show Gist options
  • Save mbaitelman/ace83086b154189f8f1d2b51f66eac31 to your computer and use it in GitHub Desktop.
Save mbaitelman/ace83086b154189f8f1d2b51f66eac31 to your computer and use it in GitHub Desktop.
Example data Terraform
# AWS S3 bucket for static hosting
resource "aws_s3_bucket" "website" {
bucket = "${var.website_bucket_name}"
acl = "public-read"
tags {
Name = "Website"
Environment = "production"
}
cors_rule {
allowed_headers = ["*"]
allowed_methods = ["PUT","POST"]
allowed_origins = ["*"]
expose_headers = ["ETag"]
max_age_seconds = 3000
}
policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "PublicReadForGetBucketObjects",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::${var.website_bucket_name}/*"
}
]
}
EOF
website {
index_document = "homepage.html"
error_document = "construction.html"
}
}
# AWS S3 bucket for www-redirect
resource "aws_s3_bucket" "website_redirect" {
bucket = "www.${var.website_bucket_name}"
acl = "public-read"
website {
redirect_all_requests_to = "${var.website_bucket_name}"
}
}
# AWS Cloudfront for caching
resource "aws_cloudfront_distribution" "s3_distribution" {
origin {
domain_name = "${aws_s3_bucket.website.bucket}.s3.amazonaws.com"
origin_id = "website"
}
enabled = true
is_ipv6_enabled = true
comment = "Managed by Terraform"
default_root_object = "homepage.html"
aliases = ["${var.domain_name}"]
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "website"
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "allow-all"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
price_class = "PriceClass_100"
restrictions {
geo_restriction {
restriction_type = "none"
}
}
tags {
Environment = "production"
}
viewer_certificate {
acm_certificate_arn = "${aws_acm_certificate.cert.arn}"
ssl_support_method = "sni-only"
minimum_protocol_version = "TLSv1.1_2016"
}
}
resource "aws_acm_certificate" "cert" {
domain_name = "Sitename.xyz"
validation_method = "DNS"
provider = "aws.virginia"
lifecycle {
create_before_destroy = true
}
}
resource "aws_route53_record" "cert_validation" {
name = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_name}"
type = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_type}"
zone_id = "ZONEID"
records = ["${aws_acm_certificate.cert.domain_validation_options.0.resource_record_value}"]
ttl = 60
}
resource "aws_route53_record" "main-a-record" {
zone_id = "ZONEID"
name = "${var.domain_name}"
type = "A"
alias {
name = "${aws_s3_bucket.website.website_domain}"
zone_id = "${aws_s3_bucket.website.hosted_zone_id}"
evaluate_target_health = false
}
}
resource "aws_route53_record" "main-c-name" {
zone_id = "ZONEID"
name = "www.${var.domain_name}"
type = "CNAME"
ttl = "300"
records = ["${var.domain_name}"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment