Each environment has two private buckets in the cluster's region. They must be two different buckets.
| Bucket | Setting | Holds | Written by | Read by |
|---|---|---|---|---|
| Assets | ASSETS_BUCKET |
Uploaded media under originals/<assetId>/<filename>, and the resized versions under derived/<hash>/<width>.<format> and derived/<hash>/<filename> |
The editor's browser (presigned PUT to originals/), the worker (derived/), the CMS (deletes), the one-off content load pod |
The CMS and worker pods; the site build, which copies only what the pages use |
| Website | WEBSITE_BUCKET |
The built public website: pages (<path>/index.html), _next/static/, media/, sitemaps, robots.txt, data files, the Google reviews feeds |
Only the Jenkins site build, which owns the whole bucket: anything the build does not have is deleted | CloudFront only, through Origin Access Control |
The CMS never writes to the website bucket (packages/content/src/media/blob-store.ts; docs/CONFIG.md, S3). The pods' IAM role on main has the assets bucket only (IAM).
Never point UAT and production at the same website bucket. The site build deletes everything in its bucket that the build being deployed does not contain, so a UAT build would wipe production's site.
#Who does what
| Task | Usually done by | What our engineer needs |
|---|---|---|
| Create both buckets | KIMS's cloud team, or our engineer if asked | The two names |
| The CORS rule on the assets bucket | Whoever creates it | The CMS host |
| The website bucket's policy for CloudFront | Whoever creates the distribution | Done on the CloudFront page |
| Filling the assets bucket | Our engineer, with the content load pod | Content load |
| Filling the website bucket | The first site build | Website |
#The values to choose
| Setting | Assets bucket | Website bucket | Why |
|---|---|---|---|
| Name (production) | kims-hospitals-assets-prod |
kims-hospitals-website-prod |
Names are unique across all of AWS, so add something of KIMS's. Lower case, digits and hyphens only: no dots, or presigned HTTPS URLs fail the certificate check |
| Name (UAT) | kims-hospitals-assets-uat |
kims-hospitals-website-uat |
|
| Bucket type | General purpose | General purpose | |
| Region | The cluster's region, the same as S3_REGION |
Same | |
| Object Ownership | ACLs disabled (Bucket owner enforced) | Same | |
| Block all public access | On (all four settings) | On (all four settings) | Both stay private; CloudFront reads the website bucket with OAC |
| Bucket Versioning | Enabled | Disabled | Assets holds the only copy of every upload; every build rewrites the website |
| Default encryption | SSE-S3, Bucket Key on | SSE-S3 | SSE-KMS would need KMS rights for the pods, the agent, the browser's presigned upload and CloudFront |
| CORS | PUT from the CMS host |
None | Browser uploads |
| Lifecycle | Expire noncurrent versions after 90 days | None | Keeps versioning from growing forever |
| Static website hosting | Off | Off | CloudFront uses the bucket's REST endpoint with OAC, never the s3-website endpoint |
#Set the names once
export AWS_REGION=ap-south-2
ASSETS_BUCKET=kims-hospitals-assets-prod
WEBSITE_BUCKET=kims-hospitals-website-prod
CMS_HOST=<CMS_HOST>export AWS_REGION=ap-south-2
ASSETS_BUCKET=kims-hospitals-assets-prod
WEBSITE_BUCKET=kims-hospitals-website-prod
CMS_HOST=<CMS_HOST>$env:AWS_REGION = "ap-south-2"
$AssetsBucket = "kims-hospitals-assets-prod"
$WebsiteBucket = "kims-hospitals-website-prod"
$CmsHost = "<CMS_HOST>"#Create the buckets: console steps
Do this twice: once for the assets bucket, once for the website bucket.
- S3 console, region selector set to the cluster's region > Buckets > Create bucket.
- Bucket type: General purpose. Bucket name: from the table.
- Object Ownership: ACLs disabled (recommended).
- Block Public Access settings for this bucket: Block all public access ticked.
- Bucket Versioning: Enable for the assets bucket, Disable for the website bucket.
- Default encryption: Server-side encryption with Amazon S3 managed keys (SSE-S3), Bucket Key Enable.
- Tags (optional):
project=kims-v2,environment=production. - Create bucket.
#Create the buckets: CLI steps
for BUCKET in "$ASSETS_BUCKET" "$WEBSITE_BUCKET"; do
aws s3api create-bucket --bucket "$BUCKET" \
--create-bucket-configuration LocationConstraint="$AWS_REGION"
aws s3api put-public-access-block --bucket "$BUCKET" \
--public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true
aws s3api put-bucket-ownership-controls --bucket "$BUCKET" \
--ownership-controls "Rules=[{ObjectOwnership=BucketOwnerEnforced}]"
aws s3api put-bucket-encryption --bucket "$BUCKET" \
--server-side-encryption-configuration '{"Rules":[{"ApplyServerSideEncryptionByDefault":{"SSEAlgorithm":"AES256"},"BucketKeyEnabled":true}]}'
aws s3api put-bucket-tagging --bucket "$BUCKET" \
--tagging 'TagSet=[{Key=project,Value=kims-v2},{Key=environment,Value=production}]'
done
aws s3api put-bucket-versioning --bucket "$ASSETS_BUCKET" --versioning-configuration Status=Enabledfor BUCKET in "$ASSETS_BUCKET" "$WEBSITE_BUCKET"; do
aws s3api create-bucket --bucket "$BUCKET" \
--create-bucket-configuration LocationConstraint="$AWS_REGION"
aws s3api put-public-access-block --bucket "$BUCKET" \
--public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true
aws s3api put-bucket-ownership-controls --bucket "$BUCKET" \
--ownership-controls "Rules=[{ObjectOwnership=BucketOwnerEnforced}]"
aws s3api put-bucket-encryption --bucket "$BUCKET" \
--server-side-encryption-configuration '{"Rules":[{"ApplyServerSideEncryptionByDefault":{"SSEAlgorithm":"AES256"},"BucketKeyEnabled":true}]}'
aws s3api put-bucket-tagging --bucket "$BUCKET" \
--tagging 'TagSet=[{Key=project,Value=kims-v2},{Key=environment,Value=production}]'
done
aws s3api put-bucket-versioning --bucket "$ASSETS_BUCKET" --versioning-configuration Status=Enabled@'
{ "Rules": [ { "ApplyServerSideEncryptionByDefault": { "SSEAlgorithm": "AES256" }, "BucketKeyEnabled": true } ] }
'@ | Set-Content -Encoding ascii encryption.json
foreach ($Bucket in $AssetsBucket, $WebsiteBucket) {
aws s3api create-bucket --bucket $Bucket `
--create-bucket-configuration "LocationConstraint=$($env:AWS_REGION)"
aws s3api put-public-access-block --bucket $Bucket `
--public-access-block-configuration "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true"
aws s3api put-bucket-ownership-controls --bucket $Bucket `
--ownership-controls "Rules=[{ObjectOwnership=BucketOwnerEnforced}]"
aws s3api put-bucket-encryption --bucket $Bucket `
--server-side-encryption-configuration file://encryption.json
aws s3api put-bucket-tagging --bucket $Bucket `
--tagging "TagSet=[{Key=project,Value=kims-v2},{Key=environment,Value=production}]"
}
aws s3api put-bucket-versioning --bucket $AssetsBucket --versioning-configuration Status=Enabledcreate-bucket prints the bucket's Location. The other commands print nothing when they succeed.
On Windows the JSON goes through a file written with -Encoding ascii, because Windows PowerShell 5.1 strips the double quotes out of JSON passed inline to a native program, and its default file encoding adds a byte-order mark the AWS CLI rejects.
#CORS on the assets bucket
Editors' browsers upload media straight to the assets bucket with a presigned PUT URL the CMS signs (presignUpload in blob-store.ts; the browser side is apps/cms/src/components/media/use-batch-upload.ts). The URL signs the Content-Type and Content-Length, is valid for 15 minutes, and the browser sends only the Content-Type header. Without this rule, every upload in the Media library fails in the browser with a CORS error, while the rest of the CMS works.
The rule from main's runbook (AWS set-up B), with production's CMS host on production's bucket and UAT's on UAT's:
[
{
"AllowedOrigins": ["https://<CMS_HOST>"],
"AllowedMethods": ["PUT"],
"AllowedHeaders": ["content-type"],
"ExposeHeaders": ["ETag"],
"MaxAgeSeconds": 3000
}
]- The origin is exactly
CMS_URL: scheme and host, no path, no trailing slash. Add a second origin only if editors use the CMS on a second host name. - No
GET: the browser never reads from the bucket. The CMS serves images to editors through its own/media/...route.
#Console steps
- S3 > the assets bucket > Permissions tab > Cross-origin resource sharing (CORS) > Edit.
- Paste the JSON above with the CMS host filled in > Save changes.
#CLI steps
The CLI wants the rules wrapped in CORSRules.
cat > cors.json <<EOF
{
"CORSRules": [
{
"AllowedOrigins": ["https://$CMS_HOST"],
"AllowedMethods": ["PUT"],
"AllowedHeaders": ["content-type"],
"ExposeHeaders": ["ETag"],
"MaxAgeSeconds": 3000
}
]
}
EOF
aws s3api put-bucket-cors --bucket "$ASSETS_BUCKET" --cors-configuration file://cors.json
aws s3api get-bucket-cors --bucket "$ASSETS_BUCKET"cat > cors.json <<EOF
{
"CORSRules": [
{
"AllowedOrigins": ["https://$CMS_HOST"],
"AllowedMethods": ["PUT"],
"AllowedHeaders": ["content-type"],
"ExposeHeaders": ["ETag"],
"MaxAgeSeconds": 3000
}
]
}
EOF
aws s3api put-bucket-cors --bucket "$ASSETS_BUCKET" --cors-configuration file://cors.json
aws s3api get-bucket-cors --bucket "$ASSETS_BUCKET"@"
{
"CORSRules": [
{
"AllowedOrigins": ["https://$CmsHost"],
"AllowedMethods": ["PUT"],
"AllowedHeaders": ["content-type"],
"ExposeHeaders": ["ETag"],
"MaxAgeSeconds": 3000
}
]
}
"@ | Set-Content -Encoding ascii cors.json
aws s3api put-bucket-cors --bucket $AssetsBucket --cors-configuration file://cors.json
aws s3api get-bucket-cors --bucket $AssetsBucketget-bucket-cors should print the rule back with your host.
#Check CORS like a browser would
This sends the same preflight request a browser sends before an upload. It needs no AWS credentials.
curl -sS -i -X OPTIONS "https://$ASSETS_BUCKET.s3.$AWS_REGION.amazonaws.com/originals/cors-test.jpg" \
-H "Origin: https://$CMS_HOST" \
-H "Access-Control-Request-Method: PUT" \
-H "Access-Control-Request-Headers: content-type" | head -n 12curl -sS -i -X OPTIONS "https://$ASSETS_BUCKET.s3.$AWS_REGION.amazonaws.com/originals/cors-test.jpg" \
-H "Origin: https://$CMS_HOST" \
-H "Access-Control-Request-Method: PUT" \
-H "Access-Control-Request-Headers: content-type" | head -n 12curl.exe -sS -i -X OPTIONS "https://$AssetsBucket.s3.$($env:AWS_REGION).amazonaws.com/originals/cors-test.jpg" `
-H "Origin: https://$CmsHost" `
-H "Access-Control-Request-Method: PUT" `
-H "Access-Control-Request-Headers: content-type" | Select-Object -First 12You should see HTTP/1.1 200 OK with Access-Control-Allow-Origin: https://<CMS_HOST> and Access-Control-Allow-Methods: PUT. A 403 with CORSResponse: This CORS request is not allowed means the origin or header does not match the rule.
#Lifecycle rule on the assets bucket
With versioning on, a deleted or replaced media file stays as a noncurrent version. Keep them 90 days, then let S3 remove them, and clean up any abandoned multipart uploads.
Console: the assets bucket > Management tab > Lifecycle rules > Create lifecycle rule > Lifecycle rule name expire-old-versions > Apply to all objects in the bucket (tick the acknowledgement) > tick Permanently delete noncurrent versions of objects and Delete expired object delete markers or incomplete multipart uploads > Days after objects become noncurrent 90 > tick Delete incomplete multipart uploads, Number of days 7 > Create rule.
Save as assets-lifecycle.json (the same on every system; on Windows save it as ASCII or UTF-8 without BOM):
{
"Rules": [
{
"ID": "expire-old-versions",
"Status": "Enabled",
"Filter": {},
"NoncurrentVersionExpiration": { "NoncurrentDays": 90 },
"AbortIncompleteMultipartUpload": { "DaysAfterInitiation": 7 },
"Expiration": { "ExpiredObjectDeleteMarker": true }
}
]
}aws s3api put-bucket-lifecycle-configuration --bucket "$ASSETS_BUCKET" --lifecycle-configuration file://assets-lifecycle.jsonaws s3api put-bucket-lifecycle-configuration --bucket "$ASSETS_BUCKET" --lifecycle-configuration file://assets-lifecycle.jsonaws s3api put-bucket-lifecycle-configuration --bucket $AssetsBucket --lifecycle-configuration file://assets-lifecycle.jsonThe website bucket needs no lifecycle rule: versioning is off and the site build deletes what it no longer ships.
#Optional: refuse plain HTTP on the assets bucket
Everything KIMS v2 does uses HTTPS already, so this changes nothing for it and protects against a mistake elsewhere. The website bucket's policy is set on the CloudFront page; do not put this one there unless you merge the two statements.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyPlainHttp",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": ["arn:aws:s3:::<ASSETS_BUCKET>", "arn:aws:s3:::<ASSETS_BUCKET>/*"],
"Condition": { "Bool": { "aws:SecureTransport": "false" } }
}
]
}Console: assets bucket > Permissions > Bucket policy > Edit > paste > Save changes. CLI: save it as assets-policy.json, then aws s3api put-bucket-policy --bucket <ASSETS_BUCKET> --policy file://assets-policy.json.
#Check the buckets
for BUCKET in "$ASSETS_BUCKET" "$WEBSITE_BUCKET"; do
echo "== $BUCKET"
aws s3api get-bucket-location --bucket "$BUCKET" --output text
aws s3api get-public-access-block --bucket "$BUCKET" --query PublicAccessBlockConfiguration
aws s3api get-bucket-versioning --bucket "$BUCKET"
aws s3api get-bucket-encryption --bucket "$BUCKET" --query "ServerSideEncryptionConfiguration.Rules[0].ApplyServerSideEncryptionByDefault.SSEAlgorithm" --output text
donefor BUCKET in "$ASSETS_BUCKET" "$WEBSITE_BUCKET"; do
echo "== $BUCKET"
aws s3api get-bucket-location --bucket "$BUCKET" --output text
aws s3api get-public-access-block --bucket "$BUCKET" --query PublicAccessBlockConfiguration
aws s3api get-bucket-versioning --bucket "$BUCKET"
aws s3api get-bucket-encryption --bucket "$BUCKET" --query "ServerSideEncryptionConfiguration.Rules[0].ApplyServerSideEncryptionByDefault.SSEAlgorithm" --output text
doneforeach ($Bucket in $AssetsBucket, $WebsiteBucket) {
"== $Bucket"
aws s3api get-bucket-location --bucket $Bucket --output text
aws s3api get-public-access-block --bucket $Bucket --query PublicAccessBlockConfiguration
aws s3api get-bucket-versioning --bucket $Bucket
aws s3api get-bucket-encryption --bucket $Bucket --query "ServerSideEncryptionConfiguration.Rules[0].ApplyServerSideEncryptionByDefault.SSEAlgorithm" --output text
}You should see, for each: the region (ap-south-2), all four public access settings true, "Status": "Enabled" for assets and nothing (or Suspended) for website, and AES256.
After the content load, the assets bucket holds about 11,307 files (about 530 MB):
aws s3 ls "s3://$ASSETS_BUCKET/" --recursive --summarize | tail -n 2aws s3 ls "s3://$ASSETS_BUCKET/" --recursive --summarize | tail -n 2aws s3 ls "s3://$AssetsBucket/" --recursive --summarize | Select-Object -Last 2#If it goes wrong
| What you see | Likely cause | What to do |
|---|---|---|
BucketAlreadyExists |
Someone in any AWS account has that name | Choose another name |
IllegalLocationConstraintException |
The region in LocationConstraint differs from the CLI's region |
Make AWS_REGION and LocationConstraint the same |
Media library: upload fails at once, browser console shows blocked by CORS policy |
No CORS rule, wrong origin (http vs https, trailing slash, other host) | CORS on the assets bucket and its curl check |
Upload fails with 403 and SignatureDoesNotMatch |
S3_REGION differs from the bucket's region, or a proxy changed the request |
Set S3_REGION to the bucket's region; try without the proxy |
Upload fails with 403 and AccessDenied |
The pods' role cannot s3:PutObject on the bucket (the presigned URL carries the pod's rights) |
IAM, the pods' role |
| Upload works but sizes never appear | The worker cannot read or write the bucket, or no route to S3 | kubectl -n kims logs deploy/kims-worker; Networking |
Site build fails at Bundle media with not in the assets bucket |
A page uses media whose derived file is missing | The build log names the key; reload the bundle or re-upload that media |
Site build fails at Sync with AccessDenied |
The agent's role lacks website bucket write | IAM, the site job |
put-bucket-encryption on Windows: Error parsing parameter |
Inline JSON lost its quotes | Use the file://encryption.json form above |
#Sources
kims-v2/docs/ON-SITE-DEPLOYMENT.mdonmain(AWS set-up B)kims-v2/docs/CONFIG.mdonmain(S3)kims-v2/packages/content/src/media/blob-store.ts,kims-v2/packages/content/src/media/keys.tskims-v2/apps/cms/src/components/media/use-batch-upload.tskims-v2/scripts/site-build/sync.mjs,kims-v2/scripts/site-build/media.mjskims-v2/deploy/jenkins/README-site.mdonmain- https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-control-block-public-access.html
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/cors.html
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/ManageCorsUsing.html
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-presigned-url.html
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/default-bucket-encryption.html
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/lifecycle-configuration-examples.html