Skip to content

Instantly share code, notes, and snippets.

@iamtheindian
Created June 13, 2020 13:00
Show Gist options
  • Save iamtheindian/f043c77fb0a76df6ba19d36f24d7f365 to your computer and use it in GitHub Desktop.
Save iamtheindian/f043c77fb0a76df6ba19d36f24d7f365 to your computer and use it in GitHub Desktop.
#setting up cloudfron env
resource "aws_cloudfront_origin_access_identity" "origin_access_identity" {
comment = "myterra-access-generated"
}
locals {
s3_origin_id = "myS3Origin"
}
resource "aws_cloudfront_distribution" "s3_distribution" {
depends_on = [aws_s3_bucket.b]
origin {
domain_name = "${aws_s3_bucket.b.bucket_regional_domain_name}"
origin_id = "${local.s3_origin_id}"
s3_origin_config {
origin_access_identity = "${aws_cloudfront_origin_access_identity.origin_access_identity.cloudfront_access_identity_path}"
}
}
enabled = true
is_ipv6_enabled = true
comment = "creating"
default_root_object = "base.html"
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "${local.s3_origin_id}"
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 86400
max_ttl = 31536000
}
restrictions {
geo_restriction {
restriction_type = "blacklist"
locations = ["US", "CA", "GB", "DE"]
}
}
price_class = "PriceClass_All"
tags = {
Environment = "production"
}
viewer_certificate {
cloudfront_default_certificate = true
}
}
#saving cloudfront_domain_name in our local file
resource "local_file" "cloud_dist_domain" {
depends_on = [aws_cloudfront_distribution.s3_distribution]
content = aws_cloudfront_distribution.s3_distribution.domain_name
filename = "/root/HybridCloud/Terraform/php/domain_name.txt"
}
#updating bucket policy
data "aws_iam_policy_document" "s3_policy" {
depends_on = [aws_cloudfront_distribution.s3_distribution]
statement {
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.b.arn}/*"]
principals {
type = "AWS"
identifiers = ["${aws_cloudfront_origin_access_identity.origin_access_identity.iam_arn}"]
}
}
statement {
actions = ["s3:ListBucket"]
resources = ["${aws_s3_bucket.b.arn}"]
principals {
type = "AWS"
identifiers = ["${aws_cloudfront_origin_access_identity.origin_access_identity.iam_arn}"]
}
}
}
resource "aws_s3_bucket_policy" "s3_bucket_pol" {
depends_on = [aws_iam_policy_document.s3_policy]
bucket = "${aws_s3_bucket.b.id}"
policy = "${data.aws_iam_policy_document.s3_policy.json}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment