[Terraform] terraformer로 기존 AWS 리소스 가져오기(import)
IT/Terraform

[Terraform] terraformer로 기존 AWS 리소스 가져오기(import)

반응형

Intro

오늘은 terraformer를 사용하여 이미 생성되어있는 리소스를 가져와 보도록 하겠습니다. (사용한 OS는 MAC M1 입니다.)

terraformer 란?

기존의 생성되어있는 인프라를 테라폼 소스로 가져오는 오픈소스 툴입니다. Waze SRE에서 만들었으며 공식적인 제품은 아니라고 합니다. terroformer는 태생이 GCP 위해 만들어졌습니다. Waze SRE 가 뭔지 궁금해서 구글링을 좀 해보니 구글의 일부라고 하는거보니 구글의 사내 오픈소스 조직 인 것 같네요. (근데 AWS에서도 사용할 수 있게 만들어줬다고? 갓 구글..)

참고 URL: https://github.com/GoogleCloudPlatform/terraformer/

terraformer로 가져올 수 있는 aws리소스 리스트

https://github.com/GoogleCloudPlatform/terraformer/blob/master/docs/aws.md

terraformer install

brew install terraformer

terrafomer import

import를 진행하면 gernerated 폴더가 생김

terraformer import aws --resources=vpc,subnet,route_table --regions=ap-northeast-2 --profile=<YOUR_PROFILE>

아래 에러 발생 시 다음 단계로

/Users/jordan/.terraform.d/plugins/darwin_arm64: no such file or directory

provider 다운로드

버전 확인 : https://releases.hashicorp.com/terraform-provider-aws

# install aws plugin (or whatever plugin you need)
curl -LO https://releases.hashicorp.com/terraform-provider-aws/4.8.0/terraform-provider-aws_4.8.0.0_darwin_arm64.zip 
mkdir -p ~/.terraform.d/plugins/darwin_arm64
mv terraform-provider-aws_4.8.0_darwin_arm64.zip ~/.terraform.d/plugins/darwin_arm64
cd ~/.terraform.d/plugins/darwin_arm64
unzip terraform-provider-aws_4.8.0_darwin_arm64.zip

.tfsate를 aws 형식으로 변경

terraformer가 gcp를 겨냥해서 만든거라서 처음 import하면 .tfstate 정보가 aws 형식과 다름. 이 부분을 aws폼으로 변경하는 작업

terraform state replace-provider -- -/aws hashicorp/aws
  • provider 변경
terraform {
  required_version = ">= 0.13.1"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 3.72"
    }
  }
}

tf init

tf plan

아래 에러는 aws configure 진행하지 않아서 발생하는 에러임. aws configure 인증 필요

│ Error: Invalid provider configuration
│ 
│ Provider "registry.terraform.io/hashicorp/aws" requires explicit configuration. Add a provider block to the root module
│ and configure the provider's required arguments as described in the provider documentation.
│ 
╵
╷
│ Error: error configuring Terraform AWS Provider: no valid credential sources for Terraform AWS Provider found.
│ 
│ Please see https://registry.terraform.io/providers/hashicorp/aws
│ for more information about providing credentials.
│ 
│ Error: failed to refresh cached credentials, no EC2 IMDS role found, operation error ec2imds: GetMetadata, request send failed, Get "http://169.254.169.254/latest/meta-data/iam/security-credentials/": dial tcp 169.254.169.254:80: i/o timeout
│ 
│ 
│   with provider["registry.terraform.io/hashicorp/aws"],
│   on <empty> line 0:
│   (source code not available)

끝으로

리소스를 테라폼 소스로 그대로 가져올 수 있으나 레이아웃 전략이 모두 다르기 때문에 이를 다시 커스터마이징 하는데에는 많은 시간이 필요할 것으로 보입니다. 혹시 기존 리소스를 가져와서 잘 활용하고 있는 사례 있으시면 공유 해주시면 좋을것 같습니다.

반응형