Jak to zrobić? #
Całość opiera się na kilku krokach:
- tworzymy podstawowy plik konfiguracyjny terraforma
- importujemy informacje o zasobie do
terraform.tfstate - sprawdzamy różnice między konfiguracją a zaimportowanym stanem
- wprowadzamy poprawki
- wracamy do punktu trzeciego
+------------------------+
| tworzymy konfigurację |
| terraform |
+------------+-----------+
|
V
+------------------------+
| importujemy zasób |
+------------+-----------+
|
V
_/\_
__/ są \__
_/ różnice \_ NIE +------+
/ w stanie i \------>| STOP |
\_konfiguracji_/ +------+
\_ ? _/<-----+
\__ __/ \
\/ \
| \
| TAK \
V |
+------------------------+ |
| wprowadzamy poprawki +---+
+------------------------+
Potrzebne komendy #
# importowanie informacji o klastrze
terraform import aws_ecs_cluster.my_cluster my_cluster
# usunięcie informacji o klastrze
terraform state rm aws_ecs_cluster.my_cluster
# sprawdzenie jak konfiguracja ma się do stanu klastra
terraform plan -target=aws_ecs_cluster.my_cluster
# zastosowanie zmian (jeśli jednak istnieją różnice)
terraform apply -target=aws_ecs_cluster.my_cluster
Przykład #
Import klastra ECS o nazwie default z regionu eu-central-1 na koncie AWS 000000000000.
Konfiguracja terraforma #
Najlepiej zacząć ze wszystkimi wymaganymi atrybutami oraz jak najmniejszą ilością atrybutów opcjonalnych.
resource "aws_ecs_cluster" "my_cluster" {
name = "example"
setting {
name = "containerInsights"
value = "enabled"
}
}
Import stanu #
W komendzie import trzeba użyć nazwy klastra oraz identyfikatora z konfiguracji terraforma.
W moim wypadku to default oraz aws_ecs_cluster.my_cluster.
$ terraform import aws_ecs_cluster.my_cluster default
aws_ecs_cluster.my_cluster: Importing from ID "default"...
aws_ecs_cluster.my_cluster: Import prepared!
Prepared aws_ecs_cluster for import
aws_ecs_cluster.my_cluster: Refreshing state... [id=arn:aws:ecs:eu-central-1:000000000000:cluster/default]
Import successful!
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
Sprawdzenie różnic #
Format jest podobny do diff.
~(tylda) - linie zmienione-(myślnik) - linie usunięte+(plus) - linie dodane
$ terraform plan -target=aws_ecs_cluster.my_cluster
aws_ecs_cluster.my_cluster: Refreshing state... [id=arn:aws:ecs:eu-central-1:000000000000:cluster/default]
Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement
Terraform will perform the following actions:
# aws_ecs_cluster.my_cluster must be replaced
-/+ resource "aws_ecs_cluster" "my_cluster" {
~ arn = "arn:aws:ecs:eu-central-1:000000000000:cluster/default" -> (known after apply)
~ id = "arn:aws:ecs:eu-central-1:000000000000:cluster/default" -> (known after apply)
~ name = "default" -> "example" # forces replacement
- tags = {} -> null
~ tags_all = {} -> (known after apply)
# (1 unchanged attribute hidden)
- setting {
- name = "containerInsights" -> null
- value = "disabled" -> null
}
+ setting {
+ name = "containerInsights"
+ value = "enabled"
}
}
Plan: 1 to add, 0 to change, 1 to destroy.
╷
│ Warning: Resource targeting is in effect
│
│ You are creating a plan with the -target option, which means that the result
│ of this plan may not represent all of the changes requested by the current
│ configuration.
│
│ The -target option is not for routine use, and is provided only for
│ exceptional situations such as recovering from errors or mistakes, or when
│ Terraform specifically suggests to use it as part of an error message.
╵
───────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.
Wprowadzenie poprawek #
resource "aws_ecs_cluster" "my_cluster" {
name = "default"
tags = {}
tags_all = {}
setting {
name = "containerInsights"
value = "disabled"
}
}
Ponowne sprawdzenie #
$ terraform plan -target=aws_ecs_cluster.my_cluster
aws_ecs_cluster.my_cluster: Refreshing state... [id=arn:aws:ecs:eu-central-1:000000000000:cluster/default]
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
╷
│ Warning: Resource targeting is in effect
│
│ You are creating a plan with the -target option, which means that the result of this plan may not represent all of the changes requested by the current configuration.
│
│ The -target option is not for routine use, and is provided only for exceptional situations such as recovering from errors or mistakes, or when Terraform specifically suggests to use it as part of an
│ error message.
╵
Źródła #
- https://developer.hashicorp.com/terraform/cli/commands/state/rm
- https://developer.hashicorp.com/terraform/cli/import/usage
- https://developer.hashicorp.com/terraform/cli/commands/plan
- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_cluster
- https://stackoverflow.com/questions/46762047/i-would-like-to-run-terraform-only-for-a-specific-resource