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_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
```python
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-0.0.8.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

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

EC2S3Wrapper-0.0.8-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for EC2S3Wrapper-0.0.8.tar.gz
Algorithm Hash digest
SHA256 e789b1da3451c7486a9942ea405795462b62d60b6258cfa6419154669902722c
MD5 5deef9f9ce0795b0d1a0edf4bf2c965a
BLAKE2b-256 a06bc7cedf5e32ac3ff9252d20a268b7cfb441eaaa4ddf320377c9c03674ee1d

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for EC2S3Wrapper-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 8e205076c31525ff52f1762dd2d8cf26a5865cb69d128d9420c7abfa1b5d0c4b
MD5 19cc6503bca831a536ed277ddb4f6886
BLAKE2b-256 7ad2f6f1f254d15aa0c4b4a59d2b429e9f5f5b7dfa572b8c82cf6c4dd8172f5d

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