일단 terraform에서 빈번히 사용되는 provider가 제공하는 module을 작성하는 법에 대해서 알아보도록 하겠습니다.
https://github.com/LE123123/DevOps-terraform
GitHub - LE123123/DevOps-terraform: terraform practice
terraform practice. Contribute to LE123123/DevOps-terraform development by creating an account on GitHub.
github.com
여기에서 기본적인 모듈 작성 폴더 구조를 보실 수 있습니다.
/accounts/versions.tf
terraform {
required_version = ">= 0.15"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.45"
}
}
}
우선 versions.tf에는 필요로하는 terraform의 버전을 적어줍니다. 그리고 그 아래에는 해당 테라폼 모듈을 사용하기 위한 provider정보를 명시해 주어야 합니다.
/accounts/variables.tf
variable "name" {
description = "The name for the AWS account. Used for the account alias."
type = string
}
variable "password_policy" {
description = "Password Policy for the AWS account."
type = object({
minimum_password_length = number
require_numbers = bool
require_symbols = bool
require_lowercase_characters = bool
require_uppercase_characters = bool
allow_users_to_change_password = bool
hard_expiry = bool
max_password_age = number
password_reuse_prevention = number
})
default = {
minimum_password_length = 8
require_numbers = true
require_symbols = true
require_lowercase_characters = true
require_uppercase_characters = true
allow_users_to_change_password = true
hard_expiry = false
max_password_age = 0
password_reuse_prevention = 0
}
}
그 다음에는 모듈에 필요한 변수들의 정보를 작성해 줍니다.
여기서 name은 AWS Account를 web console에서 봤을 떄, 103248429 이렇게 숫자로 표현되는 것을 우리가 기억하기 힘들기 때문에, 간단히 별칭을 지정해주기 위한 변수라고 보시면 되고, 그 아래는 리소스에서 활용할 변수들이라고 보시면 됩니다.
/accounts/main.tf
data "aws_caller_identity" "this" {}
###################################################
# AWS Account Alias
###################################################
resource "aws_iam_account_alias" "this" {
account_alias = var.name
}
###################################################
# Password Policy for AWS Account and IAM Users
###################################################
resource "aws_iam_account_password_policy" "this" {
minimum_password_length = var.password_policy.minimum_password_length
require_numbers = var.password_policy.require_numbers
require_symbols = var.password_policy.require_symbols
require_lowercase_characters = var.password_policy.require_lowercase_characters
require_uppercase_characters = var.password_policy.require_uppercase_characters
allow_users_to_change_password = var.password_policy.allow_users_to_change_password
hard_expiry = var.password_policy.hard_expiry
max_password_age = var.password_policy.max_password_age
password_reuse_prevention = var.password_policy.password_reuse_prevention
}
그리고 main에서는 aws_caller_identity를 이용해서 AWS account_id를 가져옵니다. 그 다음에는 aws의 provider를 통해서 account의 별칭을 지정해 줍니다. 그리고 iam user의 비밀번호 정책을 리소스를 통해서 만들어 줍니다.
/accounts/outputs.tf
output "id" {
description = "The AWS Account ID."
value = data.aws_caller_identity.this.account_id
}
output "name" {
description = "Name of the AWS account. The account alias."
value = aws_iam_account_alias.this.account_alias
}
output "signin_url" {
description = "The URL to signin for the AWS account."
value = "https://${var.name}.signin.aws.amazon.com/console"
}
output "password_policy" {
description = "Password Policy for the AWS Account. `expire_passwords` indicates whether passwords in the account expire. Returns `true` if `max_password_age` contains a value greater than 0."
value = aws_iam_account_password_policy.this
}
outputs.tf는 root module에서 참조하기 위해서 작성해 주어야 하는 매우 중요한 것입니다. 따라서 소비자들이 필요로 하는 output들을 잘 빼내는 것이 가장 중요합니다.
/main.tf
provider "aws" {
region = "ap-northeast-2"
}
module "account" {
# 로컬 소스
source = "./accounts"
name = "hyunseo-fastcampus"
password_policy = {
minimum_password_length = 8
require_numbers = true
require_symbols = true
require_lowercase_characters = true
require_uppercase_characters = true
allow_users_to_change_password = true
hard_expiry = false
max_password_age = 0
password_reuse_prevention = 0
}
}
output "id" {
value = module.account.id
}
output "account_name" {
value = module.account.name
}
output "signin_url" {
value = module.account.signin_url
}
output "account_password_policy" {
value = module.account.password_policy
}
이렇게 모듈 작성을 해서 손쉽게 활용할 수 있습니다. 또한 child module이 또 다른 child module을 참조해 갈 수도 있습니다.
'DevOps > AWS Architecture' 카테고리의 다른 글
[ DevOps ] - (패커를 이용한 머신 이미지 관리) - 패커 소개 (0) | 2022.08.02 |
---|---|
[ DevOps ] - (테라폼을 이용한 인프라 관리) 테라폼 terraform_remote_state 데이터소스 활용 (0) | 2022.08.02 |
[ DevOps ] - (테라폼을 이용한 인프라 관리) 테라폼 워크스페이스 관리 (0) | 2022.08.01 |
[ DevOps ] - (테라폼을 이용한 인프라 관리) 테라폼 리소스 강제 교체하기 (taint & untaint) (0) | 2022.08.01 |
[ DevOps ] - (테라폼을 이용한 인프라 관리) 테라폼 상태 관리 (0) | 2022.08.01 |