terraform.tf 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Terraform, compulsary.
  2. terraform {
  3. required_providers {
  4. aws = {
  5. source = "hashicorp/aws"
  6. version = "~> 4.16.0"
  7. }
  8. }
  9. required_version = ">= 0.15"
  10. }
  11. # Providers!
  12. # S3 Bucket in Ireland
  13. provider "aws" {
  14. region = "eu-west-1"
  15. }
  16. # Global for Certificates
  17. provider "aws" {
  18. region = "us-east-1"
  19. alias = "global"
  20. }
  21. # Variables
  22. variable "domainname" {
  23. type = string
  24. default = "blog.ligthert.net"
  25. }
  26. #
  27. # <S3
  28. #
  29. # Create a bucket
  30. resource "aws_s3_bucket" "bucket" {
  31. bucket = var.domainname
  32. force_destroy = true
  33. }
  34. # Place ACL on the bucket
  35. resource "aws_s3_bucket_acl" "bucket_acl" {
  36. bucket = aws_s3_bucket.bucket.id
  37. acl = "public-read"
  38. }
  39. # Enable the static file hosting
  40. resource "aws_s3_bucket_website_configuration" "bucket_website" {
  41. bucket = aws_s3_bucket.bucket.bucket
  42. index_document {
  43. suffix = "index.html"
  44. }
  45. error_document {
  46. key = "error.html"
  47. }
  48. }
  49. # Give bucket a policy
  50. resource "aws_s3_bucket_policy" "bucket_policy" {
  51. bucket = aws_s3_bucket.bucket.id
  52. policy = data.aws_iam_policy_document.bucket_policy_document.json
  53. }
  54. # Create a policy document
  55. data "aws_iam_policy_document" "bucket_policy_document" {
  56. statement {
  57. sid = "PublicReadGetObject"
  58. effect = "Allow"
  59. principals {
  60. type = "AWS"
  61. identifiers = ["*"]
  62. }
  63. actions = ["s3:GetObject"]
  64. resources = [
  65. aws_s3_bucket.bucket.arn,
  66. "${aws_s3_bucket.bucket.arn}/*",
  67. ]
  68. }
  69. }
  70. #
  71. # S3>
  72. #
  73. #
  74. # <CloudFront
  75. #
  76. locals {
  77. s3_origin_id = "S3Origin"
  78. }
  79. resource "aws_cloudfront_origin_access_identity" "dafaim" {
  80. comment = "Manage access and identities."
  81. }
  82. resource "aws_cloudfront_distribution" "s3_distribution" {
  83. aliases = ["${var.domainname}"]
  84. comment = "A CloudFront distribution"
  85. default_cache_behavior {
  86. allowed_methods = ["GET", "HEAD", "OPTIONS"]
  87. cached_methods = ["GET", "HEAD", "OPTIONS"]
  88. compress = true
  89. default_ttl = 3600
  90. forwarded_values {
  91. query_string = false
  92. cookies {
  93. forward = "none"
  94. }
  95. }
  96. max_ttl = 86400
  97. target_origin_id = local.s3_origin_id
  98. viewer_protocol_policy = "https-only"
  99. }
  100. default_root_object = "index.html"
  101. enabled = true
  102. is_ipv6_enabled = true
  103. ordered_cache_behavior {
  104. path_pattern = "/*"
  105. allowed_methods = ["GET", "HEAD", "OPTIONS"]
  106. cached_methods = ["GET", "HEAD", "OPTIONS"]
  107. target_origin_id = local.s3_origin_id
  108. forwarded_values {
  109. query_string = false
  110. headers = ["Origin"]
  111. cookies {
  112. forward = "none"
  113. }
  114. }
  115. default_ttl = 86400
  116. max_ttl = 31536000
  117. compress = true
  118. viewer_protocol_policy = "https-only"
  119. }
  120. origin {
  121. domain_name = aws_s3_bucket.bucket.bucket_regional_domain_name
  122. origin_id = local.s3_origin_id
  123. s3_origin_config {
  124. origin_access_identity = aws_cloudfront_origin_access_identity.dafaim.cloudfront_access_identity_path
  125. }
  126. }
  127. restrictions {
  128. geo_restriction {
  129. restriction_type = "none"
  130. }
  131. }
  132. price_class = "PriceClass_All"
  133. viewer_certificate {
  134. acm_certificate_arn = "arn:aws:acm:us-east-1:131289899509:certificate/affaa360-86e8-40e9-b008-e691e55646c8"
  135. cloudfront_default_certificate = false
  136. ssl_support_method = "sni-only"
  137. }
  138. }
  139. #
  140. # CloudFront>
  141. #