Skip to content

Commit

Permalink
fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
morga471 committed Sep 30, 2025
1 parent 0c9f3c9 commit c5a5f8e
Show file tree
Hide file tree
Showing 21 changed files with 2,220 additions and 1,460 deletions.
187 changes: 118 additions & 69 deletions local-app/python-tools/gfl-resource-actions/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
# AWS Resource Management Tool

A Python-based tool for discovering, starting, stopping, and managing AWS resources across multiple accounts, primarily designed for GovCloud environments to help with cost management.

## Overview
The AWS Resource Management Tool is a Python-based utility designed to discover, start, and stop AWS resources across multiple accounts, primarily in GovCloud environments. Its primary purpose is cost management by automating resource state management.

## Features
- Multi-account and multi-region support
- Resource discovery and management for EC2, RDS, EKS, EMR, and ECR
- AWS SSO and role-based authentication
- CLI for easy interaction
- Dry-run mode for safe testing
- CSV reporting for resource actions
This tool helps you manage AWS resources across multiple accounts and regions by allowing you to:

1. Discover resources of different types (EC2, RDS, EKS, EMR, ECR)
2. Start or stop resources on demand
3. Export resource information to CSV files
4. Automatically delete old ECR images

The tool is designed to work efficiently with large AWS organizations by implementing parallel processing of accounts and intelligent caching to minimize API calls.

## Supported Resource Types

- **EC2**: EC2 instances can be started/stopped
- **RDS**: RDS database instances can be started/stopped
- **EKS**: EKS clusters can be started/stopped
- **EMR**: EMR clusters can be started/stopped
- **ECR**: ECR images can be discovered and old images deleted (based on age)

## Package Structure
```
Expand All @@ -32,70 +42,109 @@ aws_resource_management/ # Main package

## Installation

1. Clone the repository:
```bash
git clone <repository-url>
cd <repository-folder>
```
### Prerequisites

- Python 3.8 or newer
- AWS credentials with appropriate permissions
- For organization management: OrganizationsReadOnlyAccess permission
- For resource management: appropriate permissions to start/stop resources

2. Install dependencies:
```bash
make install-dev
```
### Setup

1. Clone the repository
2. Install the package:

```bash
cd /path/to/gfl-resource-actions
pip install -e .
```

## Usage

### CLI Commands
The tool exposes a CLI through `aws-resource-mgmt` with the following options:

- `--start`/`--stop`: Specify the action to perform.
- `--resource-type`: Select resource type (ec2, rds, eks, emr, ecr, all).
- `--region`/`--exclude-region`: Specify regions to include or exclude.
- `--account`/`--exclude-account`: Specify accounts to include or exclude.
- `--dry-run`: Simulate actions without making changes.

#### Examples
- Run in dry-run mode:
```bash
python -m aws_resource_management.cli --stop --dry-run --resource-type ec2
```

- Stop all resources:
```bash
python -m aws_resource_management.cli --stop --resource-type all
```

- Start EC2 instances only:
```bash
python -m aws_resource_management.cli --start --resource-type ec2
```

## Development

### Code Quality
- Run linters:
```bash
make lint
```
- Format code:
```bash
make format
```

### Testing
- Run tests:
```bash
make test
```

### Build Distribution
- Build the package:
```bash
make dist
```

## Contributing
Contributions are welcome! Please see the `CONTRIBUTING.md` file for guidelines.
### Command Line Interface

The tool provides a command-line interface with the following main options:

```bash
# Stop resources with dry run
aws-resource-mgmt --stop --dry-run --resource-type ec2

# Start resources in specific regions
aws-resource-mgmt --start --resource-type rds --region us-gov-east-1 --region us-gov-west-1

# Export resources to CSV
aws-resource-mgmt --export --resource-type all --csv-output-dir ./exports

# Filter by specific account
aws-resource-mgmt --stop --resource-type eks --account 123456789012

# Stop old ECR images
aws-resource-mgmt --stop --resource-type ecr --csv-export --csv-output-dir ./
```

### Key Command Arguments

- `--stop` / `--start` / `--export`: Action to perform
- `--resource-type`: Type of resources to manage (ec2, rds, eks, emr, ecr, all)
- `--region`: AWS regions to target (can be specified multiple times)
- `--exclude-region`: AWS regions to exclude (can be specified multiple times)
- `--account`: Specific account ID to target
- `--exclude-account`: Account IDs to exclude (can be specified multiple times)
- `--dry-run`: Simulate actions without making changes
- `--csv-export`: Export discovered resources to CSV files
- `--csv-output-dir`: Directory to save CSV exports
- `--partition`: AWS partition to operate in (aws, aws-us-gov, aws-cn)

## Authentication

The tool supports multiple authentication methods:

1. **AWS Organizations**: By default, uses the current session to access AWS Organizations API and manage resources across accounts
2. **AWS Profiles**: Use the `--profile` option to specify an AWS profile from ~/.aws/config
3. **Role Assumption**: Automatically assumes appropriate roles in target accounts (OrganizationAccountAccessRole or AWSControlTowerExecution)

## Resource Management

### EC2 Instances

EC2 instances can be started or stopped. The tool intelligently handles instances in Auto Scaling groups.

### RDS Instances

RDS database instances can be started or stopped.

### EKS Clusters

EKS clusters can be discovered and managed. Worker nodes are handled separately as EC2 instances.

### EMR Clusters

EMR clusters can be discovered and controlled.

### ECR Images

ECR repositories and images can be discovered. Old images (by default, older than 365 days) can be deleted.

## CSV Export

Resources can be exported to CSV files with detailed information:

```bash
aws-resource-mgmt --export --resource-type all --csv-output-dir ./exports
```

## Configuration

The tool uses a combination of command-line arguments and configuration files to control its behavior.

## Troubleshooting

Common issues:

1. **Authentication Failures**: Check your AWS credentials and ensure your role has necessary permissions.
2. **Missing Resources**: Verify the resource type and regions specified.
3. **Performance Issues**: For large organizations, try limiting the scope with regions and account filters.

## License
This project is licensed under the MIT License. See the LICENSE file for details.

This project is licensed under the MIT License - see the LICENSE file for details.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@

# Re-export key modules from consolidated utility modules
from aws_resource_management.utils.logging_utils import (
LoggingContext,
configure_logging,
log_operation,
log_with_context,
setup_logging,
)

from aws_resource_management.utils.file_utils import (
initialize_action_log_csv as initialize_csv_log,
log_action_to_csv,
)

# Set up a default logger
logger = setup_logging()

Expand Down
Loading

0 comments on commit c5a5f8e

Please sign in to comment.