Skip to content

Commit

Permalink
create terraform s3
Browse files Browse the repository at this point in the history
  • Loading branch information
Doris-Siu committed Mar 3, 2024
1 parent b5e069d commit 1471ce0
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 1 deletion.
Binary file modified .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.env
.env
client/build
Binary file added client/.DS_Store
Binary file not shown.
34 changes: 34 additions & 0 deletions terraform-s3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc
25 changes: 25 additions & 0 deletions terraform-s3/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 85 additions & 0 deletions terraform-s3/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}

provider "aws" {
region = "eu-west-2"
access_key = var.aws_access_key_id
secret_key = var.aws_secret_access_key
}

module "template_files" {
source = "hashicorp/dir/template"
base_dir = "${path.module}/../client/build"
}

#create a s3 bucket, attach bucket policy
resource "aws_s3_bucket" "bucket" {
bucket = "my-video-app"
force_destroy = true
}

resource "aws_s3_bucket_website_configuration" "hosting_bucket_website_configuration" {
bucket = aws_s3_bucket.bucket.id
index_document {
suffix = "index.html"
}
error_document {
key = "error.html"
}
}
#enable public acl
resource "aws_s3_bucket_ownership_controls" "bucket" {
bucket = aws_s3_bucket.bucket.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}

resource "aws_s3_bucket_public_access_block" "bucket" {
bucket = aws_s3_bucket.bucket.id

block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}

resource "aws_s3_bucket_policy" "my_bucket_policy" {
bucket = aws_s3_bucket.bucket.id

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Statement1",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-video-app/*"
}
]
}
EOF
}

#upload all the files of build folder to the bucket
resource "aws_s3_object" "hosting_bucket_files" {
bucket = aws_s3_bucket.bucket.id

for_each = module.template_files.files

key = each.key
content_type = each.value.content_type

source = each.value.source_path
content = each.value.content

etag = each.value.digests.md5
}
10 changes: 10 additions & 0 deletions terraform-s3/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
variable "aws_access_key_id" {
type = string
description = "AWS Access Key ID"
}

variable "aws_secret_access_key" {
type = string
sensitive = true
description = "AWS Secret Access Key"
}

0 comments on commit 1471ce0

Please sign in to comment.