본문 바로가기

♻ Terraform(테라폼)/👉 Terraformer

tag 정보 기반 terraformer 리소스 필터링 하기

# 리소스 태그 정보를 기반으로 terraformer를 통해 리소스를 필터링 하여 terraform code로 저장하는 방법에 대해서 알아보자.

1. 리소스 태깅 정보 확인

- AWS의 리소스 태깅 정보 확인은 매우 쉽다.

예를들어 VPC의 태그정보를 확인하는 방법은 해당 태그 탭으로 이동하여 값을 확인하면 된다.

2. terraformer를 활용하여 태깅된 리소스만 terraform code로 저장하기

참고 사이트 1

https://www.rapyder.com/blogs/infrastructure-to-code/

 

Infrastructure to Code- Create Terraform File From Existing EC2 Instance in AWS Console

Infrastructure to Code- This blog gives a walkthrough of a basic example where we create a Terraform file from an existing EC2 instance in the AWS console.

www.rapyder.com

참고 사이트 2 (terraformer 공식 사이트 - 가이드 제공)

https://github.com/GoogleCloudPlatform/terraformer/blob/master/README.md

 

GitHub - GoogleCloudPlatform/terraformer: CLI tool to generate terraform files from existing infrastructure (reverse Terraform).

CLI tool to generate terraform files from existing infrastructure (reverse Terraform). Infrastructure to Code - GitHub - GoogleCloudPlatform/terraformer: CLI tool to generate terraform files from e...

github.com

- 기본 명령어는 아래와 같다.

terraformer import aws \
--resources=vpc \
--regions=ap-northeast-2 \
--filter="Name=tags.Name;Value=seungkim-vpc"

위의 명령어는 아래와 같은 태그 정보가 입력 되어 있는경우 해당 vpc 정보를 terraform code로 변환하여 generated 폴더에 저장해 준다. 해당 내용을 응요하여 여러 태깅된 정보를 최신 리소스로 업데이트 가능하며 terraform code로도 저장 할 수 있다.

3. 변환된 terraform code 확인하기

위의 명령어로 실행하고 generated 폴더에 저장된 terraform code를 확인해보자.

실행 후 아래와 같은 결과값이 콘솔에 표시 된다.

2023/03/21 23:58:13 aws importing region ap-northeast-2
2023/03/21 23:58:14 aws importing... vpc
2023/03/21 23:58:14 aws done importing vpc
2023/03/21 23:58:14 Number of resources for service vpc: 3
2023/03/21 23:58:14 Refreshing state... aws_vpc.tfer--vpc-00000000000000000
2023/03/21 23:58:14 Refreshing state... aws_vpc.tfer--vpc-00000000000000000
2023/03/21 23:58:14 Refreshing state... aws_vpc.tfer--vpc-00000000000000000
2023/03/21 23:58:14 Filtered number of resources for service vpc: 1
2023/03/21 23:58:14 aws Connecting....
2023/03/21 23:58:14 aws save vpc
2023/03/21 23:58:14 aws save tfstate for vpc

위의 내용을 보면 3개의 vpc 중에서 필더된 vpc 리소스는 1개라는 메세지가 표시되고, 해당 코드는 아래에 경로에 저장된다. generated 라는 폴더가 생기고 해당 폴더안에 terraform code가 저장된다.

자세한 경로는 아래와 같다. vpc.tf 외 여러개의 tf 파일이 생성된 것을 확인 할 수 있다.

ubuntu@ip-10-0-00-000:~/.terraform.d/plugins/generated/aws/vpc$ ll
total 24
drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 21 23:58 ./
drwxrwxr-x 3 ubuntu ubuntu 4096 Mar 21 23:58 ../
-rwxrwxr-x 1 ubuntu ubuntu  106 Mar 21 23:58 outputs.tf*
-rwxrwxr-x 1 ubuntu ubuntu  129 Mar 21 23:58 provider.tf*
-rwxrwxr-x 1 ubuntu ubuntu 2342 Mar 21 23:58 terraform.tfstate*
-rwxrwxr-x 1 ubuntu ubuntu  372 Mar 21 23:58 vpc.tf*
ubuntu@ip-10-0-00-000:~/.terraform.d/plugins/generated/aws/vpc$ pwd
/home/ubuntu/.terraform.d/plugins/generated/aws/vpc

- 변환된 코드를 vi나 vim으로 열어보면 아래와 같다.

vpc.tf

resource "aws_vpc" "tfer--vpc-000000000000000000" {
  assign_generated_ipv6_cidr_block = "false"
  cidr_block                       = "10.0.0.0/16"
  enable_dns_hostnames             = "true"
  enable_dns_support               = "true"
  instance_tenancy                 = "default"

  tags = {
    Name = "seungkim-vpc"
  }

  tags_all = {
    Name = "seungkim-vpc"
  }
}

- outputs.tf

output "aws_vpc_tfer--vpc-000000000000000000_id" {
  value = "${aws_vpc.tfer--vpc-000000000000000000.id}"
}

- provider.tf

provider "aws" {
  region = "ap-northeast-2"
}

terraform {
        required_providers {
                aws = {
            version = "~> 3.37.0"
                }
  }
}

- 마지막으로 terraform.tfstate 파일을 보면 아래와 같다. 아래와 같은 유형으로 출력되고, 상태 파일이 저장되어 있다.

"version": 3,
    "terraform_version": "0.12.31",
    "serial": 1,
    "lineage": "00000000000000000000000000000000000000000",
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {
                "aws_vpc_tfer--vpc-0000000000000000000_id": {
                    "sensitive": false,
                    "type": "string",
                    "value": "vpc-0000000000000000000"
                }
            },
...
...
...

- 해당 내용을 활용하여 다양한 태깅 정책을 통해 리소스를 효율적으로 관리할 수 있지 않을까 싶다.

역시 갓글 따봉을 외칠수 있을까?