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.2.tar.gz (12.6 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.2-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: EC2S3Wrapper-1.0.2.tar.gz
  • Upload date:
  • Size: 12.6 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.2.tar.gz
Algorithm Hash digest
SHA256 34c513bdbe24ba042a080eb6681fb9d480fcfd90dc8afebdcea2d71886d70bb7
MD5 8bc4274ab23e5dc15509a8c8975c273c
BLAKE2b-256 d2b8ebf831e4dbb44335b3cac7793935a3b516bb036e2258bcff97f9d1af6761

See more details on using hashes here.

File details

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

File metadata

  • Download URL: EC2S3Wrapper-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.7 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b1d9fc6d0e449579a2e2034420333ff6abafd8d1e36b9746b374c343e927681a
MD5 5893e562a945f4bdbbb9fbaf7c71c7a4
BLAKE2b-256 c789101d0288e1937141ae5416b3322298abbbecce6743dd6b9a05733a4ee6d3

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