def main(): # Create a VPC ec2_client = EC2Client().get_client() vpc = VPC(ec2_client) vpc_response = vpc.create_vpc() print(f"VPC created: {vpc_response}") # Add name tag to VPC vpc_name = 'Boto3-VPC' vpc_id = vpc_response['Vpc']['VpcId'] vpc.add_name_tag(vpc_id, vpc_name) print(f"Added {vpc_name} to {vpc_id}")
def main(): # Create VPC ec2_client = EC2Client().get_client() vpc = VPC(ec2_client) vpc_response = vpc.create_vpc() print('VPC created:' + str(vpc_response)) # Add name tag to VPC vpc_name = 'Boto3-VPC' vpc_id = vpc_response['Vpc']['VpcId'] vpc.add_name_tag(vpc_id, vpc_name) print('Added ' + vpc_name + ' to ' + vpc_id) # Create an IGW igw_response = vpc.create_internet_gateway() igw_id = igw_response['InternetGateway']['InternetGatewayId'] vpc.attach_igw_to_vpc(vpc_id, igw_id)
def main(): #VPC creation ec2_client = EC2Client().get_client() vpc = VPC(ec2_client) response = vpc.create_vpc() vpc_id = response['Vpc']['VpcId'] print('VPC ==> ', str(response)) igw_instance = vpc.create_igw() igw_id = igw_instance['InternetGateway']['InternetGatewayId'] vpc.attach_igw(igw_id, vpc_id) #Add name tag vpc_name = 'VPC-Igw-sub-route' vpc.add_name_tag(vpc_id, vpc_name) pub_subnet_resp = vpc.create_subnet(vpc_id, '10.0.1.0/24') subnet_id = pub_subnet_resp['Subnet']['SubnetId'] vpc.add_name_tag(subnet_id, 'Boto3-AWS') pub_routetable_resp = vpc.create_route_table(vpc_id) rtb_id = pub_routetable_resp['RouteTable']['RouteTableId'] vpc.create_igw_route(rtb_id, igw_id) vpc.associate_subnet_routetb(subnet_id, rtb_id) vpc.auto_assign_ip_subnet(subnet_id) # Create a private subnet priv_response = vpc.create_subnet(vpc_id, '10.0.0.0/24') priv_subnet_id = priv_response['Subnet']['SubnetId'] # Add name tag to private subnet vpc.add_name_tag(priv_subnet_id, 'Boto3-AWS') # EC2 Instances # Create a keypair ec2 = EC2(ec2_client) key_name = 'boto3-key' key_pair = ec2.create_key_pair(key_name) print('Key ==> ', str(key_pair)) # Create security group pub_sec_grp = 'Boto-pub-sec-grp' secgrp_res = ec2.create_sec_grp(pub_sec_grp, 'Public Security group', vpc_id) scgrp_id = secgrp_res['GroupId'] ec2.add_inbound_sg_rule(scgrp_id) user_data = """ #!/bin/bash yum update -y yum install httpd24 -y service httpd start chkconfig httpd on echo "<html><body><h1> Hello from <b>Boto3</b></h1></body></html>" > /var/www/html/index.html """ ec2.launch_ec2_instance('ami-00068cd7555f543d5', key_name, 1, 1, scgrp_id, subnet_id, user_data)
def main(): # Create a VPC ec2_client = EC2Client().get_client() vpc = VPC(ec2_client) vpc_response = vpc.create_vpc() print('VPC created' + str(vpc_response)) # Add name ag to VPC vpc_name = 'Boto3-VPC' vpc_id = vpc_response['Vpc']['VpcId'] vpc.add_name_tag(vpc_id, vpc_name) print('added ' + vpc_name + ' to ' + vpc_id) # Create an IGW igw_response = vpc.create_internet_gateway() igw_id = igw_response['InternetGateway']['InternetGatewayId'] vpc.attach_igw_to_vpc(vpc_id, igw_id) print('VPC ' + vpc_id + 'is attached to the internet gateway ' + igw_id) # Create a public subnet public_subnet_response = vpc.create_subnet(vpc_id, '10.0.1.0/24') print('Subnet created for VPC ' + vpc_id + ': ' + str(public_subnet_response)) # Create a public route table public_route_table_response = vpc.create_public_route_table(vpc_id) print("public_route_table_response " + str(public_route_table_response)) rtb_id = public_route_table_response['RouteTable']['RouteTableId'] # Adding IGW to the public route table vpc.create_igw_route_to_route_table(rtb_id, igw_id) public_subnet_id = public_subnet_response['Subnet']['SubnetId'] # Associate subnet with the route table vpc.associate_subnet_with_route_table(subnet_id=public_subnet_id, rtb_id=rtb_id) # Allow auto-assign public ip addresses for subnet vpc.allow_auto_assign_ip_addresses_for_subnet(subnet_id=public_subnet_id) # Create private subnet private_subnet_response = vpc.create_subnet(vpc_id, cidr_block='10.0.2.0/24') private_subnet_id = private_subnet_response['Subnet']['SubnetId'] print('Created private subnet ' + private_subnet_id + ' for VPC ' + vpc_id) # Add name tag to the private subnet vpc.add_name_tag(private_subnet_id, 'Boto3-Private-Subnet') # Add name tag to the public subnet vpc.add_name_tag(public_subnet_id, 'Boto3-Public-Subnet') # Create EC2 instances ec2 = EC2(ec2_client) # Create EC2 key pair key_pair_name = 'Boto3_EC2_Key_Pair' key_pair_response = ec2.create_key_pair(key_pair_name) print('Created key pair with the name: ' + key_pair_name + ' and the response is ' + str(key_pair_response)) # Create Security Group public_security_group_name = 'Boto3-Public-SG' public_security_group_description = 'public security group for the public subnet' public_security_group_response = ec2.create_security_group( public_security_group_name, public_security_group_description, vpc_id) print('Created security group ' + public_security_group_name + ' with response ' + str(public_security_group_response)) # Add rule to the security group public_security_group_id = public_security_group_response['GroupId'] add_inbound_rule_response = ec2.add_inbound_rule_to_security_group( public_security_group_id) print('Added public access rule to the security group ' + public_security_group_name) user_data = """#!/bin/bash yum update -y yum install httpd -y service httpd start chkconfig httpd on echo "<html><body>Welcome to boto3</body></html>" > /var/www/html/index.html """ image_id = 'ami-02bcbb802e03574ba' launch_ec2_instance_response_public_subnet_response = ec2.launch_ec2_instance( image_id=image_id, min_count=1, max_count=1, key_name=key_pair_name, subnet_id=public_subnet_id, security_group_id=public_security_group_id, user_data=user_data) print('Launching instance in public subnet with AMI ' + image_id + ' and instanceId' + launch_ec2_instance_response_public_subnet_response['Instances'][0] ['InstanceId']) private_security_group_name = 'Boto3-Private-SG' private_security_group_description = 'Private Security Group for Private Subnet' private_security_group_response = ec2.create_security_group( private_security_group_name, private_security_group_description, vpc_id) private_security_group_id = private_security_group_response['GroupId'] ec2.add_inbound_rule_to_security_group(private_security_group_id) # Launch a private EC2 instance launch_ec2_instance_response_in_private_subnet_response = ec2.launch_ec2_instance( image_id=image_id, min_count=1, max_count=1, key_name=key_pair_name, subnet_id=private_subnet_id, security_group_id=private_security_group_id, user_data=user_data) print('Launching instance in private subnet with AMI ' + image_id + ' and instanceId' + launch_ec2_instance_response_in_private_subnet_response['Instances'] [0]['InstanceId'])
def main(): #create a vpc ec2_client = EC2Client().get_client() vpc = VPC(ec2_client) vpc_response = vpc.create_vpc() print("VPC created:" + str(vpc_response)) #add name tag to VPC vpc_name = "Boto3-VPC" vpc_id = vpc_response["Vpc"]["VpcId"] vpc.add_name_tag(vpc_id, vpc_name) print("Added " + vpc_name + " to " + vpc_id) #Create IGW igw_response = vpc.create_internet_gateway() #to attach IGW to VPC then see methof in vpc file igw_id = igw_response["InternetGateway"]["InternetGatewayId"] vpc.attach_igw_to_vpc(vpc_id, igw_id) #create a public subnet public_subnet_reponse = vpc.create_subnet(vpc_id, "10.0.1.0/24") public_subnet_id = public_subnet_reponse["Subnet"]["SubnetId"] print("Subnet created for VPC " + vpc_id + ":" + str(public_subnet_reponse)) #add name tag to public subnet vpc.add_name_tag(public_subnet_id, "Boto3-Public-Subnet") #create a public route table public_route_table_response = vpc.create_public_route_table(vpc_id) rtb_id = public_route_table_response["RouteTable"]["RouteTableId"] #Adding IGW to the public route table vpc.create_igw_route_to_public_route_table(rtb_id, igw_id) #Associate public subnet to route table vpc.associate_subnet_with_route_table(public_subnet_id, rtb_id) #allow auto assign public ip addresses for subnet vpc.allow_auto_assign_ip_addresses_for_subnet(public_subnet_id) #create a private subnet private_subnet_response = vpc.create_subnet(vpc_id, "10.0.2.0/24") private_subnet_id = private_subnet_response["Subnet"]["SubnetId"] print("Created private subnet " + private_subnet_id + " for VPC " + vpc_id) #add name tag to private subnet vpc.add_name_tag(private_subnet_id, "Boto3-Private-Subnet") #EC2 instnaces remmeber ec2_client is the client for VPC. ec2 = EC2(ec2_client) #create keypair. key_pair_name = "Boto3-Keypair" key_pair_response = ec2.create_key_pair(key_pair_name) print("Created Key Pair with Name " + key_pair_name + ":" + str(key_pair_response)) #create a security group public_security_group_name = "Boto3-Public-SG" public_security_group_descritpion = "Public Security Group for Public Subnet Internet Access" public_security_group_response = ec2.create_security_group( public_security_group_name, public_security_group_descritpion, vpc_id) public_security_group_id = public_security_group_response["GroupId"] # add public access to SG ec2.add_inbound_rule_to_sg(public_security_group_id) print("Added public access rule to SG " + public_security_group_name) # create startup script user_data = """#!/bin/bash yum update -y yum install httpd24 -y service httpd start chkconfig httpd on echo "<html><body><h1>Hello from <b>Boto3</b> using python!</h1></body></html>" > /var/www/html/index.html""" #launch public instance ami_id = "ami-1b316af0" ec2.launch_ec2_instance(ami_id, key_pair_name, 1, 1, public_security_group_id, public_subnet_id, user_data) print("Launching public EC2 instance using AMI 1b316af0") #for private instance #adding another security group for private ec2 instance private_security_group_name = "Boto3-Private_SG" private_security_group_description = "Private Security Group for Private Subnet" private_security_group_response = ec2.create_security_group( private_security_group_name, private_security_group_description, vpc_id) private_security_group_id = private_security_group_response["GroupId"] #adding rule to SG ec2.add_inbound_rule_to_sg(private_security_group_id) #launch instance ec2.launch_ec2_instance(ami_id, key_pair_name, 1, 1, private_security_group_id, private_subnet_id, """""")
def main(): # Create a VPC ec2_client = EC2Client().get_client() vpc = VPC(ec2_client) vpc_response = vpc.create_vpc() print('VPC created:' + str(vpc_response)) # Add name tag to VPC vpc_name = 'Boto3-VPC' vpc_id = vpc_response['Vpc']['VpcId'] vpc.add_name_tag(vpc_id, vpc_name) print('Added ' + vpc_name + ' to ' + vpc_id) # Create an IGW igw_response = vpc.create_internet_gateway() igw_id = igw_response['InternetGateway']['InternetGatewayId'] vpc.attach_igw_to_vpc(vpc_id, igw_id) # Create a public subnet public_subnet_response = vpc.create_subnet(vpc_id, '10.0.1.0/24') public_subnet_id = public_subnet_response['Subnet']['SubnetId'] print('Subnet created for VPC ' + vpc_id + ':' + str(public_subnet_response)) # Add name tag to Public Subnet vpc.add_name_tag(public_subnet_id, 'Boto3-Public-Subnet') # Create a public route table public_route_table_response = vpc.create_public_route_table(vpc_id) rtb_id = public_route_table_response['RouteTable']['RouteTableId'] # Adding the IGW to public route table vpc.create_igw_route_to_public_route_table(rtb_id, igw_id) # Associate Public Subnet with Route Table vpc.associate_subnet_with_route_table(public_subnet_id, rtb_id) # Allow auto-assign public ip addresses for subnet vpc.allow_auto_assign_ip_addresses_for_subnet(public_subnet_id) # Create a Private Subnet private_subnet_response = vpc.create_subnet(vpc_id, '10.0.2.0/24') private_subnet_id = private_subnet_response['Subnet']['SubnetId'] print('Created private subnet ' + private_subnet_id + ' for VPC ' + vpc_id) # Add name tag to private subnet vpc.add_name_tag(private_subnet_id, 'Boto3-Private-Subnet') # EC2 Instances ec2 = EC2(ec2_client) # Create a key pair key_pair_name = 'Boto3-KeyPair' key_pair_response = ec2.create_key_pair(key_pair_name) print('Created Key Pair with name ' + key_pair_name + ':' + str(key_pair_response)) # Create a Security Group public_security_group_name = 'Boto3-Public-SG' public_security_group_description = 'Public Security Group for Public Subnet Internet Access' public_security_group_response = ec2.create_security_group( public_security_group_name, public_security_group_description, vpc_id) public_security_group_id = public_security_group_response['GroupId'] # Add Public Access to Security Group ec2.add_inbound_rule_to_sg(public_security_group_id) print('Added public access rule to Security Group ' + public_security_group_name) user_data = """#!/bin/bash yum update -y yum install httpd24 -y service httpd start chkconfig httpd on echo "<html><body><h1>Hello from <b>Boto3</b> using Python!</h1></body></html>" > /var/www/html/index.html""" #ami_id = 'ami-1b316af0' ami_id = 'ami-0c7fd3c68f6c6073f' # Launch a public EC2 Instance ec2.launch_ec2_instance(ami_id, key_pair_name, 1, 1, public_security_group_id, public_subnet_id, user_data) print('Launching Public EC2 Instance using AMI ' + ami_id) # Adding another Security Group for Private EC2 Instance private_security_group_name = 'Boto3-Private-SG' private_security_group_description = 'Private Security Group for Private Subnet' private_security_group_response = ec2.create_security_group( private_security_group_name, private_security_group_description, vpc_id) private_security_group_id = private_security_group_response['GroupId'] # Add rule to private security group ec2.add_inbound_rule_to_sg(private_security_group_id) # Launch a private EC2 Instance ec2.launch_ec2_instance(ami_id, key_pair_name, 1, 1, private_security_group_id, private_subnet_id, """""") print('Launching Private EC2 Instance using AMI' + ami_id)
def main(): # Create an EC2 Client Connection to AWS ec2_client = EC2Client().get_client() # VPC vpc = VPC(ec2_client) # Create the VPC vpc_response = vpc.create_vpc() print('VPC created: ' + str(vpc_response)) # Add name tag to the VPC vpc_name = 'Sam-VPC' vpc_id = vpc_response['Vpc']['VpcId'] vpc.add_name_tag(vpc_id, vpc_name) print('Added ' + vpc_name + ' tag to ' + vpc_id + ' VPC') # Create an IGW igw_response = vpc.create_igw() igw_id = igw_response['InternetGateway']['InternetGatewayId'] # Attaching the IGW to the VPC vpc.attach_igw_to_vpc(igw_id, vpc_id) # Create public subnet public_subnet_response = vpc.create_subnet(vpc_id, '10.0.1.0/24') print('Public Subnet Created: ' + str(public_subnet_response)) public_subnet_id = public_subnet_response['Subnet']['SubnetId'] # Name the public subnet public_subnet_name = 'Sam-Public-Subnet' vpc.add_name_tag(public_subnet_id, public_subnet_name) # Create a RT for the Public subnets, reserving default RT for Private subnets public_rt_response = vpc.create_public_rt(vpc_id) print('Public RT created: ' + str(public_rt_response)) rt_id = public_rt_response['RouteTable']['RouteTableId'] # Adding default route pointing to the IGW in Public RT vpc.add_def_route(rt_id, igw_id) # Associate Public Subnet with Public RT vpc.associate_subnet_with_rt(public_subnet_id, rt_id) # Allow auto-assign public ip for subnet vpc.allow_auto_assign_ip_address_for_subnet(public_subnet_id) # Create private subnet private_subnet_response = vpc.create_subnet(vpc_id, '10.0.2.0/24') print('Private Subnet Created: ' + str(private_subnet_response)) private_subnet_id = private_subnet_response['Subnet']['SubnetId'] # Name the private subnet private_subnet_name = 'Sam-Private-Subnet' vpc.add_name_tag(private_subnet_id, private_subnet_name) # EC2 Instances ec2 = EC2(ec2_client) # Create a Key Pair key_pair_name = 'sam-key' key_pair_response = ec2.create_key_pair(key_pair_name) print('Created Key Pair with name ' + key_pair_name + ': ' + str(key_pair_response)) # Create SG for Public EC2 Instance public_sg_name = 'Sam-Public-SG' public_sg_desc = 'Public SG' public_sg_response = ec2.create_sg(public_sg_name, public_sg_desc, vpc_id) public_sg_id = public_sg_response['GroupId'] # Adding Inbound HTTP and SSH Rules to Public SG ec2.add_inbound_rule_to_sg(public_sg_id) # Creating User Data for Public EC2 Instance user_data = """#!/bin/bash yum update -y yum install -y httpd systemctl enable httpd systemctl start httpd echo "<html><body><h1>Welcome to Sam Web-Page</h1></body></html>" > /var/www/html/index.html""" # Deploy Public EC2 Instance ami_id = 'ami-0e6d2e8684d4ccb3e' print("Deploying Public EC2 Instance") ec2.deploy_ec2_instance(ami_id, key_pair_name, 1, 1, public_sg_id, public_subnet_id, user_data) # Create SG for Private EC2 Instance private_sg_name = 'Sam-Private-SG' private_sg_desc = 'Private SG' private_sg_response = ec2.create_sg(private_sg_name, private_sg_desc, vpc_id) private_sg_id = private_sg_response['GroupId'] # Add Inbound HTTP and SSH Rules to Private SG ec2.add_inbound_rule_to_sg(private_sg_id) # Deploy Private EC2 Instance print("Deploying Private EC2 Instance") ec2.deploy_ec2_instance('ami-0e6d2e8684d4ccb3e', key_pair_name, 1, 1, private_sg_id, private_subnet_id, """""")
def main(): ec2_client = EC2Client().get_client() vpc = VPC(ec2_client) vpc_response = vpc.create_vpc() print('vpc created' + str(vpc_response)) # Add name tag to VPC vpc_name = 'Boto3-VPC' vpc_id = vpc_response['Vpc']['VpcId'] vpc.add_name_tag(vpc_id, vpc_name) print("Added" + vpc_name + "to" + vpc_id) # Add IGW igw_response = vpc.create_internet_gateway() igw_id = igw_response['InternetGateway']['InternetGatewayId'] vpc.attach_igw_to_vpc(vpc_id, igw_id) # Creat a public subnet public_subnet_response = vpc.create_subnet(vpc_id, '10.10.1.0/24') public_subnet_id = public_subnet_response['Subnet']['SubnetId'] print("Subnet created for " + vpc_id + ":" + str(public_subnet_response)) # Add name tag to public subnet ID vpc.add_name_tag(public_subnet_id, 'Boto3-PublicSubnet') # Create public route table public_route_table_response = vpc.create_public_route_table(vpc_id) rtb_id = public_route_table_response['RouteTable']['RouteTableId'] vpc.create_igw_route_to_public_route_table(rtb_id, igw_id) # Associate Public Subnet with Route Table vpc.associate_subnet_with_route_table(public_subnet_id, rtb_id) # Allow auto assign public IP address for subnet vpc.allow_auto_assign_ip_addresses_for_subnet(public_subnet_id) # Create private subnet private_subnet_response = vpc.create_subnet(vpc_id, '10.10.2.0/24') private_subnet_id = private_subnet_response['Subnet']['SubnetId'] print("Create private subnet " + private_subnet_id + " for vpc " + vpc_id) # Add name tag to private subnet vpc.add_name_tag(private_subnet_id, 'Boto3-PrivateSubnet') # EC2 Instances ec2 = EC2(ec2_client) print("Creating EC2 instance.....") # Creating a key pair key_pair_name = 'Boto3-KeyPair' key_pair_response = ec2.create_key_pair(key_pair_name) print("Create Key Pair with name " + key_pair_name + " : " + str(key_pair_response)) # Create a security group public_security_group_name = "Boto3-Public-SG" public_security_group_description = "Public SG for public subnet internet access" public_security_group_response = ec2.create_security_group( public_security_group_name, public_security_group_description, vpc_id) public_security_group_id = public_security_group_response['GroupId'] # Add public access to security group ec2.add_inbound_rule_to_sg(public_security_group_id) print("Added public access rule group") user_data = """#!/bin/bash apt-get update apt-get upgrade apt-get install nginx -y service nginx restart""" ami_id = 'ami-03f0fd1a2ba530e75' # Launch a public EC2 instance ec2.launch_ec2_instance(ami_id, key_pair_name, 1, 1, public_security_group_id, public_subnet_id, user_data) print("Launching public ec2 instance...") # Adding another SG for private EC2 instance private_security_group_name = "Boto3-Private-SG" private_security_group_description = "Private SG for private private instance" private_security_group_response = ec2.create_security_group( private_security_group_name, private_security_group_description, vpc_id) private_security_group_id = private_security_group_response['GroupId'] # Add rule to private security group ec2.add_inbound_rule_to_sg(private_security_group_id) # Launch private EC2 instance ec2.launch_ec2_instance(ami_id, key_pair_name, 1, 1, private_security_group_id, private_subnet_id, '')
def main(): # Create a VPC ec2_client = EC2Client().get_client() vpc = VPC(ec2_client) vpc_response = vpc.create_vpc() print('VPC created:' + str(vpc_response)) # Add name tag to VPC vpc_name = 'Boto3-VPC' vpc_id = vpc_response['Vpc']['VpcId'] vpc.add_name_tag(vpc_id, vpc_name) print('Added ' + vpc_name + ' to ' + vpc_id) # Create an IGW igw_response = vpc.create_internet_gateway() #igw_response_2 = vpc.create_internet_gateway() igw_id = igw_response['InternetGateway']['InternetGatewayId'] vpc.attach_igw_to_vpc(vpc_id, igw_id) # Create a public subnet public_subnet_response = vpc.create_subnet(vpc_id, '10.0.1.0/24') #public_subnet_response_2 = vpc.create_subnet(vpc_id, '10.0.2.0/24') public_subnet_id = public_subnet_response['Subnet']['SubnetId'] #public_subnet_id_2 = public_subnet_response_2['Subnet']['SubnetId'] print('Subnet created for VPC ' + vpc_id + ':' + str(public_subnet_response)) #print('Subnet created for VPC ' + vpc_id + ':' + str(public_subnet_response_2)) # Add name tag to Public Subnet vpc.add_name_tag(public_subnet_id, 'Public-Subnet-1') #vpc.add_name_tag(public_subnet_id_2, 'Public-Subnet-2') # Create a public route table public_route_table_response = vpc.create_public_route_table(vpc_id) #public_route_table_response_2 = vpc.create_public_route_table(vpc_id) rtb_id = public_route_table_response['RouteTable']['RouteTableId'] #rtb_id_2 = public_route_table_response_2['RouteTable']['RouteTableId'] # Adding the IGW to public route table vpc.create_igw_route_to_public_route_table(rtb_id, igw_id) #vpc.create_igw_route_to_public_route_table(rtb_id_2, igw_id) # Associate Public Subnet with Route Table vpc.associate_subnet_with_route_table(public_subnet_id, rtb_id) #vpc.associate_subnet_with_route_table(public_subnet_id_2, rtb_id_2) # Allow auto-assign public ip addresses for subnet vpc.allow_auto_assign_ip_addresses_for_subnet(public_subnet_id) #vpc.allow_auto_assign_ip_addresses_for_subnet(public_subnet_id_2) # Create a Private Subnet #private_subnet_response = vpc.create_subnet(vpc_id, '10.0.2.0/24') #private_subnet_id = private_subnet_response['Subnet']['SubnetId'] #print('Created private subnet ' + private_subnet_id + ' for VPC ' + vpc_id) # Add name tag to private subnet #vpc.add_name_tag(private_subnet_id, 'Boto3-Private-Subnet') # EC2 Instances ec2 = EC2(ec2_client) # Create a key pair key_pair_name = 'Boto3-KeyPair' f = open( "C:\\Users\\Neeraj Kunder\\PycharmProjects\\AWS\\boto3keypair.pem", "w") key_pair_response = ec2.create_key_pair(key_pair_name) boto3keypair = str(key_pair_response['KeyMaterial']) f.write(str(boto3keypair)) print('Created Key Pair with name ' + key_pair_name + ':' + str(key_pair_response)) # Create a Security Group public_security_group_name = 'Boto3-Public-SG' public_security_group_description = 'Public Security Group for Public Subnet Internet Access' public_security_group_response = ec2.create_security_group( public_security_group_name, public_security_group_description, vpc_id) public_security_group_id = public_security_group_response['GroupId'] # Add Public Access to Security Group ec2.add_inbound_rule_to_sg(public_security_group_id) print('Added public access rule to Security Group ' + public_security_group_name) user_data = """#!/bin/bash yum update -y yum install -y httpd service httpd start chkconfig httpd on echo "<html><body><h1> Hello! <b>This is my USA Web Server</b> created using Python! </h1></body></html>" > /var/www/html/index.html""" ami_id = 'ami-0de53d8956e8dcf80' #us-east-1 # Launch a public EC2 Instance ec2.launch_ec2_instance(ami_id, key_pair_name, 1, 1, public_security_group_id, public_subnet_id, user_data) #ec2.launch_ec2_instance(ami_id, key_pair_name, 1, 1, public_security_group_id, public_subnet_id_2, user_data_2) print('Launching Public EC2 Instance using AMI ' + ami_id) input("Press Enter to Continue")
def main(): # create a VPC ec2_client = EC2Client().get_client() vpc = VPC(ec2_client) vpc_response = vpc.create_vpc() print("VPC created: " + str(vpc_response)) #Add name tag to VPC vpc_name = 'Boto3_VPC' vpc_id = vpc_response['Vpc']['VpcId'] vpc.add_name_tag(vpc_id, vpc_name) print('Added ' + vpc_name + ' to ' + vpc_id) #create an Internet Gateway ig_response = vpc.create_internet_gateway() ig_id = ig_response['InternetGateway']['InternetGatewayId'] #attaching internet gateway to vpc vpc.attach_ig_to_vpc(vpc_id, ig_id) #Create a public subnet public_subnet_response = vpc.create_subnet(vpc_id, "10.0.1.0/24") public_subnet_id = public_subnet_response['Subnet']['SubnetId'] print('Subnet created for VPC ' + vpc_id + " : " + str(public_subnet_response)) # Add name tag to public subnet vpc.add_name_tag(public_subnet_id, "Boto3-Public-Subnet") #Create a public route table public_route_table_response = vpc.create_public_route_table(vpc_id) #Adding Internet Gateway to Public Route Table rt_id = public_route_table_response['RouteTable']['RouteTableId'] vpc.create_ig_route_to_public_route_table(rt_id, ig_id) # Associate public subnet with route table vpc.associate_subnet_with_route_table(public_subnet_id, rt_id) #Allow auto-assign public ip addresses for subnet vpc.allow_auto_assign_ip_addresses_for_subnet(public_subnet_id) #Create a private subnet private_subnet_response = vpc.create_subnet(vpc_id, '10.0.2.0/24') private_subnet_id = private_subnet_response['Subnet']['SubnetId'] print("Created private subnet " + private_subnet_id + " for VPC " + vpc_id) #Add name tag to private subnet vpc.add_name_tag(private_subnet_id, "Boto3-Private-Subnet") #EC2 Instances ec2 = EC2(ec2_client) #Create a key pair key_pair_name = "Boto3-KeyPair" key_pair_response = ec2.create_key_pair(key_pair_name) print("Created key pair with name " + key_pair_name + " : " + str(key_pair_response)) # Create a Security Group public_security_group_name = "Boto3-Public-SG" public_security_group_description = "Public Security Group for Public Subnet Internet Access" public_security_group_response = ec2.create_security_group( public_security_group_name, public_security_group_description, vpc_id) #Add public access to security group public_security_group_id = public_security_group_response['GroupId'] # Add rule to public security group ec2.add_inbound_rule_to_sg(public_security_group_id) print("Added public access rule to security group " + public_security_group_name) user_data = """#!/bin/bash yum update -y yum install httpd24 -y service httpd start chkconfig httpd on echo "<html><body><h1>Hello from <b>Boto3</b> using Python! </h1></body></html>" > /var/www/html/index.html""" ami_id = 'ami-xxxxxxx' #Launching public EC2 Instance ec2.launch_ec2_instance(ami_id, key_pair_name, 1, 1, public_security_group_id, public_subnet_id, user_data) print("Launching public EC2 Instance using AMI ami-ddddddddd") #Adding another security group for private EC2 Instance private_security_group_name = "Boto3-Private-SG" private_security_group_description = "Private Security Group for private subnet" private_security_group_response = ec2.create_security_group( private_security_group_name, private_security_group_description, vpc_id) private_security_group_id = private_security_group_response['GroupId'] #Add rule to private security group ec2.add_inbound_rule_to_sg(private_security_group_id) #Launching private EC2 Instance ec2.launch_ec2_instance(ami_id, key_pair_name, 1, 1, private_security_group_id, private_subnet_id, """""") print("Launching private EC2 Instance using AMI ami-dddddddddd")
def main(): # Create a VPC # get ec2 client so that we can pass it into our vpc class ec2_client = EC2Client().get_client() # now get the vpc class and pass in the ec2_client vpc = VPC(ec2_client) vpc_response = vpc.create_vpc( '10.221.0.0/16') # creates a dictionary response print(f"VPC Created: {vpc_response}") # Add Name Tag to the VPC vpc_name = "Boto3-VPC" vpc_id = vpc_response['Vpc'][ 'VpcId'] # extracting the vpcid value from the dict vpc.add_name_tag(vpc_id, vpc_name) print(f"Added {vpc_name} to {vpc_id}") # Create an IGW igw_response = vpc.create_internet_gateway() # need to get IGW ID to attach it to the VPC igw_id = igw_response['InternetGateway']['InternetGatewayId'] # now attach it to our VPC vpc.attach_igw_to_vpc(vpc_id, igw_id) # create a public subnet public_subnet_response = vpc.create_subnet(vpc_id, '10.221.1.0/24') print(f"subnet created created for {vpc_id}: {public_subnet_response}") # Were going to use the default created VPC route table for the private subnet # and now create a new route table for the public subnet just created public_route_table_response = vpc.create_public_route_table(vpc_id) # to get the RouteTableID - see create_inernet_gateway boto3 documentation rtb_id = public_route_table_response['RouteTable']['RouteTableId'] # to get SubnetId of the subnet created - see create_subnet boto3 documentation public_subnet_id = public_subnet_response['Subnet']['SubnetId'] # add name tag to public subnet vpc.add_name_tag(public_subnet_id, "Boto3 Public Subnet") # adding the IGW to the public route table (the IGW has already been attached to the vpc) vpc.create_igw_route_for_public_route_table(rtb_id, igw_id) # associate subnet (public or private) with route table just created vpc.associate_subnet_with_route_table(public_subnet_id, rtb_id) # allow auto-assign of public ip addresses to instances in the public subnet vpc.allow_auto_assign_publicip_for_public_subnet(public_subnet_id) # create a private subnet # currently it will be associated with the default vpc route table which doesn't have the IGW as the default gateway private_subnet_response = vpc.create_subnet( vpc_id, '10.221.10.0/24') # remember the boto3 client connection returns dicts private_subnet_id = private_subnet_response['Subnet']['SubnetId'] print(f"Created Private Subnet {private_subnet_id} for VPC {vpc_id}") # add name tag to private subnet # this uses the add_name_tag() method already defined vpc.add_name_tag(private_subnet_id, "Boto3 Private Subnet")
def main(): # Create a VPC ec2_client = Ec2Client().get_client() vpc = VPC(ec2_client) vpc_response = vpc.create_vpc() print('VPC Created' + str(vpc_response)) # Add a name tag to VPC vpc_name = 'test-vpc-1' vpc_id = vpc_response['Vpc']['VpcId'] vpc.add_name_tag(vpc_id, vpc_name) print('Added ' + vpc_name + ' to ' + vpc_id) # Create an IGW igw_response = vpc.create_internet_gateway() igw_id = igw_response['InternetGateway']['InternetGatewayId'] vpc.attach_igw_to_vpc(vpc_id, igw_id) print('Attached Internet gateway' + igw_id + ' to vpc :' + vpc_id) # Create a public subnets public_subnet_response = vpc.create_Subnet(vpc_id, '10.0.1.0/24', 'us-east-1a') public_subnet_id1 = public_subnet_response['Subnet']['SubnetId'] print('Public Subnet created for vpc ' + vpc_id + ' : subnet id ' + public_subnet_id1) # Add a name tag to public subnet public_subnet_name1 = 'PublicSubnet1' vpc.add_name_tag(public_subnet_id1, public_subnet_name1) public_subnet_response = vpc.create_Subnet(vpc_id, '10.0.3.0/24', 'us-east-1c') public_subnet_id2 = public_subnet_response['Subnet']['SubnetId'] print('Public Subnet created for vpc ' + vpc_id + ' : subnet id ' + public_subnet_id2) # Add a name tag to public subnet public_subnet_name2 = 'PublicSubnet2' vpc.add_name_tag(public_subnet_id2, public_subnet_name2) public_subnet_response = vpc.create_Subnet(vpc_id, '10.0.5.0/24', 'us-east-1f') public_subnet_id3 = public_subnet_response['Subnet']['SubnetId'] print('Public Subnet created for vpc ' + vpc_id + ' : subnet id ' + public_subnet_id3) # Add a name tag to public subnet public_subnet_name3 = 'PublicSubnet3' vpc.add_name_tag(public_subnet_id3, public_subnet_name3) # Create a route table public_route_table_response = vpc.create_public_route_table(vpc_id) print('Route table created for vpc ' + vpc_id) rtb_id = public_route_table_response['RouteTable']['RouteTableId'] # Adding a public Route add_public_route_response = vpc.create_igw_route_to_public_route_table( rtb_id, igw_id) # Associating subnet with public route table associate_route_tab_response = vpc.associate_subnet_with_route_table( public_subnet_id1, rtb_id) associate_route_tab_response = vpc.associate_subnet_with_route_table( public_subnet_id2, rtb_id) associate_route_tab_response = vpc.associate_subnet_with_route_table( public_subnet_id3, rtb_id) # Allow auto-assign public ip in public subnet resources allow_auto_assign_ip_address_for_subnet_response = vpc.allow_auto_assign_ip_address_for_subnet( public_subnet_id1) allow_auto_assign_ip_address_for_subnet_response = vpc.allow_auto_assign_ip_address_for_subnet( public_subnet_id2) allow_auto_assign_ip_address_for_subnet_response = vpc.allow_auto_assign_ip_address_for_subnet( public_subnet_id3) # Create a private subnet private_subnet_response = vpc.create_Subnet(vpc_id, '10.0.2.0/24', 'us-east-1b') private_subnet_id = private_subnet_response['Subnet']['SubnetId'] print('Private Subnet created for vpc ' + vpc_id + ' : subnet id ' + private_subnet_id) # Add a name tag to private subnet private_subnet_name = 'PrivateSubnet' vpc.add_name_tag(private_subnet_id, private_subnet_name)