Skip to main content

EC2S3Wrapper is a Python package designed to simplify the management of AWS EC2 and S3 services. It provides an easy-to-use interface for common tasks like creating and managing EC2 instances, as well as uploading, downloading, and managing files in S3. Ideal for developers looking to streamline AWS operations with minimal code

Project description

EC2S3Wrapper

This repository provides a Python-based toolkit for managing AWS resources efficiently. The toolkit consists of two main modules: EC2Manager for managing Amazon EC2 instances and S3Manager for managing Amazon S3 buckets and objects. These modules are designed with production-grade best practices, including robust error handling, modular design, and scalability.


Table of Contents

  1. Features
  2. Installation
  3. Modules
  4. Usage
  5. License

Features

  • EC2Manager:

    • Manage EC2 instances: start, stop, terminate, retrieve public IPs.
    • Create and manage key EC2 instances.
    • Monitor EC2 instance states with a timeout feature.
  • S3Manager:

    • Manage S3 buckets: create, list, delete buckets.
    • Upload and download files or entire folders to/from S3.
    • List and delete objects in a bucket.
    • Empty buckets before deletion.

Installation

Prerequisites

  • Python 3.10 or above.
  • AWS credentials configured using the AWS CLI or IAM access keys.

Install Required Libraries

Install the dependencies using pip:

pip install boto3 

To install EC2S3Wrapper, you can use pip:

pip install EC2S3Wrapper

Modules

EC2Manager

Overview: The EC2Manager module simplifies managing EC2 instances. It supports key EC2 operations and integrates robust exception handling.

Key Functions:

  1. list_all_instances: Lists all EC2 instances
  2. start_instance: Starts an EC2 instance by instance ID.
  3. stop_instance: Stops an EC2 instance by instance ID.
  4. terminate_instance: Terminates an EC2 instance by instance ID.
  5. create_key_pair: Creates an EC2 key pair and saves it as a .pem file.
  6. get_instance_public_ip: Fetches the public IP address of an instance.

S3Manager

Overview: The S3Manager module provides tools for managing S3 buckets and their contents. It ensures safe operations with extensive error handling.

Key Functions:

  1. list_buckets: Lists all S3 buckets in the account.
  2. create_bucket: Creates a new S3 bucket.
  3. upload_file: Uploads a file to an S3 bucket.
  4. upload_folder: Recursively uploads a folder and its contents to an S3 bucket.
  5. download_file: Downloads a file from an S3 bucket.
  6. download_s3_folder: Recursively downloads an entire folder from an S3 bucket to a local directory.
  7. list_objects_in_bucket: Lists all objects in an S3 bucket.
  8. delete_objects: Deletes all objects in a bucket.
  9. delete_bucket: Deletes a bucket after ensuring it is empty.

Usage

Setting Up AWS Credentials

AWS credentials can be passed directly or set up in the default AWS CLI profile.

Example Usage

Initialize EC2Manager and S3Manager

from ec2_manager import EC2Manager
from s3_manager import S3Manager

# Initialize EC2Manager
ec2_manager = EC2Manager(
    aws_access_key_id="your_access_key",
    aws_secret_access_key="your_secret_key",
    region_name="us-east-1"
)

# Initialize S3Manager
s3_manager = S3Manager(
    aws_access_key_id="your_access_key",
    aws_secret_access_key="your_secret_key",
    region_name="us-east-1"
)

Working with EC2 Instances

# List all EC2 instances
instances = ec2_manager.list_instances()
print(instances)

# Start an EC2 instance
ec2_manager.start_instance(instance_id="i-0123456789abcdef0")

# Fetch public IP of an instance
public_ip = ec2_manager.get_instance_public_ip(instance_id="i-0123456789abcdef0")
print(f"Public IP: {public_ip}")

Managing S3 Buckets and Objects

# List all S3 buckets
buckets = s3_manager.list_buckets()
print(buckets)

# Create a new bucket
s3_manager.create_bucket(bucket_name="my-new-bucket")

# Upload a file
s3_manager.upload_file(file_path="path/to/local/file.txt", bucket_name="my-new-bucket")

# Download a file
s3_manager.download_file(
    object_name="file.txt",
    bucket_name="my-new-bucket",
    file_path="path/to/local/file.txt"
)

# Delete a bucket
s3_manager.delete_bucket(bucket_name="my-new-bucket")

This toolkit is designed to simplify AWS resource management while ensuring robust and production-grade functionality. Contributions and suggestions are welcome! 😊

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

EC2S3Wrapper-1.0.1.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

EC2S3Wrapper-1.0.1-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file EC2S3Wrapper-1.0.1.tar.gz.

File metadata

  • Download URL: EC2S3Wrapper-1.0.1.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for EC2S3Wrapper-1.0.1.tar.gz
Algorithm Hash digest
SHA256 5b0686da27e925bcd1e4430acc458379023c9d61fc593a1a5d0433a1771fe231
MD5 6efa2195684b802ee9e5257c942b22ab
BLAKE2b-256 16467c655a887b8e05c9b8d78d941221b66188f1cbdf535a0323bc65b72f647d

See more details on using hashes here.

File details

Details for the file EC2S3Wrapper-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: EC2S3Wrapper-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for EC2S3Wrapper-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1174d7081771320e554574aef556f9454c99b1b4aa72401a5464f86547cadd53
MD5 d0909ebaa0ed5d5df7bc33fd3d60531c
BLAKE2b-256 924961e236e9d3c16c9891b35445d3704fa53ae5dccc810c35eae5f5990bff5e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page