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-0.0.9.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.9-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: EC2S3Wrapper-0.0.9.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.9.tar.gz
Algorithm Hash digest
SHA256 d7fc8777d395d80e2b360d54759475e5784633b8210fb60d81a2fc5202fefc66
MD5 d8842657a96d6f1223ff21fa70158f33
BLAKE2b-256 2be738196f852e5c44fd0ffc6c5b1d7bb482e4517f3921e0a777cc5404e81e7c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: EC2S3Wrapper-0.0.9-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.9-py3-none-any.whl
Algorithm Hash digest
SHA256 a6c6cef266239ea45b9e207dc8dee3627067d74c82c8144d8f6b07cc76c07c2d
MD5 51b49d8d862490a3d0217787c5fc0748
BLAKE2b-256 623eecd7c7e21dec18c06cae803f6ed6b1d2dfbb54334e523fa7b46e8a0c0421

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