diff --git a/cloudfront.py b/cloudfront.py new file mode 100644 index 0000000..5238cfd --- /dev/null +++ b/cloudfront.py @@ -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) \ No newline at end of file diff --git a/cloudfront.sh b/cloudfront.sh new file mode 100644 index 0000000..6baa7d3 --- /dev/null +++ b/cloudfront.sh @@ -0,0 +1,4 @@ +#!/bin/bash +chmod +x cloudfront.py +pip install boto3 +python cloudfront.py \ No newline at end of file