This commit is contained in:
dzikafoczka 2024-12-21 22:47:14 +01:00
parent a38cddf060
commit bddb8ca9c3

113
ec2.py
View File

@ -284,43 +284,6 @@ def upload_files_to_s3(s3_client, bucket_name, folder_path):
except Exception as e:
print(f"Failed to upload {file} to S3 bucket '{bucket_name}': {str(e)}")
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)
@ -328,54 +291,46 @@ def main():
# 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)
# # Create VPC
# print("Creating VPC...")
# vpc_id = create_vpc(ec2_client)
#
# # Create subnet
# print("Creating subnet...")
# subnet = create_subnet(ec2_client, vpc_id, cidr_block='10.0.1.0/24', availability_zone='us-east-1a')
#
# # Create Internet Gateway
# print("Creating Internet Gateway...")
# igw_id = create_internet_gateway(ec2_client, vpc_id)
#
# # Create Route Table
# print("Creating Route Table...")
# route_table_id = create_route_table(ec2_client, vpc_id, subnet, igw_id)
#
# # Create Key Pair
# print("Creating Key Pair...")
# key_name = create_key_pair(ec2_client, f"{PREFIX}-key", f"{PREFIX}-key.pem")
#
# # Create Security Group
# print("Creating Security Group...")
# security_group_id = create_security_group(ec2_client, vpc_id)
#
# # Create Launch Template
# print("Creating Launch Template...")
# launch_template_id = create_launch_template(ec2_client, key_name, security_group_id)
#
# # Run EC2 instance
# 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")
# Create subnet
print("Creating subnet...")
subnet = create_subnet(ec2_client, vpc_id, cidr_block='10.0.1.0/24', availability_zone='us-east-1a')
# Create Internet Gateway
print("Creating Internet Gateway...")
igw_id = create_internet_gateway(ec2_client, vpc_id)
# Create Route Table
print("Creating Route Table...")
route_table_id = create_route_table(ec2_client, vpc_id, subnet, igw_id)
# Create Key Pair
print("Creating Key Pair...")
key_name = create_key_pair(ec2_client, f"{PREFIX}-key", f"{PREFIX}-key.pem")
# Create Security Group
print("Creating Security Group...")
security_group_id = create_security_group(ec2_client, vpc_id)
# Create Launch Template
print("Creating Launch Template...")
launch_template_id = create_launch_template(ec2_client, key_name, security_group_id)
# Run EC2 instance
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")
bucket_name = "s464863-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()