일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Python
- taint
- ansible
- SSL 인증서
- Amazon VPC
- Amazon Route 53
- deployment
- Terraform
- k8s
- Backend
- docker
- AWS
- Amazon RDS
- AWS Security Hub
- DevOps
- mariaDB
- Terraform state
- AWS EC2
- staticmethod
- Cognito
- ReplicaSet
- Industry Week 2023
- classmethod
- Amazon GuardDuty
- Heartbleed
- DaemonSet
- Kubernetis
- ConfigMap
- CI CD
- Amazon DynamoDB
Archives
- Today
- Total
Nubes et Stella
Terraform #09 (Module) 본문
01. Terraform Module
테라폼 모듈은 테라폼에서 여러번 재사용가능한 코드모음이라고 생각하면 될 것 같다. 모듈을 사용하는 주 목적은 여러번 사용되는 기능을 모듈화하여 코드 관리성을 높이기 위함이다.
Terraform Module 구현
- main.tf 파일 아래 account 디렉터리를 생성하여, 그안에 모듈 코드를 작성한다. (하나의 모듈은 하나의 디렉터리 라고 생각하면 편할 것 같다.)
- version.tf 파일에서 terraform 과 provider(aws)에서 요구하는 버전 정보를 명시한다.
1 terraform {
2 required_version = ">= 0.15"
3
4 required_providers {
5 aws = {
6 source = "hashicorp/aws"
7 version = ">= 3.45"
8 }
9 }
10 }
- variables.tf 파일에서 각 종 변수를 사전 설정한다.
1 variable "name" {
2 description = "The name for the AWS account. Used for the account alias."
3 type = string
4 }
5
6 variable "password_policy" {
7 description = "Password Policy for the AWS account."
8 type = object({
9 minimum_password_length = number
10 require_numbers = bool
11 require_symbols = bool
12 require_lowercase_characters = bool
13 require_uppercase_characters = bool
14 allow_users_to_change_password = bool
15 hard_expiry = bool
16 max_password_age = number
17 password_reuse_prevention = number
18 })
19 default = {
20 minimum_password_length = 8
21 require_numbers = true
22 require_symbols = true
23 require_lowercase_characters = true
24 require_uppercase_characters = true
25 allow_users_to_change_password = true
26 hard_expiry = false
27 max_password_age = 0
28 password_reuse_prevention = 0
29 }
- line 1 : aws_caller_identity 리소스를 사용하여 account_id를 가져온다.
- line 8~10 : aws 계정에 대한 alias를 설정한다.
- line 17~27 : aws_iam_account_password_policy 리소스를 사용하여 변수로 받은 password_policy 정보를 받아온다.
1 data "aws_caller_identity" "this" {}
2
3
4 ###################################################
5 # AWS Account Alias
6 ###################################################
7
8 resource "aws_iam_account_alias" "this" {
9 account_alias = var.name
10 }
11
12
13 ###################################################
14 # Password Policy for AWS Account and IAM Users
15 ###################################################
16
17 resource "aws_iam_account_password_policy" "this" {
18 minimum_password_length = var.password_policy.minimum_password_length
19 require_numbers = var.password_policy.require_numbers
20 require_symbols = var.password_policy.require_symbols
21 require_lowercase_characters = var.password_policy.require_lowercase_characters
22 require_uppercase_characters = var.password_policy.require_uppercase_characters
23 allow_users_to_change_password = var.password_policy.allow_users_to_change_password
24 hard_expiry = var.password_policy.hard_expiry
25 max_password_age = var.password_policy.max_password_age
26 password_reuse_prevention = var.password_policy.password_reuse_prevention
27 }
- outputs.tf 파일을 활용하여 모듈 실행시 출력되는 정보들을 설정한다.
1 output "id" {
2 description = "The AWS Account ID."
3 value = data.aws_caller_identity.this.account_id
4 }
5
6 output "name" {
7 description = "Name of the AWS account. The account alias."
8 value = aws_iam_account_alias.this.account_alias
9 }
10
11 output "signin_url" {
12 description = "The URL to signin for the AWS account."
13 value = "https://${var.name}.signin.aws.amazon.com/console"
14 }
15
16 output "password_policy" {
17 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."
18 value = aws_iam_account_password_policy.this
19 }
- line 5~6 : main.tf 파일을 생성할 때 module을 생성하고 source를 통하여 이전에 만든 디렉터리를 지정한다.
- line 8~36: 이전에 만든 모듈에서 설정한 정보들을 기입한다.
1 provider "aws" {
2 region = "ap-northeast-2"
3 }
4
5 module "account" {
6 source = "./account"
7
8 name = "syhan-good"
9 password_policy = {
10 minimum_password_length = 8
11 require_numbers = true
12 require_symbols = true
13 require_lowercase_characters = true
14 require_uppercase_characters = true
15 allow_users_to_change_password = true
16 hard_expiry = false
17 max_password_age = 0
18 password_reuse_prevention = 0
19 }
20 }
21
22 output "id" {
23 value = module.account.id
24 }
25
26 output "account_name" {
27 value = module.account.name
28 }
29
30 output "signin_url" {
31 value = module.account.signin_url
32 }
33
34 output "account_password_policy" {
35 value = module.account.password_policy
36 }
- terraform apply 하여, outputs.tf 파일에서 설정한 정보들이 출력되는 것을 확인 할 수 있다.
- AWS 콘솔에 접속하여 정상적으로 Terraform 코드가 적용되었는지 확인한다.
'DevOps > Terraform' 카테고리의 다른 글
Terraform #08 (taint/Workspace) (0) | 2023.10.24 |
---|---|
Terraform #07 (Backend/State) (0) | 2023.10.23 |
Terraform #06 (Conditional/For) (0) | 2023.10.19 |
Terraform #05 (count/for_each) (1) | 2023.10.18 |
Terraform #04 (Variable/Output/Local) (1) | 2023.10.09 |