Skip to content

Import Orphaned Resources #2

Import Orphaned Resources

Import Orphaned Resources #2

name: Import Orphaned Resources
on:
workflow_dispatch:
inputs:
confirmation:
description: 'Type "IMPORT" to proceed with importing resources'
required: true
default: ''
dry_run:
description: 'Run in dry-run mode (validate only, no actual imports)'
required: false
default: 'true'
type: choice
options:
- 'true'
- 'false'
jobs:
import-resources:
runs-on: ubuntu-latest
if: github.event.inputs.confirmation == 'IMPORT'
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
ref: testgrid
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.5.0
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-south-1
- name: Terraform Init
run: |
cd terraform/implementations/aws/infra
echo "🔄 Initializing Terraform..."
terraform init
- name: Copy and Execute Import Script
run: |
cd terraform/implementations/aws/infra
echo "📝 Copying import script..."
cp ../../../../scripts/import_resources.sh ./
chmod +x import_resources.sh
if [ "${{ github.event.inputs.dry_run }}" == "true" ]; then
echo "🧪 DRY RUN MODE - Validating import commands"
sed 's/terraform import/echo "DRY RUN: terraform import"/g' import_resources.sh > import_dry.sh
chmod +x import_dry.sh
./import_dry.sh
else
echo "🚀 LIVE MODE - Performing actual imports"
./import_resources.sh
echo "📊 Resources in state: \$(terraform state list | wc -l)"
fi
- name: Verify and Commit State
if: github.event.inputs.dry_run != 'true'
run: |
cd terraform/implementations/aws/infra
if [ -f terraform.tfstate ]; then
echo "✅ State file created: \$(du -h terraform.tfstate)"
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add -f terraform.tfstate
git commit -s -m "Import orphaned resources - nginx server (15.206.88.253) and all infrastructure"
git push origin testgrid
echo "✅ State file committed successfully"
fi
- name: Summary
run: |
echo "=================================================="
if [ "${{ github.event.inputs.dry_run }}" == "true" ]; then
echo "🧪 DRY RUN COMPLETED - No changes made"
echo "Ready for live import (set dry_run=false)"
else
echo "✅ IMPORT COMPLETED - All 61 resources imported"
echo "✅ NGINX server (15.206.88.253) is now manageable"
echo "✅ Infrastructure under terraform control"
fi
echo "=================================================="