Skip to content

Commit

Permalink
add s3 bucket for static files, implement in network compose and k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
IkeHunter committed Nov 17, 2024
1 parent 0abed86 commit d5942a7
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 4 deletions.
1 change: 1 addition & 0 deletions docker-compose.network.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ services:
- SERVER_URI=server-jbx:9000
- WEBSOCKET_URI=server-jbx:9000
- CLUB_MANAGER_URI=club-app-network:9000
- PUBLIC_STATIC_URI=jukebox-client-631bab280a434e28995fd3fcafd1fa5a.s3-website-us-east-1.amazonaws.com

# attach: false

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
44 changes: 44 additions & 0 deletions infra/scripts/create-public-bucket.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

# Docs:
# Creating buckets: https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html
# cli create-bucket: https://docs.aws.amazon.com/cli/latest/reference/s3api/create-bucket.html
# cli public access block: https://docs.aws.amazon.com/cli/latest/reference/s3api/put-public-access-block.html
# cli bucket acls: https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-acl.html
# cli bucket website: https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-website.html

# Context: infra/

set -e

BUCKET_PREFIX="jukebox-client"
REGION="us-east-1"

# Create bucket with unique name
export bucket_name="$BUCKET_PREFIX-$(uuidgen | tr -d - | tr '[:upper:]' '[:lower:]' )"
aws s3api create-bucket \
--bucket "$bucket_name" \
--region "$REGION" \
--object-ownership BucketOwnerPreferred > /dev/null

# Disable default security protocols, allow public access
aws s3api put-public-access-block \
--bucket "$bucket_name" \
--public-access-block-configuration "BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false" > /dev/null


# Create policy so public can only read bucket content
aws s3api put-bucket-policy \
--bucket "$bucket_name" \
--policy "$(envsubst < ./templates/s3/s3-allow-public-access-policy.json.tpl)" > /dev/null

# Create s3 static website hosting
aws s3api put-bucket-website \
--bucket "$bucket_name" \
--website-configuration '{ "IndexDocument": { "Suffix": "index.html" } }' > /dev/null

bucket_uri="$bucket_name.s3-website-$REGION.amazonaws.com"
echo "Bucket upload uri: s3://$bucket_name"
echo "Bucket proxy uri: $bucket_uri"
echo "Bucket endpoint: http://$bucket_uri"

File renamed without changes.
16 changes: 16 additions & 0 deletions infra/templates/s3/s3-allow-public-access-policy.json.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::${bucket_name}/*"
]
}
]
}
File renamed without changes.
1 change: 1 addition & 0 deletions k8s/configs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ data:
SERVER_URI: 'jukebox-service:9000'
CLUB_MANAGER_URI: 'club-service:9000'
WEBSOCKET_URI: 'jukebox-service:9000'
PUBLIC_STATIC_URI: 'jukebox-client-631bab280a434e28995fd3fcafd1fa5a.s3-website-us-east-1.amazonaws.com'

---
apiVersion: v1
Expand Down
18 changes: 14 additions & 4 deletions proxy/default.conf.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,22 @@ server {
proxy_set_header Connection "upgrade";
}

# location /api/docs {
# root /vol/client;
# index index.html index.htm;
# try_files $uri $uri/ /index.html /vol/apidoc/;
# error_page 404 =200 /index.html;
# }

location / {
root /vol/client;
index index.html index.htm;
proxy_intercept_errors on;
proxy_redirect off;
proxy_hide_header X-Amz-Id-2;
proxy_hide_header X-Amz-Request-Id;
try_files $uri $uri/ /index.html /vol/apidoc/;
error_page 404 =200 /index.html;
error_page 400 403 404 500 =200 /index.html;
proxy_pass "http://$PUBLIC_STATIC_URI/jukebox-client/";
}

}

0 comments on commit d5942a7

Please sign in to comment.