chrisluiz16

Untitled

May 12th, 2023
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.69 KB | None | 0 0
  1. provider "aws" {
  2.   region = var.region
  3. }
  4.  
  5. terraform {
  6.   backend "s3" {
  7.     bucket = "terraform-bucket-iampolicy"
  8.     key    = "terraform.tfstate"
  9.     region = "ap-south-1"
  10.   }
  11. }
  12.  
  13. # Bucket creation [Error happend ran the terraform init again]
  14.  
  15. resource "aws_s3_bucket" "test" {
  16.   bucket        = "terraform-bucket-iampolicy"
  17.   force_destroy = true
  18. #  versioning {
  19. # enabled = true
  20. #}
  21.   tags = {
  22.     Name        = "terraform-bucket-iampolicy"
  23.     Environment = "Dev"
  24.   }
  25. }
  26.  
  27.  
  28. # Bucket versioning enabled
  29.  
  30. resource "aws_s3_bucket_versioning" "version" {
  31.   bucket = aws_s3_bucket.test.id
  32.  
  33.   versioning_configuration {
  34.     status = "Enabled"
  35.   }
  36. }
  37.  
  38. # Bucket policy Creation
  39.  
  40. data "aws_iam_policy_document" "iam" {
  41.  
  42.   statement {
  43.     principals {
  44.       type        = "*"
  45.       identifiers = ["*"]
  46.     }
  47.     actions = [
  48.       "s3:GetObject",
  49.       "s3:PutObject",
  50.       "s3:ListBucket",
  51.     ]
  52.     resources = [
  53.       "${aws_s3_bucket.test.arn}",
  54.       "${aws_s3_bucket.test.arn}/*",
  55.     ]
  56.   }
  57. }
  58.  
  59.  
  60. #  Block public acess for the bucket
  61.  
  62. resource "aws_s3_bucket_public_access_block" "false" {
  63.   bucket = aws_s3_bucket.test.id
  64.  
  65.   block_public_acls       = false
  66.   block_public_policy     = false
  67.   ignore_public_acls      = false
  68.   restrict_public_buckets = false
  69. }
  70.  
  71.  
  72. # Bucket Policy attach
  73.  
  74. resource "aws_s3_bucket_policy" "attach" {
  75.   bucket = aws_s3_bucket.test.id
  76.   policy = data.aws_iam_policy_document.iam.json
  77. }
  78.  
  79. # Copying one files to the object
  80.  
  81. resource "aws_s3_bucket_object" "index" {
  82.   bucket = aws_s3_bucket.test.id
  83.   key    = "index.hmtl"
  84.   source = "/home/ec2-user/index.html"
  85.   content_type = "text/html"
  86. }
  87.  
  88. # Copying multiple files to the object
  89.  
  90. resource "aws_s3_bucket_object" "multiple" {
  91. for_each = fileset("/home/ec2-user","**/*.html")
  92. bucket = aws_s3_bucket.test.id
  93.   key    = each.key
  94.   source = "/home/ec2-user/${each.key}"
  95.   content_type = "text/html"
  96.  etag = filemd5("/home/ec2-user/${each.key}")
  97. }
  98.  
  99.  
  100. ####### Teraform Output #########
  101.  
  102. [ec2-user@ip-172-31-32-32 s3-bucket]$ terraform output
  103. url_chris_html = "http://terraform-bucket-iampolicy.s3.amazonaws.com/chris.html"
  104. url_index_html = "http://terraform-bucket-iampolicy.s3.amazonaws.com/index.html"
  105.  
  106.  
  107. ###### Terraform state list ###########
  108. [ec2-user@ip-172-31-32-32 s3-bucket]$ terraform state list
  109. data.aws_iam_policy_document.iam
  110. aws_s3_bucket.test
  111. aws_s3_bucket_object.index
  112. aws_s3_bucket_object.multiple["ashna.html"]
  113. aws_s3_bucket_object.multiple["chris.html"]
  114. aws_s3_bucket_object.multiple["index.html"]
  115. aws_s3_bucket_object.multiple["s3-bucket/index.html"]
  116. aws_s3_bucket_policy.attach
  117. aws_s3_bucket_public_access_block.false
  118. aws_s3_bucket_versioning.version
Advertisement
Add Comment
Please, Sign In to add comment