Infrastructure-as-Code generator CLI
npm install aws-iac-genAWS Infrastructure as Code Generator - A CLI tool for generating CloudFormation templates from existing AWS resources.
aws-iac-gen (or iac-gen for short) helps you generate CloudFormation templates from your existing AWS resources. It uses AWS CloudFormation's resource scanning capabilities to discover resources in your account and convert them into reusable Infrastructure as Code templates.
- Resource Scanning: Scan your AWS account for existing resources
- Resource Discovery: List and select from previous resource scans
- Template Generation: Convert discovered resources into CloudFormation templates
- Stack Filtering: Automatically excludes resources managed by existing CloudFormation stacks
- Real-time Progress: Visual feedback with spinners during long operations
- Resource Limits: Built-in warnings and errors for AWS limits (500 resources per template)
``bash`
npm install -g aws-iac-gen
Or install from source:
`bash`
git clone https://github.com/krzmknt/aws-iac-gen.git
cd aws-iac-gen
npm install
npm run build
npm link
- Node.js 18 or higher
- AWS CLI configured with appropriate credentials
- AWS permissions for CloudFormation resource scanning and template generation
#### iac-gen resources
Download AWS resources from a scan.
Options:
- --new-scan - Start a new resource scan--from-scan
- - Choose from existing scans-o, --output
- - Output file name (default: resources.json)
Examples:
`bashStart a new scan and save resources
iac-gen resources --new-scan
####
iac-gen templateGenerate a CloudFormation template from resources.
Options:
-
-i, --input - Input resources file (default: resources.json)
- -o, --output - Output template file (default: template.json)
- --from-stack - Use an existing stack as the template baseExamples:
`bash
Generate template from resources.json
iac-gen templateUse custom input/output files
iac-gen template -i my-resources.json -o my-template.yamlGenerate template from existing stack
iac-gen template --from-stack my-existing-stack
`Workflow Example
1. Scan for resources:
`bash
iac-gen resources --new-scan
`2. Review the resources (optional):
`bash
cat resources.json | jq '.[] | .ResourceType' | sort | uniq -c
`3. Generate CloudFormation template:
`bash
iac-gen template
`4. Deploy the template (using AWS CLI):
`bash
aws cloudformation create-stack \
--stack-name my-new-stack \
--template-body file://template.json
`Important Notes
$3
- AWS CloudFormation supports a maximum of 500 resources per template
- The tool will warn when your scan contains more than 500 resources
- Template generation will fail if you try to process more than 500 resources
$3
- Resources already managed by CloudFormation stacks are automatically filtered out
- The tool shows how many stack-managed resources were excluded
$3
- Only
COMPLETE scans can be used for resource extraction
- The tool will error if you select a scan that is still IN_PROGRESS or FAILEDConfiguration
The tool uses your AWS CLI configuration. Set your region using:
`bash
export AWS_REGION=us-east-1
or
export AWS_DEFAULT_REGION=us-east-1
`Development
`bash
Install dependencies
npm installRun in development mode
npm run dev -- [command] [options]Build
npm run buildLint
npm run lintFormat code
npm run format
`License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Troubleshooting
$3
- Ensure you have run at least one scan using
iac-gen resources --new-scan
- Check that you're in the correct AWS region$3
- Wait for the scan to complete (usually takes a few minutes)
- Resource scans must be in
COMPLETE status to extract resources$3
- Filter your resources before generating a template
- Consider splitting resources into multiple templates
- Remove unnecessary resource types from the JSON file
$3
Ensure your AWS credentials have the following permissions:
-
cloudformation:StartResourceScan
- cloudformation:DescribeResourceScan
- cloudformation:ListResourceScans
- cloudformation:ListResourceScanResources
- cloudformation:CreateGeneratedTemplate
- cloudformation:DescribeGeneratedTemplate
- cloudformation:GetGeneratedTemplate`