# Importing Resources

Source: https://docs.ops0.com/docs/discovery/importing

After scanning, generate Terraform code for discovered resources and import them into IaC management. ops0 supports AI-assisted and direct code generation with multiple output formats.

## Import Workflow

### Select Resources
Choose resources to import from the discovery session.

### Organize into Projects
Group selected resources into discovery projects by environment, application, or team.

### Choose Generation Mode
Select AI-assisted or direct Terraform generation.

### Configure State Backend
Set up the Terraform state backend (S3, Azure Blob, GCS, or local).

### Review Code
Review the generated Terraform code before finalizing.

### Import
Create an IaC project, download as ZIP, or push directly to GitHub.

---

## Generation Modes

![ops0 IaC code generation from discovered resources](https://docs.ops0.com/images/discovery-iac-generation.png)

### AI-Assisted Generation

AI-powered code generation that produces optimized, production-ready Terraform:

- Real-time security scanning during generation
- Event streaming shows generation progress
- Intelligent module grouping by resource type
- Automatic variable extraction for configurable values
- Best-practice patterns applied (naming, tagging, dependencies)

The AI generation uses streaming output so you can watch the code being generated in real time. If issues are detected during security scanning, they appear inline with suggestions.

### Direct Generation

Generate Terraform code without AI, with progress tracking from 10 to 100 percent:

| Output Mode | Files Produced | Use Case |
|-------------|----------------|----------|
| **Single file** | `main.tf` | Quick imports, small resource sets |
| **Modular project** | Organized into modules by resource type | Large environments, team collaboration |
| **Flat import** | `main.tf` + `variables.tf` + `outputs.tf` | Standard Terraform import workflow |

---

## Resource Status

| Status | Meaning |
|--------|---------|
| **Unmanaged** | Not in any Terraform state; available for import |
| **Managed** | Already managed by Terraform; skip or re-import |

---

## Filtering Resources

| Filter | Description |
|--------|-------------|
| **Type** | Filter by resource type (EC2, VPC, RDS, etc.) |
| **Region** | Filter by cloud region/location |
| **Tags** | Filter by resource tags (Environment, Owner, etc.) |
| **Search** | Filter by name or resource ID |

---

## State Backend Configuration

![Configure Terraform state backend for imported resources](https://docs.ops0.com/images/discovery-configure-backend.png)

Configure where Terraform stores state for the imported resources:

| Backend | Configuration |
|---------|--------------|
| **S3** | Bucket, key, region, optional DynamoDB table for locking |
| **Azure Blob** | Storage account, container, key |
| **GCS** | Bucket, prefix |
| **Local** | Local filesystem path (single user only) |

**Recommended**

Use S3, Azure Blob, or GCS for team environments. Local backends are suitable for individual testing only.

---

## Generated Code

ops0 produces Terraform files with proper resource references and dependency ordering:

| File | Contents |
|------|----------|
| `main.tf` | Resource definitions with actual cloud configuration |
| `variables.tf` | Variable declarations for configurable values |
| `outputs.tf` | Output values for important resource attributes |
| `imports.tf` | Import blocks for Terraform 1.5+ |

### Example: Generated Import Block

```hcl
import {
  to = aws_vpc.main
  id = "vpc-0abc123def456"
}

resource "aws_vpc" "main" {
  cidr_block           = "10.0.0.0/16"
  enable_dns_hostnames = true
  enable_dns_support   = true

  tags = {
    Name        = "production-vpc"
    Environment = "production"
  }
}
```

### Modular Output Structure

When using modular generation, code is organized by resource type:

```
project-name/
├── main.tf
├── variables.tf
├── outputs.tf
├── imports.tf
└── modules/
    ├── networking/
    │   ├── vpc.tf
    │   ├── subnets.tf
    │   └── security_groups.tf
    ├── compute/
    │   └── instances.tf
    └── database/
        └── rds.tf
```

---

## Import Destinations

After code generation, choose where to send the produced Terraform:

![Link discovered resources to an IaC project in ops0](https://docs.ops0.com/images/discovery-link-iacproject.png)

- **Create IaC Project**: Create a new project in ops0 with the generated code already in place.

- **Download ZIP**: Download all generated files as a ZIP archive for local review or handoff.

- **Push to GitHub**: Sync the generated code directly to a connected GitHub repository.

---

## GitHub Sync

Push generated Terraform code directly to a connected GitHub repository:

### Select Repository
Choose a connected GitHub repository from the GitHub sync modal.

### Choose Branch
Select an existing branch or create a new one.

### Set Path
Configure the directory path within the repository.

### Configure commit options
Enter a commit message (pre-filled with discovery context). Optionally check **Create Pull Request** to push to a new branch and open a PR for review instead of pushing directly.

### Push
ops0 commits and pushes the generated code to the repository.

---

## Regeneration

![Discovery import complete — IaC project created from discovered resources](https://docs.ops0.com/images/discovery-complete.png)

If the initial generation doesn't meet your needs, regenerate the code:

- Adjust generation settings (output mode, module structure)
- Re-run AI generation with different parameters
- Regenerate for specific resources while keeping others

---

## Troubleshooting

- **Import Failed**: The resource may have been deleted or permissions may have changed. Re-run discovery to refresh the inventory.

- **Already Managed**: The resource already exists in another Terraform state. Skip it here or remove it from the other state first.

- **Generation Timeout**: Large batches can time out during AI generation. Split the import by resource type or environment.

- **State Lock**: Another process is holding the Terraform state lock. Wait for it to clear or force unlock if the lock is stale.

**Importing Best Practices**

- Import incrementally instead of bringing everything in at once.
- Group related resources by environment or application.
- Review generated Terraform before deploying.
- Run `terraform plan` after import to verify the result.
- Push to GitHub immediately so the imported state is versioned.

---

## Related

- [Discovery Overview](https://docs.ops0.com/docs/discovery/overview): How discovery works and platform coverage.

- [Scanning Resources](https://docs.ops0.com/docs/discovery/scanning): Configure and run discovery scans.

- [IaC Projects](https://docs.ops0.com/docs/iac/projects): Manage Terraform projects created from discovery imports.
