Reputation: 4389
I'm trying to automate creating/uploading a website to a specified bucket in S3.
Say for example I have this policy template:
{
"Version":"2012-10-17",
"Statement":[{
"Sid":"PublicReadGetObject",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":["arn:aws:s3:::example-bucket/*"
]
}
]
}
I create the bucket using:
aws s3api create-bucket --bucket $bucket --region eu-central-1 --create-bucket-configuration LocationConstraint=eu-central-1
How can I apply a policy to this bucket from a json string like the one above and also how to enable website hosting
- all this using awscli not amazon gui?
Upvotes: 1
Views: 206
Reputation: 4389
In case anyone is interested in this approach, I ended up using (for now) the following commands:
bucket=$1
region=$2
# Create bucket
aws s3api create-bucket --bucket $bucket --region $region --create-bucket-configuration LocationConstraint=$region
# Apply policy to allow get access to public
policy=$(cat /tmp/policy)
aws s3api put-bucket-policy --bucket $bucket --policy "$policy"
# Enable static hosting
aws s3 website s3://$bucket/ --index-document index.html
# Deploy app production distribution to new bucket
aws s3 cp dist/prod/ s3://$bucket --recursive
# See the results
open "http://$bucket.s3-website.$region.amazonaws.com"
But I'm accepting the other answer since this can grow to be very complex for more advanced requirements - compared to using boto3 in a python script that easily accepts different combinations of parameters e.g. skipping create bucket, reading default region from config etc.
Upvotes: 2
Reputation: 24643
See commands under aws s3api
:
$ aws s3api help | egrep -i "website|policy"
o delete-bucket-policy
o delete-bucket-website
o get-bucket-policy
o get-bucket-website
o put-bucket-policy
o put-bucket-website
For automating, I'd recommend dropping into a language. Python's boto3 is fantastic, the Ruby and Java SDKs are also very good.
Upvotes: 1