Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ################### Main.tf ################
- provider "aws" {
- region = var.region
- access_key = var.access_key
- secret_key = var.secret_key
- }
- terraform {
- backend "s3" {
- bucket = "bucket-shopping"
- key = "dev/terraform.tfstate"
- region = "ap-south-1"
- dynamodb_table = "bucket-shopping"
- }
- }
- # Bucket creation
- resource "aws_s3_bucket" "test" {
- bucket = "bucket-shopping"
- force_destroy = true
- # versioning {
- # enabled = true
- #}
- tags = {
- Name = "bucket-shopping"
- Environment = "Dev"
- }
- }
- # Bucket versioning enabled
- resource "aws_s3_bucket_versioning" "version" {
- bucket = aws_s3_bucket.test.id
- versioning_configuration {
- status = "Enabled"
- }
- }
- # Bucket policy Creation
- data "aws_iam_policy_document" "iam" {
- statement {
- principals {
- type = "*"
- identifiers = ["*"]
- }
- actions = [
- "s3:GetObject",
- "s3:PutObject",
- "s3:ListBucket",
- ]
- resources = [
- "${aws_s3_bucket.test.arn}",
- "${aws_s3_bucket.test.arn}/*",
- ]
- }
- }
- # Block public acess for the bucket
- resource "aws_s3_bucket_public_access_block" "false" {
- bucket = aws_s3_bucket.test.id
- block_public_acls = false
- block_public_policy = false
- ignore_public_acls = false
- restrict_public_buckets = false
- }
- # Bucket Policy attach
- resource "aws_s3_bucket_policy" "attach" {
- bucket = aws_s3_bucket.test.id
- policy = data.aws_iam_policy_document.iam.json
- }
- # Copying one files to the object
- resource "aws_s3_bucket_object" "index" {
- bucket = aws_s3_bucket.test.id
- key = "index.html"
- source = "/home/ec2-user/index.html"
- content_type = "text/html"
- }
- # Copying multiple files to the object
- #resource "aws_s3_bucket_object" "multiple" {
- # for_each = fileset("/home/ec2-user", "**/*.html")
- # bucket = aws_s3_bucket.test.id
- # key = each.key
- # source = "/home/ec2-user/${each.key}"
- # content_type = "text/html"
- # etag = filemd5("/home/ec2-user/${each.key}")
- #}
- # Copying a template
- locals {
- mime_types = {
- "css" = "text/css"
- "html" = "text/html"
- "ico" = "image/vnd.microsoft.icon"
- "js" = "application/javascript"
- "json" = "application/json"
- "map" = "application/json"
- "png" = "image/png"
- "svg" = "image/svg+xml"
- "txt" = "text/plain"
- "woff" = "application/font-woff"
- "woff2" = "application/font-woff2"
- "jpg" = "image/jpeg"
- }
- }
- resource "aws_s3_bucket_object" "content" {
- for_each = fileset("/home/ec2-user/chrisich.fun", "**/*.*")
- bucket = aws_s3_bucket.test.id
- key = each.key
- source = "/home/ec2-user/chrisich.fun/${each.key}"
- content_type = lookup(tomap(local.mime_types), element(split(".", each.key), length(split(".", each.key)) - 1))
- etag = filemd5("/home/ec2-user/chrisich.fun/${each.key}")
- }
- resource "aws_s3_bucket_website_configuration" "site" {
- bucket = aws_s3_bucket.test.id
- index_document {
- suffix = "index.html"
- }
- }
- ############### Output.tf ###########
- [ec2-user@ip-172-31-32-32 s3]$ cat output.tf
- output "bucket_url" {
- value = "http://${aws_s3_bucket_website_configuration.site.website_endpoint}"
- }
- [ec2-user@ip-172-31-32-32 s3]$ terraform output
- bucket_url = "http://bucket-shopping.s3-website.ap-south-1.amazonaws.com"
Advertisement
Add Comment
Please, Sign In to add comment