test
This commit is contained in:
parent
a38cddf060
commit
bddb8ca9c3
113
ec2.py
113
ec2.py
@ -284,43 +284,6 @@ def upload_files_to_s3(s3_client, bucket_name, folder_path):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Failed to upload {file} to S3 bucket '{bucket_name}': {str(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():
|
def main():
|
||||||
# Create EC2 client
|
# Create EC2 client
|
||||||
ec2_client = boto3.client('ec2', region_name=REGION)
|
ec2_client = boto3.client('ec2', region_name=REGION)
|
||||||
@ -328,54 +291,46 @@ def main():
|
|||||||
# Create S3 client
|
# Create S3 client
|
||||||
s3_client = boto3.client('s3', region_name=REGION)
|
s3_client = boto3.client('s3', region_name=REGION)
|
||||||
|
|
||||||
# Create CloudFront client
|
# Create VPC
|
||||||
cloudfront_client = boto3.client('cloudfront', region_name=REGION)
|
print("Creating VPC...")
|
||||||
|
vpc_id = create_vpc(ec2_client)
|
||||||
|
|
||||||
# # Create VPC
|
# Create subnet
|
||||||
# print("Creating VPC...")
|
print("Creating subnet...")
|
||||||
# vpc_id = create_vpc(ec2_client)
|
subnet = create_subnet(ec2_client, vpc_id, cidr_block='10.0.1.0/24', availability_zone='us-east-1a')
|
||||||
#
|
|
||||||
# # Create subnet
|
# Create Internet Gateway
|
||||||
# print("Creating subnet...")
|
print("Creating Internet Gateway...")
|
||||||
# subnet = create_subnet(ec2_client, vpc_id, cidr_block='10.0.1.0/24', availability_zone='us-east-1a')
|
igw_id = create_internet_gateway(ec2_client, vpc_id)
|
||||||
#
|
|
||||||
# # Create Internet Gateway
|
# Create Route Table
|
||||||
# print("Creating Internet Gateway...")
|
print("Creating Route Table...")
|
||||||
# igw_id = create_internet_gateway(ec2_client, vpc_id)
|
route_table_id = create_route_table(ec2_client, vpc_id, subnet, igw_id)
|
||||||
#
|
|
||||||
# # Create Route Table
|
# Create Key Pair
|
||||||
# print("Creating Route Table...")
|
print("Creating Key Pair...")
|
||||||
# route_table_id = create_route_table(ec2_client, vpc_id, subnet, igw_id)
|
key_name = create_key_pair(ec2_client, f"{PREFIX}-key", f"{PREFIX}-key.pem")
|
||||||
#
|
|
||||||
# # Create Key Pair
|
# Create Security Group
|
||||||
# print("Creating Key Pair...")
|
print("Creating Security Group...")
|
||||||
# key_name = create_key_pair(ec2_client, f"{PREFIX}-key", f"{PREFIX}-key.pem")
|
security_group_id = create_security_group(ec2_client, vpc_id)
|
||||||
#
|
|
||||||
# # Create Security Group
|
# Create Launch Template
|
||||||
# print("Creating Security Group...")
|
print("Creating Launch Template...")
|
||||||
# security_group_id = create_security_group(ec2_client, vpc_id)
|
launch_template_id = create_launch_template(ec2_client, key_name, security_group_id)
|
||||||
#
|
|
||||||
# # Create Launch Template
|
# Run EC2 instance
|
||||||
# print("Creating Launch Template...")
|
print("Running EC2 instance...")
|
||||||
# launch_template_id = create_launch_template(ec2_client, key_name, security_group_id)
|
ec2_id = run_instance(ec2_client, launch_template_id, subnet)
|
||||||
#
|
|
||||||
# # Run EC2 instance
|
# Create S3 bucket
|
||||||
# print("Running EC2 instance...")
|
print("Creating S3 bucket...")
|
||||||
# ec2_id = run_instance(ec2_client, launch_template_id, subnet)
|
bucket_name = create_s3_bucket(s3_client, f"{PREFIX}-bucket")
|
||||||
#
|
|
||||||
# # Create S3 bucket
|
|
||||||
# print("Creating S3 bucket...")
|
|
||||||
# bucket_name = create_s3_bucket(s3_client, f"{PREFIX}-bucket")
|
|
||||||
bucket_name = "s464863-bucket"
|
bucket_name = "s464863-bucket"
|
||||||
|
|
||||||
# Upload files to S3
|
# Upload files to S3
|
||||||
print("Uploading files to S3...")
|
print("Uploading files to S3...")
|
||||||
upload_files_to_s3(s3_client, bucket_name, 'static')
|
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__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
Loading…
Reference in New Issue
Block a user