my_aws_api_library module

class my_aws_api_library.MyAws(aws_cr_fl, pb_ky_fl)[source]

Bases: object

create_ec2_instance(customer_id, node_type)[source]

Crete the Ec2 instance, and save the customer-id tag value.

Each instance will have Amazon SSMAgent at launch. Tested only with Amazon Linux Instances (Free Tier).

Each instance have “AmazonEC2RoleforSSM” to build a SSM connection for remote command run.

If node_type “Peer”, Instance is t2.micro and has 10gb EBS disk.

If node_type “Manager”, Instance is t2.medium and has 20gb EBS disk.

Args:

customer_id: The customer id tag value of instance that will be created.

node_type : The node type of instance that will be created. (“Peer” or “Manager”)

Returns:
The created instance id. 0 shows error.
execute_commands_on_linux_instances(commands, instance_ids)[source]

Execute the specified commands on specified Instances using SSM service.

Args:

commands: Command phrases that will be executed

instance_ids: Instances that will run the commands

Returns: List of;
  1. Instances that have SSM agent and are in specified Instances,
  2. Instances that didn’t run the command and are in specified Instances,
  3. Command run outputs.

See also

Amazon EC2 Systems Manager (SSM) requires;

1. An IAM role for EC2 instances that will process commands. There should be a system manager role and the instance should use this role ! (It is done while creation instance)

2. And a separate role for users executing commands. Aws IAM user that has access and secret keys should have ssm permission. (i.e. AmazonSSMFullAccess)

http://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-configuring-access-policies.html

get_all_instances()[source]

List the all Instances according to their sorted Customer ID

Returns:
List of (customer_id, instance_id, instance_state)
get_customer_by_instance_id(ins_id)[source]

When given an instance ID, return the Customer IDs from the Instance’s customer-id tag.

Args:
ins_id: Instance ID

Warning

Assumes that an instance should be owned by only one customer.

Returns:
Customer ID
get_instance_by_customer_id(cid)[source]

When given a Customer ID, return the Instances’s IDs using the Instance’s customer-id tag.

Args:
cid: Customer ID
Returns:
Instance Id list.
get_instance_by_node_type(ntype)[source]

When given an Node Type, return the instance IDs using the Instance’s node-type tag.

Args:
ntype: Instance Node Type
Returns:
Instance Id list
list_backups(node_id)[source]

Return all the backups belongs to specified Instance, with their start-time.

Args:
node_id: Instance ID
Returns:
List of (Snapshot ID, Snapshot Start-time), None if errors.
make_backup(n_id)[source]

Backup the specified Instance’s “/data” mount point.

For clearness, first, script creates a volume for Instance, format it and mount it to /data mount point.

Note

Don’t forget to provide an IAM role (policy –> AmazonEC2RoleforSSM) for the Instance and permission (AmazonSSMFullAccess) for the user. This script uses SSM send_command !

Args:
n_id: Instance Id that will be backed up.
Returns:
Snapshot Id of the backup or None if errors.
roll_back(back_id, n_id)[source]

Search for the volumes associate with the given snapshot, detach and delete these volumes. Create new volumes from specified snapshot then attach them to the instance. While doing this steps, take into account the mount points.

Args:

back_id: Snapshot ID that will restore

n_id: Instance ID

Returns:
True if success. Otherwise False.
terminate_instances(i_id)[source]

Terminate the specified Instances.

Args:
i_id: Instance Id list. “ALL” means all IDs.
Returns:
Prints the termination output