This commit is contained in:
dzikafoczka 2024-12-21 22:47:08 +01:00
parent f23cc3c677
commit a38cddf060
2 changed files with 49 additions and 0 deletions

45
cloudfront.py Normal file
View File

@ -0,0 +1,45 @@
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)

4
cloudfront.sh Normal file
View File

@ -0,0 +1,4 @@
#!/bin/bash
chmod +x cloudfront.py
pip install boto3
python cloudfront.py