Exemple #1
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)
Exemple #2
0
                #                                                        #           
                #  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
public_subnet = vpc.create_subnet(vpc1_id,'apse1-az2','10.10.0.0/20')
public_subnet_id = public_subnet['Subnet']['SubnetId']

#Private Subnet
private_subnet = vpc.create_subnet(vpc1_id,'apse1-az1','10.10.16.0/20')
private_subnet_id = private_subnet['Subnet']['SubnetId']

#Public Route Table
public_route_table = vpc.create_route_table(vpc1_id)
public_route_table_id = public_route_table['RouteTable']['RouteTableId']
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, """""")