You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

70 lines
1.9 KiB

import boto3
from botocore.exceptions import ClientError
region = 'eu-central-1'
availabilityZone = 'eu-central-1b'
subnet1 = 'subnet-41422b28'
subnet2 = 'subnet-5c5f6d16'
subnet3 = 'subnet-6f2ea214'
client = boto3.setup_default_session(region_name=region)
ec2Client = boto3.client("ec2")
ec2Resource = boto3.resource('ec2')
response = ec2Client.describe_vpcs()
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
elbv2Client = boto3.client('elbv2')
for i in range(3, 4):
print("Running new Web Server instance...")
print("------------------------------------")
response = ec2Client.run_instances(
ImageId=imageId,
InstanceType=instanceType,
Placement={'AvailabilityZone': availabilityZone, },
KeyName=keyName,
MinCount=1,
MaxCount=1,
UserData=userDataWebServer,
SecurityGroupIds=[
security_group_id,
],
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{'Key': 'Name', 'Value': 'tug-of-war-webserver1'},
{'Key': 'tug-of-war', 'Value': 'webserver'}
],
}
],
)
instanceIdWeb = response['Instances'][0]['InstanceId']
instance = ec2Resource.Instance(instanceIdWeb)
instance.wait_until_running()
instance.load()
print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address)
try:
response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup'])
targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
except ClientError as e:
print(e)
print("Registering instance...")
print("------------------------------------")
response = elbv2Client.register_targets(
TargetGroupArn=targetgroup_arn,
Targets=[
{
'Id': instanceIdWeb,
},
],
)