내가 입사 하기 전에 AWS 클라우드에 Prod, Stage 환경이 구축이 되어 있었다.
AWS Console에서 작업을 진행하거나 shell scripts를 통해서 생성된 리소스들이 대부분 이였다.
구축에 대한 히스토리 및 스크립트에 대한 설명을 확인 할 수 없었다.
Terraform 스터디를 진행하면서 기존의 리소스 및 정보들을 Terraform 코드로 변환하고자 하는 욕구(?) 의지가 끌어 올랐다.
Terraform에서 Import는 Terraform을 통해서 생성된 Resource가 아니라 AWS Console을 통해서 직접 추가한 Resource나 다른 Terraform 환경에서 생성한 Resource를 가져오기 위해 제공되는 CLI 명령이다.
사용하는 이유는 Terraform은 tfstate파일에 있는 Resource만 본다는 점 때문이다.
terraform import
를 통해서 해결 가능하다$ terraform import aws_iam_user.bob bob
Error: resource address "aws_iam_user.bob" does not exist in the configuration.
Before importing this resource, please create its configuration in the root module. For example:
resource "aws_iam_user" "bob" {
# (resource arguments)
}
resource "aws_iam_user" "bob" {
# (resource arguments)
}
$ terraform import aws_iam_user.bob bob
aws_iam_user.bob: Importing from ID "bob"...
aws_iam_user.bob: Import prepared!
Prepared aws_iam_user for import
aws_iam_user.bob: Refreshing state... [id=bob]
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.
terraform.tfstate
파일에서도 리소스에 대한 정보가 정상적으로 갖고 왔음을 확인 할수 있다.{
"version": 4,
"terraform_version": "1.3.2",
"serial": 1,
"lineage": "XXXXXXX",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "aws_iam_user",
"name": "bob",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"arn": "arn:aws:iam::XXXXXXXX:user/bob",
"force_destroy": null,
"id": "bob",
"name": "bob",
"path": "/",
"permissions_boundary": null,
"tags": {
"level": "mid",
"manager": "XXXXX"
},
"tags_all": {
"level": "mid",
"manager": "XXXXXX"
},
"unique_id": "XXXXXX"
},
"sensitive_attributes": [],
"private": "XXXXXXXXXXXXX=="
}
]
}
],
"check_results": null
}
terraform plan
aws_iam_user.bob: Refreshing state... [id=bob]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
- destroy
Terraform will perform the following actions:
# aws_iam_user.bob will be destroyed
# (because aws_iam_user.bob is not in configuration)
- resource "aws_iam_user" "bob" {
- arn = "arn:aws:iam::xxxxx:user/bob" -> null
- id = "bob" -> null
- name = "bob" -> null
- path = "/" -> null
- tags = {
- "level" = "mid"
- "manager" = "xxxxx"
} -> null
- tags_all = {
- "level" = "mid"
- "manager" = "xxxxx"
} -> null
- unique_id = "xxxxx" -> null
}
Plan: 0 to add, 0 to change, 1 to destroy.
Terraform import
를 실행하기 전에 Terraform 스크립트에서 리소스를 정의한다resource "aws_s3_bucket" "main" {
bucket = "mybucket-s3-louis"
acl = "private"
force_destroy = true
}
Terraform import
를 실행해서 정의한 리소스에 import 한다terraform import 'aws_s3_bucket.main' mybucket-s3-louis
$ terraform import 'aws_s3_bucket.main' mybucket-s3-louis
aws_s3_bucket.main: Importing from ID "mybucket-s3-louis"...
aws_s3_bucket.main: Import prepared!
Prepared aws_s3_bucket for import
aws_s3_bucket.main: Refreshing state... [id=mybucket-s3-louis]
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.
terraform plan
으로 정의한 리소스에 기존 S3 리소스가 반영 된 것을 확인 할 수 있다.$ terraform plan
aws_s3_bucket.main: Refreshing state... [id=mybucket-s3-louis]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
~ update in-place
Terraform will perform the following actions:
# aws_s3_bucket.main will be updated in-place
~ resource "aws_s3_bucket" "main" {
+ acl = "private"
+ force_destroy = true
id = "mybucket-s3-louis"
tags = {}
# (9 unchanged attributes hidden)
# (3 unchanged blocks hidden)
}
Plan: 0 to add, 1 to change, 0 to destroy.
╷
│ Warning: Argument is deprecated
│
│ with aws_s3_bucket.main,
│ on main.tf line 11, in resource "aws_s3_bucket" "main":
│ 11: acl = "private"
│
│ Use the aws_s3_bucket_acl resource instead
│
│ (and one more similar warning elsewhere)
to be continue...
https://developer.hashicorp.com/terraform/cli/commands/import
https://github.com/dtan4/terraforming
https://ch4njun.tistory.com/181
https://medium.com/@cheekorkind/terraform-import정리-aws콘솔에서-클릭으로-만든건데-반영안될까-d3852b0701d9