Terraform import 하는 방법을 정리해보자.
https://github.com/GoogleCloudPlatform/terraformer
MacOS에서는 brew를 통해 terraformer
를 설치할 수 있다.
$ brew install terraformer
만약 AWS 계정을 SSO로 로그인하여 사용하고 있다면, aws2-wrap
를 설치하여 임시로 terraformer profile을 생성해야한다. SSS 로그인으로 사용하지 않는다면 할 필요 없다.
https://github.com/GoogleCloudPlatform/terraformer/issues/1588 참고
$ brew install aws2-wrap
이어서 profile을 만들어준다. 이것 또한 SSS 로그인으로 사용하지 않는다면 할 필요 없다.
$ aws2-wrap --profile <profile_name> --generate --outprofile terraformer
이제 terraformer
명령어를 통해 생성되어있는 AWS 리소스를 Terraform 코드로 추출하자.
먼저, 아래와 같이 간단히 providers.tf
코드를 만든다.
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.20.0"
}
}
}
provider "aws" {
region = "ap-northeast-1"
}
만약 Terraform Cloud를 사용한다면 아래와 같이 cloud
부분을 추가한다.
terraform {
required_version = ">= 1.8.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.40.0"
}
}
cloud {
organization = "temp-org"
workspaces {
name = "test-workspace"
}
}
}
provider "aws" {
region = "ap-northeast-1"
}
만약 profile 이름이 default
가 아닌 다른 이름이라면 provider 부분에 profile
을 추가한다. 이 때, SSO 로그인으로 AWS를 접근하는 사람은 위에서 만들었던 terraformer profile이 아닌 원래 이용하고 있었던 profile 이름으로 적는다.
provider "aws" {
region = "ap-northeast-1"
profile = <profile_name>
}
이제 Terraform 코드로 추출해보자.
$ terraformer import aws --resources=sg,vpc,iam --regions=ap-northeast-1 --profile=<profile_name>
SSO 로그인이라면 아래와 같이 입력하자.
$ terraformer import aws --resources=sg,vpc,iam --regions=ap-northeast-1 --profile=terraformer
하지만 아래와 같이 에러 로그가 나올 수 있다.
2024/05/09 17:33:03 aws importing region ap-northeast-1
2024/05/09 17:33:03 open /Users/dongminlee/.terraform.d/plugins/darwin_arm64: no such file or directory
그러면 아래의 명령어를 입력한다.
$ wget https://releases.hashicorp.com/terraform-provider-aws/5.20.0/terraform-provider-aws_5.20.0_darwin_arm64.zip
$ mkdir -p ~/.terraform.d/plugins/darwin_arm64
$ mv terraform-provider-aws_5.20.0_darwin_arm64.zip ~/.terraform.d/plugins/darwin_arm64
$ cd ~/.terraform.d/plugins/darwin_arm64
$ unzip terraform-provider-aws_5.20.0_darwin_arm64.zip
$ rm -r terraform-provider-aws_5.20.0_darwin_arm64.zip
AWS 보안 그룹만 실행했을 때 아래와 같이 나온다.
$ terraformer import aws --resources=sg --regions=ap-northeast-1 --profile=terraformer
2024/04/26 12:38:24 aws importing region ap-northeast-1
2024/04/26 12:38:24 aws importing... sg
2024/04/26 12:38:25 aws done importing sg
2024/04/26 12:38:25 Number of resources for service sg: 197
2024/04/26 12:38:25 Refreshing state... aws_security_group....
2024/04/26 12:38:25 Refreshing state... aws_security_group....
2024/04/26 12:38:25 Refreshing state... aws_security_group....
2024/04/26 12:38:25 Refreshing state... aws_security_group_rule....
...
...
2024/04/26 12:38:33 Filtered number of resources for service sg: 197
2024/04/26 12:38:33 aws Connecting....
2024/04/26 12:38:33 aws save sg
2024/04/26 12:38:33 aws save tfstate for sg
이제 tfstate 파일을 올려야하는데, 나의 경우, DynamoDB + S3 조합이 아닌 Terraform Cloud를 이용하고 있기 때문에 terraformer에서 나온 tfstate를 사용하지 않고 일일이 terraform import
명령어로 tfstate 파일을 올려주었다..
terraform import
명령어를 입력하기전에 먼저 main.tf
에 resource
를 만들어주어야한다. 여기서는 terraformer에서 나온 Terraform 코드를 사용하면 된다. main.tf
에 복사 & 붙여넣기 후에 아래의 명령어처럼 찾아서 실행하면 Terraform Cloud에 tfstate 파일이 업로드된다.
# iam role
tf import aws_iam_role.<terraform_resource_role_name> <aws_resource_role_name>
# iam role policy attachment
tf import aws_iam_role_policy_attachment.<terraform_resource_role_policy_attachment_name> <aws_resource_role_nmae>/arn:aws:iam::<aws_account_number>:policy/<aws_resource_policy_name>
# iam policy
tf import aws_iam_policy.<terraform_resource_policy_name> <aws_resource_policy_arn>
아쉽게도 Terraformer에 OpenSearch Service에 대한 Terraform 코드 변환이 지원이 되지 않았다.
그래서 TerraCognita라는 오픈소스를 이용하였다.
https://github.com/cycloidio/terracognita
MacOS에서는 brew를 통해 terracognita
를 설치할 수 있다.
$ brew install terracognita
마찬가지로 AWS 계정을 SSO로 로그인하여 사용하고 있다면, aws2-wrap
를 설치하여 임시로 terracognita profile을 생성해야한다.
$ brew install aws2-wrap
이어서 profile을 만들어준다. 이것 또한 SSS 로그인으로 사용하지 않는다면 할 필요 없다.
$ aws2-wrap --profile <profile_name> --generate --outprofile terracognita
이제 terracognita
명령어를 통해 생성되어있는 AWS 리소스를 Terraform 코드로 추출하자.
terracognita aws --aws-profile terracognita --aws-default-region ap-northeast-1 --include aws_elasticsearch_domain --tfstate es.tfstate --hcl es.tf
나머지는 2. AWS 리소스 추출 에서 설명된 부분을 따라가면 된다.