45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
import boto3
|
|
import base64
|
|
import os
|
|
|
|
def create_cloudfront_distribution(cloudfront_client, bucket_name):
|
|
# Create CloudFront distribution for the S3 static website
|
|
response = cloudfront_client.create_distribution(
|
|
DistributionConfig={
|
|
'CallerReference': str(base64.b64encode(os.urandom(32))),
|
|
'Origins': {
|
|
'Items': [
|
|
{
|
|
'Id': bucket_name,
|
|
'DomainName': f'{bucket_name}.s3.amazonaws.com',
|
|
'S3OriginConfig': {
|
|
'OriginAccessIdentity': ''
|
|
},
|
|
}
|
|
],
|
|
'Quantity': 1
|
|
},
|
|
'Enabled': True,
|
|
'DefaultCacheBehavior': {
|
|
'TargetOriginId': bucket_name,
|
|
'ViewerProtocolPolicy': 'redirect-to-https',
|
|
'AllowedMethods': {
|
|
'Items': ['GET', 'HEAD'],
|
|
'Quantity': 2,
|
|
},
|
|
},
|
|
'DefaultRootObject': 'index.html',
|
|
}
|
|
)
|
|
distribution_id = response['Distribution']['Id']
|
|
distribution_domain = response['Distribution']['DomainName']
|
|
|
|
print(f"CloudFront distribution created with ID: {distribution_id}")
|
|
print(f"CloudFront URL: http://{distribution_domain}")
|
|
|
|
return distribution_domain
|
|
|
|
def main():
|
|
cloudfront_client = boto3.client('cloudfront', region_name='us-east-1')
|
|
bucket_name = 's464863-bucket'
|
|
create_cloudfront_distribution(cloudfront_client, bucket_name) |