From bd3a1a312a525c65a9683fe3dd7e13dd40e945f7 Mon Sep 17 00:00:00 2001 From: dzikafoczka Date: Sat, 21 Dec 2024 22:22:10 +0100 Subject: [PATCH] faktury --- ec2.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/ec2.py b/ec2.py index fb79820..92dbf1d 100644 --- a/ec2.py +++ b/ec2.py @@ -1,5 +1,6 @@ import boto3 import base64 +import os REPOZYTORIUM = "https://git.wmi.amu.edu.pl/s464863/aws_faktury.git" PREFIX = "s464863" @@ -252,10 +253,78 @@ def create_js_scripts(ip): js_file.write(js_content) print(f"Generated scripts.js with ip: {ip}") +def create_s3_bucket(s3_client, bucket_name): + s3_client.create_bucket( + Bucket=bucket_name, + CreateBucketConfiguration={'LocationConstraint': REGION} + ) + print(f"S3 bucket '{bucket_name}' created.") + + s3_client.put_bucket_website( + Bucket=bucket_name, + WebsiteConfiguration={ + 'IndexDocument': {'Suffix': 'index.html'}, + } + ) + print(f"Static website hosting configured for '{bucket_name}'.") + + return bucket_name + +def upload_files_to_s3(s3_client, bucket_name, folder_path): + for root, dirs, files in os.walk(folder_path): + for file in files: + file_path = os.path.join(root, file) + s3_key = os.path.relpath(file_path, folder_path) # Using relative path + s3_client.upload_file(file_path, bucket_name, s3_key) + print(f"Uploaded {file} to S3 bucket '{bucket_name}' at key '{s3_key}'.") + +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(): # Create EC2 client ec2_client = boto3.client('ec2', region_name=REGION) + # Create S3 client + s3_client = boto3.client('s3', region_name=REGION) + + # Create CloudFront client + cloudfront_client = boto3.client('cloudfront', region_name=REGION) + # Create VPC print("Creating VPC...") vpc_id = create_vpc(ec2_client) @@ -288,5 +357,18 @@ def main(): print("Running EC2 instance...") ec2_id = run_instance(ec2_client, launch_template_id, subnet) + # Create S3 bucket + print("Creating S3 bucket...") + bucket_name = create_s3_bucket(s3_client, f"{PREFIX}-bucket") + + # Upload files to S3 + print("Uploading files to S3...") + upload_files_to_s3(s3_client, bucket_name, 'static') + + # Create CloudFront distribution + print("Creating CloudFront distribution...") + distribution_domain = create_cloudfront_distribution(cloudfront_client, bucket_name) + print(f"Static website URL: http://{distribution_domain}") + if __name__ == '__main__': main() \ No newline at end of file