aws-samples / aws-samples/aws-organizations-tool

Implement AWS Organizations close-account functionality

Open
#2 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
18
Forks
3
PR merge metrics
No merged PRs in 30d

Description

# Implement AWS Organizations `close-account` functionality

## Summary
Add support for the AWS Organizations `close-account` API to enable permanent account closure directly from the orgtool CLI, complementing the existing account lifecycle management capabilities.

## Background
Currently, orgtool provides comprehensive account management including creation, updates, tagging, and organizational unit placement. However, it lacks the ability to permanently close accounts using the AWS Organizations `close-account` API.

**Current gap:**
- Accounts can only be removed from configuration (becomes "unmanaged")
- No direct way to close/suspend accounts permanently
- Manual AWS CLI intervention required for account closure

## Proposed Solution

### 1. CLI Command
Add new command to `orgtoolaccounts`:
```bash
orgtoolaccounts close --config --account-name [--exec] [-q] [-d|-dd]
```

### 2. Configuration Management
Add new command to `orgtoolconfigure`:
```bash
orgtoolconfigure account close --config --account-name [--exec] [-q] [-d|-dd]
```

### 3. Implementation Details

**Core functionality:**
- Call AWS Organizations `close_account()` API
- Update account status tracking in configuration
- Handle asynchronous operation status checking
- Provide clear warnings about permanent nature

**Safety features:**
- Confirmation prompts for destructive operation
- Dry-run mode by default (requires `--exec`)
- Rate limiting awareness (10% of accounts per 30 days)
- Pre-flight checks for delegated administrator roles

## Technical Implementation

### Files to modify:
1. **`orgtool/accounts.py`**
- Add `close_account()` function
- Update CLI usage documentation
- Add status checking functionality

2. **`orgtool/configure.py`**
- Add `account_close()` method to Configure class
- Update CLI usage documentation
- Add to main() command routing

3. **`orgtool/orgs.py`**
- Handle SUSPENDED accounts in organization management
- Update account status reporting

### Example implementation:
```python
def close_account(org_client, args, log, account_name):
"""Close an AWS account permanently"""
# Get account ID
account_id = lookup(deployed_accounts, 'Name', account_name, 'Id')

# Safety checks
if not account_id:
log.error(f"Account '{account_name}' not found")
return

# Confirmation
log.warning(f"DESTRUCTIVE OPERATION: Closing account '{account_name}' ({account_id})")
log.warning("Account will be SUSPENDED and deleted after 90 days")

if args['--exec']:
try:
org_client.close_account(AccountId=account_id)
log.info(f"Account closure initiated for '{account_name}'")
log.info("Check status with: orgtoolaccounts report")
except Exception as e:
log.error(f"Failed to close account: {e}")
else:
log.info("DRY RUN: Use --exec to actually close the account")
```

## Benefits

1. **Complete Account Lifecycle**: Create → Manage → Close accounts entirely within orgtool
2. **Infrastructure as Code**: Account closure becomes part of declarative configuration
3. **Safety & Auditability**: Proper logging, dry-run mode, and confirmation prompts
4. **Consistency**: Matches existing orgtool command patterns and safety practices

## Requirements

- Organization must be in "All Features" mode
- Must be executed from management account
- Cannot close management account itself
- Respects AWS rate limits (10% of accounts per 30 days)

## Acceptance Criteria

- [ ] `orgtoolaccounts close` command implemented
- [ ] `orgtoolconfigure account close` command implemented
- [ ] Proper error handling and validation
- [ ] Dry-run mode support (default behavior)
- [ ] Clear warnings about permanent nature
- [ ] Status checking and reporting
- [ ] Documentation updates
- [ ] Unit tests for new functionality
- [ ] Integration with existing account management workflows

## Related Issues

- Complements existing account deletion from configuration (#TBD)
- Enhances account lifecycle management capabilities
- Addresses gap in AWS Organizations API coverage

## Priority
**Medium** - Valuable feature for complete account lifecycle management, but not critical for core functionality.

## Issue Type
**Feature**

## Labels
`enhancement`

Contributor guide

Open the contributing guide

Research direction

Read orgtool/accounts.py, orgtool/configure.py, and orgtool/orgs.py, then inspect the existing account lifecycle commands and tests. Use the proposed orgtoolaccounts close and orgtoolconfigure account close entry points as the starting scope. Done means both commands support safe dry runs, validation, closure status handling, documentation, and unit tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python
Domain
cli, cloud
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.