def terminate_instance():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)

    ec2_response = ec2.terminate_instance('i-xxxxxxxxxx')

    print(str(ec2_response))
예제 #2
0
def describe_instances():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)

    ec2_response = ec2.describe_ec2_instances()

    print(str(ec2_response))
def start_instance():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)

    ec2_response = ec2.start_instance('i-xxxxxxxxxx')

    print(str(ec2_response))
예제 #4
0
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))
예제 #5
0
def fetch_ip():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)
    ec2_response = ec2.describe_ec2_instances()
    ip_addr = ec2_response['Reservations'][0]['Instances'][0][
        'PublicIpAddress']
    print(ip_addr)
    return ip_addr
예제 #6
0
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)
예제 #7
0
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}")
예제 #8
0
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)
예제 #9
0
def terminate_instance():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)

    ec2.terminate_instance('i-03c64711e1adb2c28')
예제 #10
0
def start_instance():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)

    ec2.start_instance('i-03c64711e1adb2c28')
예제 #11
0
def modify_instance():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)

    ec2.modify_ec2_instance('i-03c64711e1adb2c28')
예제 #12
0
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")
예제 #13
0
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")
예제 #14
0
def modify_instance():
    instance_id = 'i-012a14fdc79b7a599'

    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)
    ec2.modify_ec2_instance(instance_id)
예제 #15
0
def terminate_instance():
    ec2_client = EC2Client().get_client()
    # get the ec2 reference
    ec2 = EC2(ec2_client)
    ec2.terminate_instance("i-0cce30cf2bcacf627")
예제 #16
0
def terminate_instance():
    instance_id = 'i-012a14fdc79b7a599'

    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)
    ec2.terminate_ec2_instance(instance_id)
예제 #17
0
def terminate_instance():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)
    
    ec2.terminate_instance('i-015b8168e8a1b0571')
예제 #18
0
def start_instance():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)
    
    ec2.start_instance('i-015b8168e8a1b0571')
예제 #19
0
def modify_instance():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)
    
    ec2.modify_ec2_instance('i-015b8168e8a1b0571')
예제 #20
0
def start_instance():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)

    ec2.start_instance('i-049f27f4be7441270')
예제 #21
0
def terminate_instance():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)

    ec2.terminate_instance('i-049f27f4be7441270')
예제 #22
0
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)
예제 #23
0
def modify_instance():
    ec2_client = EC2Client().get_client()
    # get the ec2 reference
    ec2 = EC2(ec2_client)
    ec2_response = ec2.modify_ec2_instances("i-054f9e6c3b56d49d1")
예제 #24
0
def modify_instance():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)

    ec2.modify_ec2_instance('i-01560b6cd12a884a1')
예제 #25
0
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,
                            """""")
예제 #26
0
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, """""")
예제 #27
0
def start_instance():
    instance_id = 'i-012a14fdc79b7a599'

    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)
    ec2.start_ec2_instance(instance_id)
예제 #28
0
from src.ec2.ec2 import EC2
from src.ec2.elb import ELB
from src.client_locator import EC2Client

"""
This Script is for ap-southeast-1 region (Singapore). If you use for another Region, you need to change the availability
zones of Subnets,VPC Peering Connection Region Id,AMI Image Id. For the numbers of EC2 instance to be launch you need to
specify the MinCount and MaxCount 
"""
print("""       ##########################################################
                #                                                        #           
                #  DEPLOYING AWS INFRASTRUCTURE ... Amazon Web Service.  #
                #                                                        #
                ##########################################################      """)

ec2_client = EC2Client().get_client()
vpc = VPC(ec2_client)

cidrblock_1 = '10.10.0.0/16'
vpc1_response = vpc.create_vpc(cidrblock_1)

vpc1_id = vpc1_response['Vpc']['VpcId']
vpc1_name = 'vpc01'
vpc.add_tag(vpc1_id,vpc1_name)

igw_response = vpc.create_igw()
igw_id = igw_response['InternetGateway']['InternetGatewayId']

vpc.attach_igw(igw_id,vpc1_id)

#Public Subnet
예제 #29
0
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'])
예제 #30
0
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, '')