# Importing ilert UI Resources into Terraform State

When working with with Terraform you have the option to create resources (managed by Terraform) or data sources (managed by ilert and read-only accessed by your Terraform config).

Often you have setup resources in ilert through API or UI before you start using Terraform to manage your setup and you want to know how to manage these resources as well, without deleting them first.

To do that, we can import resources into your local Terraform config.

{% hint style="success" %}
**Shortcut: Use Export to Terraform in the UI**

ilert can generate the resource block and import command for you automatically. Open any supported resource in the ilert web UI and click the **Export to Terraform** button to get a ready-to-use HCL block and the matching `terraform import` command. See [Export to Terraform](https://docs.ilert.com/developer-docs/terraform/export-to-terraform) for details.
{% endhint %}

## **First step**: Block

Bring the configuration into your Terraform config.

*Imagine we have the user Max Mustermann with the ID 123456 created through the UI in ilert*. To import him into TF, we first setup a block.

```hcl
 resource "ilert_user" "max_mustermann" {
  email              = "max.mustermann@gmail.com"
  first_name         = "Max"
  language           = "de"
  last_name          = "Mustermann"
  region             = "DE"
  role               = "USER"
  timezone           = "Europe/Berlin"
}
```

{% hint style="info" %}
If you dont want to type the block by hand, you can also try to setup an import block for it and use the **-generate-config-out** argument (<https://developer.hashicorp.com/terraform/language/import/generating-configuration>) however we think that setting up the block by hand is faster. Alternatively, use the [**Export to Terraform**](https://docs.ilert.com/developer-docs/terraform/export-to-terraform) button in the ilert UI for supported resource types.
{% endhint %}

## **Second step**: Import

Now with the block setup, we need to tell Terraform to import the existing user from ilert into the block relation for the internal state management (otherwise TF will try to create a new user instead of updating it)

```
terraform import ilert_user.max_mustermann 123456
```

{% hint style="info" %}
Note that while in 99% of the cases, the import keys (identifiers) are the same as the entity’s ID, they sometimes might differ. You can find the import description at the bottom of each resource in [the ilert Terraform provider’s documentation](https://registry.terraform.io/providers/iLert/ilert/latest).
{% endhint %}

After the import was successful, the block is now properly mapped to the identifer and you can continue as usual running `terraform plan` or `terraform apply`.

## FAQ

### **How to figure out the ID of my resource?**

You can either use the API or the UI to find the identifier of your resource:

**In the UI** head to the detail view of your desired resource and copy the id param of your browser's URL:

<figure><img src="https://1534978131-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F684bnaGsqSFjESghpiiu%2Fuploads%2FcZeIQ87fBVgx0aWDW9av%2Fimage.png?alt=media&#x26;token=05b9c947-3693-4774-b58a-5a336680996c" alt=""><figcaption></figcaption></figure>

**For the API**, you may call the GET list resources of your entities e.g. `GET /api/alert-sources` and use the **id** field of the returned objects.

<figure><img src="https://1534978131-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F684bnaGsqSFjESghpiiu%2Fuploads%2FkNtIs5x4XqMAPTvlRcq5%2Fimage.png?alt=media&#x26;token=02a6e7c9-ac1e-44ba-b7f7-3bbb2c007797" alt=""><figcaption></figcaption></figure>

Should you have many entities, we recommend using the *(inofficial)* `?query=` param to search for the name of your desired resource:

<figure><img src="https://1534978131-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F684bnaGsqSFjESghpiiu%2Fuploads%2F38GCC0Hz4vBqsMbysBV4%2Fimage.png?alt=media&#x26;token=3f26e9e6-38b8-4453-a8d5-ef04b247fa48" alt=""><figcaption></figcaption></figure>
