2024.11.10. Today I Learned

polar·2024년 11월 10일

2024년 11월 TIL

목록 보기
8/28
post-thumbnail

배운점

  1. 모듈을 선언하여 외부 폴더의 변수를 사용할 수 있으며, 이때 output.tf 파일에 정의된 변수만 외부에서 접근할 수 있다.

    예를 들어, bastion_instance_sg 모듈에서 VPC ID가 필요할 때, vpc 모듈의 output.tf 파일에 vpc_id 변수를 정의하면 이를 참조할 수 있다.

    # /sg/main.tf
    module "vpc" {
    	source = "../vpc"
    }
    
    module "bastion_instance_sg" {
    	source = "terraform-aws-modules/security-group/aws"
    
    	name        = "bastion-instance-sg"
    	description = "Security group for bastion instance with SSH open within VPC"
    	vpc_id      = module.vpc.vpc_id
    
    	...
    }
    # /vpc/output.tf
    output "vpc_id" {
    	description = "VPC ID"
    	value       = module.vpc.vpc_id
    }
  1. (코드를 작성할 때) 동일한 모듈을 선언하더라도 terraform init을 통해 초기화해야 모듈이 적용된다.

    예를 들어, bastion_instance_sg에서 terraform-aws-modules/security-group/aws 모듈을 사용해 초기화했더라도,

    module "bastion_instance_sg" {
    	source = "terraform-aws-modules/security-group/aws"
    
    	name        = "bastion-instance-sg"
    	description = "Security group for bastion instance with SSH open within VPC"
    	vpc_id      = module.vpc.vpc_id
    
    	...
    }
    

    terraform init을 다시 실행하여 web_server_instance_sg 모듈이 정상적으로 초기화되도록 해야 한다.

    module "web_server_instance_sg" {
    	source = "terraform-aws-modules/security-group/aws"
    
    	name        = "web-server-instance-sg"
    	description = "Security group for web server instance with HTTP, HTTPS open within VPC and SSH open only from bastion"
    	vpc_id = module.vpc.vpc_id
    
    	....
    }
  1. 모듈을 선언한 폴더에서 terraform apply를 실행하면, 해당 모듈이 참조하는 다른 모듈의 리소스도 함께 생성된다.

    예를 들어, sg 폴더에서 vpc 모듈을 선언하면, terraform apply 명령어를 실행할 때 vpc에 정의된 리소스도 함께 생성된다.

    # /sg/main.tf
    module "vpc" {
    	source = "../vpc"
    }
    
    ...
  1. 모듈 명이 변경되더라도 terraform init 명령어를 다시 실행해야 한다.

    예를 들어, database_instance_sg 모듈을 초기화하여 개발하던 중, 모듈 이름을 mysql_database_instance_sg로 변경하는 경우(코드 수정이 없더라도), terraform init 명령어를 다시 실행해야 한다.

    module "database_instance_sg" {
    	...
    }
    module "mysql_database_instance_sg" {
    	...
    }

느낀점

SW 아카데미 당시 terraform에 대해 너무 겉핥기로 배웠다는 느낌이 든다.

역시 직접 뭔가 만들어봐야, 부족한 부분이 보이고 이해도 빠른 것 같다.

(그래서 경력직을 찾나... 나는 어디서 경력 쌓지....)

0개의 댓글