-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add readme; exclude org-based services
- Loading branch information
Showing
2 changed files
with
183 additions
and
56 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
local-app/python-tools/aws-service-linked-roles/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| # AWS Service Linked Role to Terraform Generator | ||
|
|
||
| A Python-based utility to audit AWS Service-Linked Roles (SLRs) and generate Terraform import blocks and variable files. This tool is designed to help teams transition existing, manually-created service-linked roles into Terraform management using `for_each` patterns. | ||
|
|
||
| ## How It Works | ||
|
|
||
| The script performs the following steps: | ||
| 1. **AWS Audit**: Connects to your AWS account and lists all IAM roles located under the `/aws-service-role/` path. | ||
| 2. **Partition & Account Detection**: Dynamically identifies whether the account is in the `aws` (Commercial) or `aws-us-gov` (GovCloud) partition via STS to ensure generated ARNs are correct. | ||
| 3. **Principal Extraction**: Inspects the Trust Relationship of each role to identify the specific AWS Service Principal (e.g., `s3.amazonaws.com`). | ||
| 4. **Code Generation**: | ||
| - Generates an `import.tf` file containing Terraform `import` blocks using the correct partition-aware ARN format. | ||
| - Generates a `.tfvars` file containing a list of service names. | ||
|
|
||
| ## Installation | ||
|
|
||
| ### Prerequisites | ||
| - Python 3.8+ | ||
| - AWS CLI configured with permissions: `iam:ListRoles`, `iam:GetRole`, and `sts:GetCallerIdentity`. | ||
|
|
||
| ### Setup | ||
| 1. Save the script as `aws-slr-generate.py`. | ||
| 2. Install dependencies: | ||
| ```bash | ||
| pip install boto3 | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Basic Usage | ||
| ```bash | ||
| python aws-slr-generate.py | ||
| ``` | ||
|
|
||
| ### Advanced Usage (GovCloud / Custom Files) | ||
| ```bash | ||
| python aws-slr-generate.py --profile gov-admin --region us-gov-west-1 --import-file slr_imports.tf | ||
| ``` | ||
|
|
||
| ### Dry Run | ||
| ```bash | ||
| python aws-slr-generate.py --dry-run | ||
| ``` | ||
|
|
||
| ### Usage with Filtering | ||
| To run the script while excluding specific organization-level services and respecting existing Terraform tags: | ||
|
|
||
| ```bash | ||
| # Using the default internal ignore list | ||
| python aws-slr-generate.py --profile my-profile | ||
| # Using a custom CSV list of services to ignore | ||
| python aws-slr-generate.py --organization-services org-managed-list.csv | ||
| ``` | ||
|
|
||
| ### Filtering Logic | ||
| The script now applies a three-tier filter before creating an import: | ||
| 1. **Tag Check**: Looks for `boc:created_by: terraform`. | ||
| 2. **Service Check**: Compares the service principal against the internal `DEFAULT_IGNORE_SERVICES` list. | ||
| 3. **Manual Overrides**: Includes any services found in the optional `--organization-services` CSV. | ||
|
|
||
| ### Command Line Arguments | ||
| - `--profile`: AWS CLI profile to use. | ||
| - `--region`: AWS region. | ||
| - `--import-file`: Filename for import blocks (default: `import.tf`). | ||
| - `--variables-file`: Filename for tfvars (default: `variables.service-linked-roles.auto.tfvars`). | ||
| - `--dry-run`: Output to terminal instead of files. | ||
| - `--organization-services`: Organization managed service linked roles (excluded from import and list) | ||
|
|
||
| ## Terraform Integration | ||
|
|
||
| ```hcl | ||
| resource "aws_iam_service_linked_role" "roles" { | ||
| for_each = toset(var.service_linked_roles) | ||
| aws_service_name = each.key | ||
| } | ||
| ``` | ||
|
|
||
| ## Changelog | ||
|
|
||
| ### [1.0.5] - 2026-04-14 | ||
| * **Added**: Built-in ignore list for organization-managed services (e.g., GuardDuty, Security Hub, SSO). | ||
| * **Added**: Support for `--organization-services` CSV file to dynamically expand the ignore list. | ||
| * **Added**: Tag-based filtering. Roles with the tag `boc:created_by = terraform` are now skipped to prevent duplicate management. | ||
| * **Fixed**: ARN generation logic. The script now uses the full role ARN returned by AWS instead of a constructed string, ensuring the role name is included in the Terraform `import` block. | ||
| * **Improved**: Added inline comments in the generated `import.tf` file to explain why specific roles were skipped. | ||
|
|
||
| ### [1.0.4] - 2026-04-14 | ||
| * **Fixed**: Dynamic Partition Detection. Replaced the hardcoded `aws` partition with a dynamic lookup via STS. | ||
| * **Improved**: The script now correctly generates `arn:aws-us-gov` for GovCloud environments and `arn:aws` for commercial environments. | ||
| * **Updated**: Improved `get_session_info` function to reliably extract partition data from the caller identity ARN. | ||
|
|
||
| ### [1.0.3] - 2026-04-14 | ||
| - **Added**: Dynamic Partition Detection (supports `aws`, `aws-us-gov`, etc.). | ||
| - **Updated**: Renamed script to `aws-slr-generate.py` in documentation. | ||
| - **Improved**: ARN generation logic to use detected partition. | ||
|
|
||
| ### [1.0.2] - 2026-04-14 | ||
| - **Added**: `--dry-run` option. | ||
| - **Improved**: Summary output formatting. | ||
|
|
||
| ### [1.0.1] - 2026-04-14 | ||
| - **Added**: CLI arguments for profile, region, and custom filenames. | ||
| - **Added**: Automatic Account ID detection. | ||
|
|
||
| ### [1.0.0] - 2026-04-14 | ||
| - **Initial Release**: Basic SLR scraping and file generation. | ||
|
|
131 changes: 75 additions & 56 deletions
131
local-app/python-tools/aws-service-linked-roles/aws-slr-generate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters