You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add faq about terraformer
* Apply suggestions from code review
Co-authored-by: Thomas Hervé <thomas.herve@datadoghq.com>
* Add section about generation
Co-authored-by: Thomas Hervé <thomas.herve@datadoghq.com>
Terraform supports an out-of-the-box way to import existing resources into your terraform state via the [`terraform import`][1] command.
9
+
This can be done via the `terraform import <resource_type>.<resource_name> <existing_id>`.
10
+
11
+
This approach is `state only` and requires already having the HCL resource fully defined in your terraform configuration files. To import the configuration fully, you can use a tool like Terraformer.
12
+
13
+
## Terraformer
14
+
15
+
The [terraformer project][2] allows you to import a resource as both state and HCL configuration.
16
+
17
+
Once installed, you can setup a terraform directory with a basic `main.tf`
18
+
19
+
This uses terraform 0.13+ syntax, but you can find more configurations on the [official datadog provider docs][3]
20
+
21
+
```hcl
22
+
# main.tf
23
+
24
+
terraform {
25
+
required_providers {
26
+
datadog = {
27
+
source = "DataDog/datadog"
28
+
}
29
+
}
30
+
}
31
+
32
+
# Configure the Datadog provider
33
+
provider "datadog" {
34
+
api_key = var.datadog_api_key
35
+
app_key = var.datadog_app_key
36
+
}
37
+
38
+
variable "datadog_api_key" {
39
+
default = "<YOUR_API_KEY>"
40
+
}
41
+
42
+
variable "datadog_app_key" {
43
+
default = "<YOUR_APPLICATION_KEY>"
44
+
}
45
+
```
46
+
47
+
Then run `terraform init` from within this directory to pull the datadog terraform provider.
48
+
49
+
Now you can use `terraformer` to start importing resources. For example, to import Dashboard `abc-def-ghi` you can run
This generates a folder `generated` that contains both a terraform state file, as well as HCL terraform config files representing the imported resource.
54
+
55
+
```
56
+
generated
57
+
└── datadog
58
+
└── dashboard
59
+
├── dashboard.tf
60
+
├── outputs.tf
61
+
├── provider.tf
62
+
└── terraform.tfstate
63
+
```
64
+
65
+
*`dashboard.tf`: The HCL configuration file for the newly imported dashboard
66
+
*`outputs.tf`: An HCL containing outputs to use potentially in other configurations
67
+
*`provider.tf`: An HCL initialization of the provider, similar to whats in our `main.tf` file
68
+
*`terraform.tfstate`: The terraform state representing the imported dashboard
69
+
70
+
## Other examples of running terraformer
71
+
72
+
All example commands require the `--api-key` and `--app-key` flags.
73
+
74
+
* Import all monitors: `terraformer import datadog --resources=monitor`
75
+
* Import monitor with id 1234: `terraformer import datadog --resources=monitor --filter=monitor=1234`
76
+
* Import monitors with id 1234 and 1234: `terraformer import datadog --resources=monitor --filter=monitor=1234:12345`
77
+
* Import all monitors and dashboards: `terraformer import datadog --resources=monitor,dashboard`
78
+
* Import monitor with id 1234 and dashboard with id abc-def-ghi: `terraformer import datadog --resources=monitor,dashboard --filter=monitor=1234,dashboard=abc-def-ghi`
0 commit comments