chrisluiz16

Untitled

May 13th, 2023
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.43 KB | None | 0 0
  1. ################### Main.tf  ################
  2.  
  3.  
  4. provider "aws" {
  5.   region = var.region
  6.   access_key = var.access_key
  7.   secret_key = var.secret_key
  8. }
  9.  
  10. terraform {
  11.   backend "s3" {
  12.     bucket = "bucket-shopping"
  13.     key    = "dev/terraform.tfstate"
  14.     region = "ap-south-1"
  15.     dynamodb_table = "bucket-shopping"
  16.   }
  17. }
  18.  
  19. # Bucket creation
  20.  
  21. resource "aws_s3_bucket" "test" {
  22.   bucket        = "bucket-shopping"
  23.   force_destroy = true
  24.   #  versioning {
  25.   # enabled = true
  26.   #}
  27.   tags = {
  28.     Name        = "bucket-shopping"
  29.     Environment = "Dev"
  30.   }
  31. }
  32.  
  33.  
  34. # Bucket versioning enabled
  35.  
  36. resource "aws_s3_bucket_versioning" "version" {
  37.   bucket = aws_s3_bucket.test.id
  38.  
  39.   versioning_configuration {
  40.     status = "Enabled"
  41.   }
  42. }
  43.  
  44. # Bucket policy Creation
  45.  
  46. data "aws_iam_policy_document" "iam" {
  47.  
  48.   statement {
  49.     principals {
  50.       type        = "*"
  51.       identifiers = ["*"]
  52.     }
  53.     actions = [
  54.       "s3:GetObject",
  55.       "s3:PutObject",
  56.       "s3:ListBucket",
  57.     ]
  58.     resources = [
  59.       "${aws_s3_bucket.test.arn}",
  60.       "${aws_s3_bucket.test.arn}/*",
  61.     ]
  62.   }
  63. }
  64.  
  65.  
  66. #  Block public acess for the bucket
  67.  
  68. resource "aws_s3_bucket_public_access_block" "false" {
  69.   bucket = aws_s3_bucket.test.id
  70.  
  71.   block_public_acls       = false
  72.   block_public_policy     = false
  73.   ignore_public_acls      = false
  74.   restrict_public_buckets = false
  75. }
  76.  
  77.  
  78. # Bucket Policy attach
  79.  
  80. resource "aws_s3_bucket_policy" "attach" {
  81.   bucket = aws_s3_bucket.test.id
  82.   policy = data.aws_iam_policy_document.iam.json
  83. }
  84.  
  85. # Copying one files to the object
  86.  
  87. resource "aws_s3_bucket_object" "index" {
  88.   bucket       = aws_s3_bucket.test.id
  89.   key          = "index.html"
  90.   source       = "/home/ec2-user/index.html"
  91.   content_type = "text/html"
  92. }
  93.  
  94. # Copying multiple files to the object
  95.  
  96. #resource "aws_s3_bucket_object" "multiple" {
  97. #  for_each     = fileset("/home/ec2-user", "**/*.html")
  98. #  bucket       = aws_s3_bucket.test.id
  99. #  key          = each.key
  100. #  source       = "/home/ec2-user/${each.key}"
  101. #  content_type = "text/html"
  102. #  etag         = filemd5("/home/ec2-user/${each.key}")
  103. #}
  104.  
  105.  
  106. # Copying a template
  107.  
  108. locals {
  109.   mime_types = {
  110.     "css"   = "text/css"
  111.     "html"  = "text/html"
  112.     "ico"   = "image/vnd.microsoft.icon"
  113.     "js"    = "application/javascript"
  114.     "json"  = "application/json"
  115.     "map"   = "application/json"
  116.     "png"   = "image/png"
  117.     "svg"   = "image/svg+xml"
  118.     "txt"   = "text/plain"
  119.     "woff"  = "application/font-woff"
  120.     "woff2" = "application/font-woff2"
  121.     "jpg"   = "image/jpeg"
  122.   }
  123. }
  124.  
  125. resource "aws_s3_bucket_object" "content" {
  126.   for_each = fileset("/home/ec2-user/chrisich.fun", "**/*.*")
  127.  
  128.   bucket       = aws_s3_bucket.test.id
  129.   key          = each.key
  130.   source       = "/home/ec2-user/chrisich.fun/${each.key}"
  131.   content_type = lookup(tomap(local.mime_types), element(split(".", each.key), length(split(".", each.key)) - 1))
  132.   etag         = filemd5("/home/ec2-user/chrisich.fun/${each.key}")
  133. }
  134.  
  135.  
  136. resource "aws_s3_bucket_website_configuration" "site" {
  137.   bucket = aws_s3_bucket.test.id
  138.   index_document {
  139.     suffix = "index.html"
  140.   }
  141. }
  142.  
  143. ############### Output.tf ###########
  144. [ec2-user@ip-172-31-32-32 s3]$ cat output.tf
  145. output "bucket_url" {
  146. value = "http://${aws_s3_bucket_website_configuration.site.website_endpoint}"
  147. }
  148.  
  149.  
  150. [ec2-user@ip-172-31-32-32 s3]$ terraform output
  151. bucket_url = "http://bucket-shopping.s3-website.ap-south-1.amazonaws.com"
  152.  
  153.  
Advertisement
Add Comment
Please, Sign In to add comment