Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- provider "aws" {
- region = var.region
- }
- terraform {
- backend "s3" {
- bucket = "terraform-bucket-iampolicy"
- key = "terraform.tfstate"
- region = "ap-south-1"
- }
- }
- # Bucket creation [Error happend ran the terraform init again]
- resource "aws_s3_bucket" "test" {
- bucket = "terraform-bucket-iampolicy"
- force_destroy = true
- # versioning {
- # enabled = true
- #}
- tags = {
- Name = "terraform-bucket-iampolicy"
- 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.hmtl"
- 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}")
- }
- ####### Teraform Output #########
- [ec2-user@ip-172-31-32-32 s3-bucket]$ terraform output
- url_chris_html = "http://terraform-bucket-iampolicy.s3.amazonaws.com/chris.html"
- url_index_html = "http://terraform-bucket-iampolicy.s3.amazonaws.com/index.html"
- ###### Terraform state list ###########
- [ec2-user@ip-172-31-32-32 s3-bucket]$ terraform state list
- data.aws_iam_policy_document.iam
- aws_s3_bucket.test
- aws_s3_bucket_object.index
- aws_s3_bucket_object.multiple["ashna.html"]
- aws_s3_bucket_object.multiple["chris.html"]
- aws_s3_bucket_object.multiple["index.html"]
- aws_s3_bucket_object.multiple["s3-bucket/index.html"]
- aws_s3_bucket_policy.attach
- aws_s3_bucket_public_access_block.false
- aws_s3_bucket_versioning.version
Advertisement
Add Comment
Please, Sign In to add comment