# IaC Reconciliation

## Datafy and Infrastructure as Code (IaC)

When using **Datafy AutoScaler**, your original EBS volumes are replaced with Datafy-managed volumes. While the OS and applications remain unaffected thanks to Datafy's virtualization layer, Infrastructure as Code (IaC) tools like Terraform may encounter issues.

IaC tools anticipate specific EBS volumes as declared in code, leading to a mismatch between the expected original volume and the actual autoscaling volumes. Potential problems include unnecessary volume recreations, drift detection errors, failure during instance operations, and increased manual reconciliation efforts.

To resolve this, Datafy provides reconciliation plugins for common IaC tools. These plugins ensure that your IaC tool understands and accommodates the state changes introduced by Datafy.

{% hint style="info" %}
Don’t see your tool listed? Let us know—we’re actively expanding support.
{% endhint %}

### Terraform

Datafy provides a custom Terraform provider called [`datafyaws`](https://registry.terraform.io/providers/datafy-io/datafyaws/latest/docs). It integrates seamlessly into your Terraform workflows and ensures that Terraform’s state remains in sync with the actual volumes managed by AutoScaler.

{% hint style="warning" %}
The `datafyaws` provider reconciles the state of volumes that were originally created using Terraform. It cannot reconcile volumes not created or managed by Terraform.
{% endhint %}

#### Getting Started

{% stepper %}
{% step %}
**Install Provider**

Add the provider to your `required_providers` block:

```hcl
terraform {
  required_providers {
    datafyaws = {
      source  = "datafy-io/datafyaws"
      version = "~> 1.0"
      // ADDITIONAL_CONFIGS = "copy values from matching aws provider"
    }
  }
}
```

A `datafyaws` provider needs to be defined for every [AWS provider](https://registry.terraform.io/providers/hashicorp/aws/latest/docs) already defined in your Terraform files.

Make sure to use a version of `datafyaws` that supports the version of the AWS provider you use. See the documentation of the [latest version](https://registry.terraform.io/providers/datafy-io/datafyaws/latest/docs) in the Terraform registry.
{% endstep %}

{% step %}
**Configure Provider**

The `datafyaws` provider should be configured with the same parameters as the existing AWS providers. The `datafy_token` can be set via environment variable or directly in the config:

```hcl
provider "datafyaws" {
  datafy_token  = <TOKEN>
  // ADDITIONAL_CONFIGS = "copy values from matching aws provider"
}
```

{% endstep %}

{% step %}
**Add Provider to Resources**

The following Terraform resource types are supported:

* [`aws_ebs_volume`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ebs_volume)
* [`aws_volume_attachment`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/volume_attachment)
* [`aws_instance`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance)

Add `provider = datafyaws` to each supported resource that is managed by AutoScaler, or associated with a volume managed by AutoScaler:

```hcl
resource "aws_ebs_volume" "example" {
  provider = datafyaws

  availability_zone = "us-east-1a"
  size              = 40
}
```

{% hint style="info" %}
`datafyaws` does not support nested volume declarations. Volumes must be defined separately from the instances to which they are attached.
{% endhint %}
{% endstep %}
{% endstepper %}

#### Limitations

* The provider should only be used with the supported resource types listed above. Other resource types are currently not supported for autoscaling volumes.
* `final_snapshot` is **not supported** and must be explicitly set to `false` when destroying volumes.

#### Removing Datafy Provider

As long as your AWS account is associated with an active Datafy account, there is no need to remove the `datafyaws` provider - it will work in the same manner as the AWS Terraform provider for all non-autoscaling volumes.

If you need to remove the provider, perform the following steps:

1. Turn off autoscaling for all relevant volumes. Make sure the migration is complete before proceeding to the next step.
2. Run `terraform apply` with `datafyaws` provider for all relevant resources. This will update the Terraform state.
3. Remove `datafyaws` from relevant resources. You can now continue to use Terraform as desired.


---

# 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://docs.datafy.io/set-up-and-installation/iac-reconciliation.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.
