lb2
This commit is contained in:
parent
aca5547293
commit
5c20a1775f
@ -63,7 +63,8 @@ def delete_resources(ec2_client, elbv2_client, resources):
|
||||
delete_key_pair(ec2_client, resources["key_name"])
|
||||
delete_internet_gateway(ec2_client, resources["igw_id"], resources["vpc_id"])
|
||||
delete_route_table(ec2_client, resources["route_table_id"])
|
||||
delete_subnet(ec2_client, resources["subnet_id"])
|
||||
delete_subnet(ec2_client, resources["subnet_ids"][0])
|
||||
delete_subnet(ec2_client, resources["subnet_ids"][1])
|
||||
delete_vpc(ec2_client, resources["vpc_id"])
|
||||
|
||||
def main():
|
||||
|
51
lb2.py
51
lb2.py
@ -1,6 +1,7 @@
|
||||
import boto3
|
||||
import base64
|
||||
from USER_DATA import user_data
|
||||
import json
|
||||
|
||||
# Configuration variables
|
||||
PREFIX = "s464863"
|
||||
@ -25,7 +26,6 @@ def create_vpc(ec2_client):
|
||||
]
|
||||
)
|
||||
vpc_id = response['Vpc']['VpcId']
|
||||
aws_resources['vpc_id'] = vpc_id
|
||||
print(f"Created VPC with ID: {vpc_id}")
|
||||
|
||||
# Enable DNS support and hostnames
|
||||
@ -49,7 +49,6 @@ def create_subnet(ec2_client, vpc_id):
|
||||
]
|
||||
)
|
||||
subnet_id = response['Subnet']['SubnetId']
|
||||
aws_resources['subnet_id'] = subnet_id
|
||||
print(f"Created Subnet with ID: {subnet_id}")
|
||||
|
||||
# Public IP addresses for instances in the subnet
|
||||
@ -70,7 +69,6 @@ def create_internet_gateway(ec2_client, vpc_id):
|
||||
]
|
||||
)
|
||||
igw_id = response['InternetGateway']['InternetGatewayId']
|
||||
aws_resources['igw_id'] = igw_id
|
||||
print(f"Created Internet Gateway with ID: {igw_id}")
|
||||
|
||||
# Attach Internet Gateway to VPC
|
||||
@ -92,7 +90,6 @@ def create_route_table(ec2_client, vpc_id, subnet_id, igw_id):
|
||||
]
|
||||
)
|
||||
route_table_id = response['RouteTable']['RouteTableId']
|
||||
aws_resources['route_table_id'] = route_table_id
|
||||
print(f"Created Route Table with ID: {route_table_id}")
|
||||
|
||||
# Create route to Internet Gateway
|
||||
@ -120,7 +117,6 @@ def create_key_pair(ec2_client, key_name, save_to_file):
|
||||
file.write(private_key)
|
||||
|
||||
print(f"Key pair '{key_name}' created and saved to '{save_to_file}'.")
|
||||
aws_resources['key_name'] = key_name
|
||||
return response['KeyName']
|
||||
|
||||
|
||||
@ -135,7 +131,6 @@ def create_security_group(ec2_client, vpc_id):
|
||||
)
|
||||
|
||||
security_group_id = response['GroupId']
|
||||
aws_resources['security_group_id'] = security_group_id
|
||||
print(f"Security group '{group_name}' created with ID: {security_group_id}")
|
||||
|
||||
# Set ingress rules
|
||||
@ -175,7 +170,6 @@ def create_launch_template(ec2_client, key_name, security_group_id):
|
||||
)
|
||||
|
||||
print(f"Launch Template created with ID: {response['LaunchTemplate']['LaunchTemplateId']}")
|
||||
aws_resources['launch_template_id'] = response['LaunchTemplate']['LaunchTemplateId']
|
||||
return response['LaunchTemplate']['LaunchTemplateId']
|
||||
|
||||
|
||||
@ -198,7 +192,6 @@ def create_target_group(elbv2_client, vpc_id):
|
||||
)
|
||||
|
||||
print(f"Target Group created with ARN: {response['TargetGroups'][0]['TargetGroupArn']}")
|
||||
aws_resources['target_group_arn'] = response['TargetGroups'][0]['TargetGroupArn']
|
||||
return response['TargetGroups'][0]['TargetGroupArn']
|
||||
|
||||
|
||||
@ -215,7 +208,6 @@ def create_load_balancer(elbv2_client, subnet_id, security_group_id):
|
||||
|
||||
load_balancer_arn = response['LoadBalancers'][0]['LoadBalancerArn']
|
||||
print(f"Load Balancer created with ARN: {load_balancer_arn}")
|
||||
aws_resources['load_balancer_arn'] = load_balancer_arn
|
||||
return load_balancer_arn
|
||||
|
||||
|
||||
@ -234,7 +226,6 @@ def create_load_balancer_listener(elbv2_client, load_balancer_arn, target_group_
|
||||
)
|
||||
|
||||
print(f"Listener created with ARN: {response['Listeners'][0]['ListenerArn']}")
|
||||
aws_resources['listener_arn'] = response['Listeners'][0]['ListenerArn']
|
||||
return response['Listeners'][0]['ListenerArn']
|
||||
|
||||
|
||||
@ -251,7 +242,6 @@ def create_ec2_instances(ec2_client, launch_template_id, subnet_id):
|
||||
|
||||
instance_ids = [instance['InstanceId'] for instance in response['Instances']]
|
||||
print(f"Created EC2 instances with IDs: {instance_ids}")
|
||||
aws_resources['instance_ids'] = instance_ids
|
||||
return instance_ids
|
||||
|
||||
|
||||
@ -269,19 +259,17 @@ def main():
|
||||
region_name=REGION
|
||||
)
|
||||
|
||||
# Auto Scaling client
|
||||
autoscaling_client = boto3.client(
|
||||
'autoscaling',
|
||||
region_name=REGION
|
||||
)
|
||||
|
||||
# Create VPC
|
||||
print("Creating VPC...")
|
||||
vpc_id = create_vpc(ec2_client)
|
||||
|
||||
# Create subnet
|
||||
print("Creating subnet...")
|
||||
subnet_id = create_subnet(ec2_client, vpc_id)
|
||||
# Create first subnet
|
||||
print("Creating first subnet...")
|
||||
subnet_id_1 = create_subnet(ec2_client, vpc_id, availability_zone='us-east-1a')
|
||||
|
||||
# Create second subnet
|
||||
print("Creating second subnet...")
|
||||
subnet_id_2 = create_subnet(ec2_client, vpc_id, availability_zone='us-east-1b')
|
||||
|
||||
# Create Internet Gateway
|
||||
print("Creating Internet Gateway...")
|
||||
@ -289,7 +277,7 @@ def main():
|
||||
|
||||
# Create Route Table
|
||||
print("Creating Route Table...")
|
||||
route_table_id = create_route_table(ec2_client, vpc_id, subnet_id, igw_id)
|
||||
route_table_id = create_route_table(ec2_client, vpc_id, subnet_id_1, igw_id)
|
||||
|
||||
# Create key pair
|
||||
print("Creating key pair...")
|
||||
@ -309,11 +297,11 @@ def main():
|
||||
|
||||
# Create Load Balancer
|
||||
print("Creating Load Balancer...")
|
||||
load_balancer_arn = create_load_balancer(elbv2_client, subnet_id, security_group_id)
|
||||
load_balancer_arn = create_load_balancer(elbv2_client, [subnet_id_1, subnet_id_2], security_group_id)
|
||||
|
||||
# Create EC2 instances
|
||||
print("Creating EC2 instances...")
|
||||
instance_ids = create_ec2_instances(ec2_client, launch_template_id, subnet_id)
|
||||
instance_ids = create_ec2_instances(ec2_client, launch_template_id, subnet_id_1)
|
||||
|
||||
# Create Load Balancer Listener
|
||||
print("Creating Load Balancer Listener...")
|
||||
@ -335,8 +323,23 @@ def main():
|
||||
print(f"Load Balancer DNS name: {dns_name}")
|
||||
|
||||
# Save AWS resources to file
|
||||
aws_resources = {
|
||||
'vpc_id': vpc_id,
|
||||
'subnet_ids': [subnet_id_1, subnet_id_2],
|
||||
'igw_id': igw_id,
|
||||
'route_table_id': route_table_id,
|
||||
'key_name': key_name,
|
||||
'security_group_id': security_group_id,
|
||||
'launch_template_id': launch_template_id,
|
||||
'target_group_arn': target_group_arn,
|
||||
'load_balancer_arn': load_balancer_arn,
|
||||
'listener_arn': listener_arn,
|
||||
'instance_ids': instance_ids,
|
||||
'dns_name': dns_name
|
||||
}
|
||||
|
||||
with open(AWS_RESOURCES_FILE, 'w') as file:
|
||||
file.write(str(aws_resources))
|
||||
json.dump(aws_resources, file, indent=4)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Loading…
Reference in New Issue
Block a user