Exemple #1
0
def get_values(event):
    region = None
    if 'region' in event:
        region = event['region']

    modules_process = []
    
    if 'custom' not in event:
        beanstalk = Beanstalk()
        modules_process.append(beanstalk.run(region))

        ec2 = EC2()
        modules_process.append(ec2.run(region))

        rds = RDS()
        modules_process.append(rds.run(region))

        ssm = SSM()
        modules_process.append(ssm.run(region))

        ses = SES()
        modules_process.append(ses.run(region))

        elasticache = Elasticache()
        modules_process.append(elasticache.run(region))
    else:
        data = str(event['custom'])

        if 'BEANSTALK' in data:
            beanstalk = Beanstalk()
            modules_process.append(beanstalk.run(region))

        if 'INSTANCES' in data:
            ec2 = EC2()
            modules_process.append(ec2.run(region))

        if 'SSM' in data:
            ssm = SSM()
            modules_process.append(ssm.run(region))

        if 'ELASTICACHE' in data:
            elasticache = Elasticache()
            modules_process.append(elasticache.run(region))

        if 'RDS' in data:
            rds = RDS()
            modules_process.append(rds.run(region))

        if 'SES' in data:
            ses = SES()
            modules_process.append(ses.run(region))


    start_all(modules_process)
    wait_all(modules_process)
    ret_dict = read_all(modules_process)

    return ret_dict
    def get_aws_describe_instances(self):

        ec2_list = []
        session = boto3.Session()
        ec2 = session.resource('ec2')
        instances = ec2.instances.filter()

        for instance in instances:
            availability_zone = instance.placement["AvailabilityZone"]
            state = instance.state['Name']
            tags = instance.tags[0]['Value']
            try:
                account_number = instance.network_interfaces_attribute[0][
                    'OwnerId']
                account_name = Account.map_account_name_to_account_number(
                    account_number)
                pu = Account.map_pu_to_account(account_number)
            except Exception as e:
                pass

            if state == "running":

                ec2 = EC2(availability_zone, instance.id,
                          instance.instance_type, instance.launch_time, state,
                          instance.ebs_optimized, tags, account_number, pu,
                          account_name)
                ec2_list.append(ec2)

        return ec2_list
Exemple #3
0
    def test_node_property(self, boto3, node_ssh_commands):
        subject = EC2('Amazon')

        subject.node.shell()

        self.mock_node_ssh_commands.assert_called_with(subject.get_external_ip)
        self.mock_node_ssh_commands.return_value.shell.assert_called_with()
Exemple #4
0
def populateregion(profilename, regionname):

    region = Region(regionname)
    region.name = regionname

    vpcs = Vpc.loaddata(profilename, regionname)

    eips = ElasticIp.loaddata(profilename, regionname)
    instances = EC2.loaddata(profilename, regionname)

    region.elasticips = eips
    region.instances = instances
    region.vpcs = vpcs

    subnets = Subnet.loaddata(profilename, regionname)
    region.subnets = subnets

    loadbalancers = ElasticLoadBalancer.loaddata(profilename, regionname)
    region.elasticloadbalancers = loadbalancers

    region.linksubnetstovpcs()

    # writetoyaml(account)

    return region
Exemple #5
0
def describe_instances():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)

    ec2_response = ec2.describe_ec2_instances()

    print(str(ec2_response))
Exemple #6
0
    def test_get_external_ip(self, boto3, printing, time, mocker):
        subject = EC2('Thor')

        result = subject.get_external_ip()

        assert self.mock_print.mock_calls == [
            mocker.call(
                '\t\tWaiting for external IP for Thor EC2 instance...'),
            mocker.call('\t\tdone.')
        ]
        self.mock_time.sleep.assert_called_with(1)
        assert result == '1.2.3.4'
Exemple #7
0
def exec_action(action, env):
    """
    :param access_key:
    :param secret_key:
    :param action: start or stop
    :param env: env name
    :return: None
    """
    ec2 = EC2(action, env)
    ec2.exec_ec2_action()

    return None
Exemple #8
0
def main():
    logging.basicConfig(
        format=
        "%(asctime)-15s %(levelname)s %(filename)s:%(funcName)s#%(lineno)d - %(message)s",
        level=logging.ERROR)

    logger = logging.getLogger("app")
    logger.setLevel(logging.DEBUG)
    logger.info("started")

    ec2 = EC2(region=REGION)
    ec2.generateCSV()

    logger.info("finished")
Exemple #9
0
    def test_restart_instance(self, mocker, printing, time, boto3):
        subject = EC2('Thor')

        subject.restart_instance()

        assert self.mock_print.mock_calls == [
            mocker.call('\tRestarting Thor EC2 instance'),
            mocker.call(
                "\t\tWaiting for Thor EC2 instance to reach a state in ['running', 'stopped']..."
            ),
            mocker.call('\t\tdone.')
        ]
        self.mock_time.sleep.assert_called_with(1)
        self.mock_boto3.client.return_value.reboot_instances.assert_called_with(
            InstanceIds=['foo'])
Exemple #10
0
    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        VPC = ec2.Vpc(self,
                      "vpc",
                      cidr="10.0.0.0/22",
                      max_azs=2,
                      nat_gateways=1)

        if self.node.try_get_context("envoy_ec2"):
            print("Will deploy the Envoy@EC2 stack")
            EC2(self, VPC)

        if self.node.try_get_context("envoy_eks") or self.node.try_get_context(
                "ingress"):
            print("Will create the EKS Cluster")
            EKS(self, VPC)
Exemple #11
0
    def get_aws_describe_instances(self,account):
        
        ec2_list = []
        session = boto3.Session()
        ec2 = session.resource('ec2')
        instances = ec2.instances.filter()

        for instance in instances:
            availability_zone = instance.placement["AvailabilityZone"]           
            state = instance.state['Name']                                                             
            
            if state == "running":
           
                ec2 = EC2(availability_zone, instance.id, instance.instance_type, instance.launch_time, state,  instance.ebs_optimized, account.account_number, account.department, account.account_name)
                ec2_list.append(ec2)

        return ec2_list    
Exemple #12
0
    def create_db_security_group_and_add_rules(self):
        ec2_client = EC2Client().get_client()
        ec2 = EC2(ec2_client)

        # create security group
        security_group = ec2.create_security_group()

        # get id of the sg
        security_group_id = security_group['GroupId']

        print("Created RDS security group with id " + security_group_id)

        # add public access rule to sg
        ec2.add_inbound_rule_to_sg(security_group_id)

        print("Added inbound public access rule to sg with id " + security_group_id)

        return security_group_id
Exemple #13
0
    def create_db_security_group_and_rules(self):
        ec2_client = ECSClient().get_client()
        ec2 = EC2(ec2_client)

        # Security group
        security_group = ec2.create_security_group()

        # get id of sg
        security_group_id = security_group['GroupId']

        print(f"created RD with security group {security_group_id}")

        # add public access rule to sg
        ec2.add_inbound_rule_to_sg(security_group_id)
        print(
            f'Added inbound public access rule to sg with id ={security_group_id}'
        )

        return security_group_id
Exemple #14
0
def main():
    template = Template()
    template.add_description("Example Server")

    for key, value in Mappings().mappings.iteritems():
        template.add_mapping(key, value)

    parameters = Parameters()
    for param in parameters.values():
        template.add_parameter(param)

    template.add_metadata({
        "AWS::CloudFormation::Interface": {
            "ParameterGroups": [
                {
                    "Label": {
                        "default": "Required parameters."
                    },
                    "Parameters": [
                        "DBPassword",
                        "KeyPair",
                    ]
                },
                {
                    "Label": {
                        "default": "Advanced: Database and instance"
                    },
                    "Parameters": [
                        "DBInstanceType", "DBStorageSize", "DBBackupRetention",
                        "EC2InstanceType"
                    ]
                },
            ],
            "ParameterLabels": {
                "DBPassword": {
                    "default": "Choose a database password"
                },
                "DBStorageSize": {
                    "default": "Database storage (advanced)"
                },
                "DBBackupRetention": {
                    "default": "How long to keep backups (advanced)"
                },
                "DBInstanceType": {
                    "default": "Database instance class (advanced)"
                },
                "KeyPair": {
                    "default": "Choose a key pair"
                },
                "EC2InstanceType": {
                    "default": "Instance class (advanced)"
                },
            }
        }
    })

    vpc = VPC()
    for res in vpc.values():
        template.add_resource(res)

    elb = LoadBalancer(vpc=vpc)
    for res in elb.values():
        template.add_resource(res)

    db = Database(parameters=parameters, vpc=vpc, loadbalancer=elb)
    for res in db.values():
        template.add_resource(res)

    ec2 = EC2(parameters=parameters, vpc=vpc, loadbalancer=elb)
    for res in ec2.values():
        template.add_resource(res)

    template.add_output(
        Output("LoadBalancerDNSName",
               Value=GetAtt(elb.load_balancer, "DNSName")))

    print(template.to_json())
Exemple #15
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'

    # 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)
Exemple #16
0
def terminate_instance():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)

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

    ec2.start_instance('i-049f27f4be7441270')
Exemple #18
0
def modify_instance():
    ec2_client = EC2Client().get_client()
    ec2 = EC2(ec2_client)

    ec2.modify_ec2_instance('i-01560b6cd12a884a1')
Exemple #19
0
    def test_construction(self, boto3):
        subject = EC2('Thor')

        self.mock_boto3.client.assert_called_with('ec2', EC2_CONFIG['region'])
        assert subject.name == 'Thor'
        assert subject.ip == ''
Exemple #20
0
load_balancer_name = 'my-lb'

key_pair_obj_nv = KeyPair(ec2_nv_key_name, 'us-east-1', session)
key_pair_obj_oh = KeyPair(ec2_oh_key_name, 'us-east-2', session)
try:
    key_pair_obj_nv.create()
except:
    print("key pair already exists in North Virginia")
try:
    key_pair_obj_oh.create()
except:
    print("key pair already exists in Ohio")

## Ohio __init__

ec2_ohio = EC2(ubuntu20_ohio_img, ec2_oh_key_name, 't2.micro', 'us-east-2',
               'secgroup-teste', script_database, session)
ohio_subnets_ids, oh_vpc_ids = ec2_ohio.describe_subnets()
print("creating database instance...")
ec2_id_ohio = ec2_ohio.create_instances(1)
print("Status - OK")

## reload é necessário para pegar o ip das instancias
ec2_id_ohio[0].reload()
database_ip = ec2_id_ohio[0].public_ip_address

script_machines = f"""#!/bin/bash
sudo apt update
sudo apt install python3-dev libpq-dev python3-pip -y
cd /home/ubuntu
git clone https://github.com/pedromtelho/tasks.git
sudo sed -i "83 c 'HOST' : '{database_ip}'," tasks/portfolio/settings.py
Exemple #21
0
    def test_dns_property(self, boto3, dns_ssh_commands):
        subject = EC2('Amazon')

        self.mock_dns_ssh_commands.assert_called_with(subject.get_external_ip)