# Codifying Infrastructure

## Overview

The agent can identify unmanaged resources across your connected cloud providers, recommend which ones to bring under management, and handle the entire import process: generating the code, validating it against your live environment, and opening a pull request. You can also ask it to import specific resources directly.

## How it works

Ask the agent to find unmanaged resources, or tell it what to import:

```
Codify the unmanaged S3 buckets in the production account
```

```
Which resources in my AWS account aren't managed by code?
```

The agent then:

1. **Identifies resources** in the [context layer](/docs/getting-started/building-blocks.md#the-context-layer) that match your request
2. **Generates infrastructure code** from the selected resources
3. **Validates the configuration** by running a plan and checking for errors
4. **Iterates** until the plan reaches a no-changes state, confirming the code matches your live infrastructure
5. **Opens a pull request** with the generated code for your review

Each step runs inline in the conversation. You see progress updates, generated code blocks, and a plan summary as the agent works. If it encounters an issue (for example, cross-resource dependencies it cannot resolve), it stops and explains what happened so you can adjust your request.

Once complete, the codified resources appear as managed in the context layer and a pull request is ready for your review on GitHub.

{% hint style="info" %}
Grouping unrelated resources in the same request may cause the import to fail due to dependency conflicts. Stick to resources that belong together.
{% endhint %}

## Reviewing the generated code

After a successful import, you can retrieve the full Terraform code locally using the [Bricks CLI](/docs/bricks-cli/cli-reference/bricks_blueprint/bricks_blueprint_fetch.md):

```
bricks blueprint fetch
```

Once fetched, you can review, modify, and push the code to your Git repositories.

## Importing from the Cloud Graph

You can also start an import from the [Cloud Graph](/docs/orchestration/cloud-graph.md) by selecting unmanaged resources visually instead of through conversation.

### Prerequisites

The collection must have both **discovery permissions** and **orchestration permissions** enabled. Without both, the agent cannot analyze existing resources or execute the generated code.

### How to import from the Cloud Graph

<figure><img src="/files/PiAjXcZkJgIYvAD4qwra" alt=""><figcaption></figcaption></figure>

1. Open the **Cloud Graph**
2. Select a **collection** with orchestration and discovery icons (**O**) (**D**)
3. In the explorer drawer, select one or more **unmanaged resources**
4. Add an **environment name**
5. Click **Import**

After the import begins:

* The drawer switches to the agent tab
* The agent codifies the selected resources, iterating until the code and state are synced
* The agent publishes the blueprint and creates the environment

When complete:

* The generated **blueprint** appears in the [blueprints](/docs/orchestration/packages/blueprints-overview.md) list
* The created **environment** appears in the [environments](/docs/orchestration/environments.md) list
* The Cloud Graph refreshes with the new managed nodes

### Best practices

* Import logically related resources together, for example resources that make up a single workload
* Use clear and descriptive environment names
* Review generated blueprints before applying further changes
* Create smaller, focused blueprints over large, monolithic ones

### Notes and limitations

* Some resources may require manual adjustments after import
* Import does not modify live infrastructure during the process


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bluebricks.co/docs/agent/codifying-infrastructure.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
