ကျွန်တော်ဒီနေ့မှာတော့ terraform remote state အကြောင်းကိုရှင်းပြပေးမှာဖြစ်ပါတယ်။ remote state data source ဆိုတာက ကျွန်တော်တို့ backend တစ်ခုခုမှာ သိမ်းထားတဲ့ state ထဲက data တွေကို အခြား module တွေမှာပြန်သုံးဖို့အတွက်သုံးတာဖြစ်ပါတယ်
What is terraform state file?
state file ဆိုတာက ကျွန်တော်တို့ provision လုပ်လိုက်တဲ့ infrastructure တစ်ခုလုံးနဲ့သက်ဆိုင်တဲ့ အချက်အလက်တွေပါဝင်ပါတယ်။ ဒါကြောင့် အဲ့ဒီ file တွေကို လုံခြုံစိတ်ချရတဲ့ နေရာ တစ်ခုခုမှာ သိမ်းထားဖို့လိုပါတယ်။ ဘာလို့လဲဆိုရင် sensitive data တွေ ဖြစ်တဲ့ database password တွေ user credentials တွေပါဝင်နေလို့ပါ။
ဒါကြောင့် security အတွက်ဆို remote backend တွေကိုသုံးဖို့အကြံပြုကြပါတယ်။ remote backend တွေကလည်းအမျိုးမျိုးရှိပါတယ်။ aws s3, terraform cloud, azure blob စသည်ဖြင့်ပေါ့။ ကျွန်တော်ကတော့ terraform cloud နဲ့ s3 backend ကိုပဲအသုံးများပါတယ်။ terraform cloud ဆိုရင် state file ကိုအမြဲ encrypt လုပ်ပေးပါတယ်။ ဒီလောက်ဆို backend အကြောင်းကို အနည်းငယ်သဘောပေါက်လောက်မယ်ထင်ပါတယ်။
ပိုမြင်သာအောင် ကျွန်တော် remote state data source အကြောင်းကို lab လေးတစ်ခုနဲ့ ရှင်းပြပါမယ်
1├── infrastructure
2│ ├── backend.tf
3│ ├── main.tf
4│ ├── outputs.tf
5│ ├── providers.tf
6│ ├── variables.tf
7│ └── vpc.tf
8├── alb
9│ ├── README.md
10│ ├── alb.tf
11│ ├── backend.tf
12│ ├── main.tf
13│ ├── outputs.tf
14│ ├── providers.tf
15│ ├── usage.md
16│ └── variables.tf
17├── README.mdအပေါ်က tree မှာဆိုရင် module နှစ်ခုကို တွေ့ရမှာဖြစ်ပါတယ်။ ပထမတစ်ခုကတော့ infra module ဖြစ်ပြီး ဒုတိယတစ်ခုကတော့ alb module ဖြစ်ပါတယ်။ infra module မှာတော့ vpc, subnet တို့လို resource တွေပါဝင်ပြီးတော့ alb module မှာတော့ alb နဲ့ဆိုင်တဲ့ resource တွေပါဝင်ပါတယ်။
Infrastructure Module
infra module ထဲမှာ ပထမဦးဆုံး provider နဲ့ backend configuration ကိုအရင်ချပါမယ်။ provider ကတော့ aws provider ကိုပဲ example အနေနဲ့ သုံးထားပါတယ်။ အောက်ကတော့ aws provider အတွက် code ပါ။
1#infrastructure/providers.tf
2
3terraform {
4 required_providers {
5 aws = {
6 source = "hashicorp/aws"
7 version = "~> 4.9"
8 }
9 }
10}
11
12provider "aws" {
13 region = var.aws_region
14}ပြီးသွားရင်တော့ backend configuration ရေးပါမယ်။ s3 backend ကို remote backend အနေနဲ့ သုံးလိုက်ပါမယ်။
1#infrastructure/backend.tf
2
3terraform {
4 backend "s3" {
5 bucket = "my-terraform-remote-state-lab-s3"
6 key = "tf-infrastructure.tfstate"
7 region = "us-west-2"
8 encrypt = "true"
9 dynamodb_table = "my-terraform-remote-state-dynamodb"
10 }
11}backend configuration ထဲမှာသုံးထားတာကတော့ရိုးရှင်းပါတယ်။ state file ကို s3 bucket ထဲမှာ tf-infrastructure.tfstate နာမည်နဲ့ သိမ်းထားမယ်လို့ဆိုလိုတာပါ။ key ဆိုတာကတော့ file နာမည်ဖြစ်ပါတယ်။ encrypt ဆိုတာက state file ကို encrypt လုပ်မယ်လို့ဆိုလိုတာပါ။ dynamodb ကတော့ locking အတွက်သုံးတာပါ။
aws vpc module
backend configuration ပြီးရင်တော့ infra module ထဲမှာ vpc resource တွေကို create လုပ်ဖို့အတွက် vpc module ကိုသုံးပါမယ်။
1#infrastructure/vpc.tf
2
3locals {
4 prefix = "manage-alb-terraform"
5 vpc_name = "${local.prefix}-vpc"
6 vpc_cidr = "10.10.0.0/16"
7 common_tags = {
8 Environment = "dev"
9 ManagedBy = "Terraform"
10 }
11}
12
13module "vpc" {
14 source = "terraform-aws-modules/vpc/aws"
15
16 name = local.vpc_name
17 cidr = local.vpc_cidr
18
19 azs = ["${var.aws_region}a", "${var.aws_region}b"]
20 public_subnets = [
21 cidrsubnet(local.vpc_cidr, 8, 0),
22 cidrsubnet(local.vpc_cidr, 8, 1)
23 ]
24
25 private_subnets = [
26 cidrsubnet(local.vpc_cidr, 8, 2),
27 cidrsubnet(local.vpc_cidr, 8, 3)
28 ]
29
30 enable_nat_gateway = true
31 single_nat_gateway = true
32 enable_dns_hostnames = true
33
34
35 tags = merge(
36 {
37 Name = local.vpc_name
38 },
39 local.common_tags
40 )
41}vpc module subnet တွေ NAT တွေ create လိုက်ပါတယ်။
အားလုံးပြီးသွားရင်တော့ output တွေထုတ်ပေးလိုက်ပါမယ်။ အကြောင်းမဲ့ output တွေထုတ်တာတော့မဟုတ်ပါဘူး။ အဲ့ဒီ output တွေဖြစ်တဲ့ vpc id ၊ subnet တွေကို alb module မှာပြန်သုံးဖို့အတွက်ပါ။
1#infrstructure/outputs.tf
2
3output "prefix" {
4 value = local.prefix
5 description = "Exported common resources prefix"
6}
7
8output "common_tags" {
9 value = local.common_tags
10 description = "Exported common resources tags"
11}
12
13output "vpc_id" {
14 value = module.vpc.vpc_id
15 description = "VPC ID"
16}
17
18output "public_subnets" {
19 value = module.vpc.public_subnets
20 description = "VPC public subnets' IDs list"
21}
22
23output "private_subnets" {
24 value = module.vpc.private_subnets
25 description = "VPC private subnets' IDs list"
26}Child and Parent Module
ဒီ lab မှာဆိုရင် alb module ကို create လုပ်တဲ့အခါမှာ vpc ၊ subnet စတာတွေကို infra module ဆီကနေပြန်သုံးမှာပါ။ ဒါကြောင့် အခေါ်ခံရတဲ့ infra module က child module ဖြစ်ပြီး alb module က parent module or calling module လို့ခေါ်ပါတယ်။
ALB Module
module တွေဟာ တစ်ခုနဲ့တစ်ခု ဆက်စပ်မှုမရှိတာကြောင့် module တစ်ခုကနေတစ်ခု resource တွေကို ခေါ်သုံးဖို့ဆိုရင် remote state data source ဖြစ်ဖြစ် module တွေကို initialize လုပ်ပြီးပဲဖြစ်ဖြစ်တစ်နည်းနည်းနဲ့ခေါ်သုံးရပါမယ်။ ကျွန်တော်တို့ခေါ်သုံးမယ့် child module ထဲမှာ တခြား module မှာပြန်သုံးနိုင်ဖို့အတွက် output တွေထုတ်ပေးရပါမယ်။ ဒါကြောင့် ကျွန်တော်အပေါ်က infra module ထဲမှာ outputs တွေထုတ်ခဲ့ပါတယ်။
alb module အတွက် backend ကို create ပေးလိုက်ပါမယ်။
1alb/backend.tf
2
3terraform {
4 backend "s3" {
5 bucket = "my-terraform-remote-state-s3"
6 key = "terraform-alb.tfstate"
7 region = "us-west-2"
8 encrypt = "true"
9 dynamodb_table = "my-terraform-remote-state-dynamodb"
10 }
11}infra module ထဲက vpc ၊ subnet စတာတွေကိုပြန်သုံးဖို့အတွက် remote state data source ကိုသုံးရပါတော့မယ်။ အဲ့လို data source ကိုသုံးမှသာလျှင် infra module ထဲက resource တွေကိုပြန်သုံးနိုင်မှာဖြစ်ပါတယ်။ infra module အတွက် state ကို သိမ်းထားတဲ့ s3 bucket ၊ key ၊ region တို့ကိုပြန်ထည့်ပေးလိုက်တာပါ။
1alb/data.tf
2
3data "terraform_remote_state" "infrastructure" {
4 backend = "s3"
5 config = {
6 bucket = "my-terraform-remote-state-s3"
7 region = "us-west-2"
8 key = "tf-infrastructure.tfstate"
9 }
10}ပြီးသွားရင်တော့ alb အတွက် လိုအပ်တဲ့ security group ကို အရင် create လုပ်ပါမယ်။
1alb/alb.tf
2
3module "alb_sg" {
4 source = "terraform-aws-modules/security-group/aws"
5
6 name = "alb-sg"
7 description = "Security group for ALB"
8 vpc_id = data.terraform_remote_state.infrastructure.outputs.vpc_id
9
10 egress_rules = ["all-all"]
11}ဒီနေရာမှာ vpc_id နေရာမှာ data.terraform_remote_state.infrastructure.outputs.vpc_id ဆိုပြီးတော့ infra module က output ဖြစ်တဲ့ vpc_id ကိုခေါ်သုံးလိုက်တာပါ။ ပြီးရင် alb resource ကို create လုပ်ပါမည်။
1alb/alb.tf
2
3resource "aws_lb" "web" {
4 name = "alb-demo"
5 subnets = data.terraform_remote_state.infrastructure.outputs.public_subnets
6 security_groups = [module.alb_sg.security_group_id]
7
8 tags = {
9 Name = "alb-demo"
10 }
11}ဒီနေရာမှာလည်း subnet နေရာကို infra moudle က public subnet တွေကို remote state data နဲ့ခေါ်သုံးလိုက်ပါတယ််။ ဒီလောက်ဆို terraform ရဲ့ remote state data source အကြောင်းကိုနားလည်သဘောပေါက်လို့မယ်ထင်ပါတယ်။
Discussion
Join the conversation
How do you feel about this article?
Comments
Sign in to join the conversation
Sign in to be the first to comment!
Share Your Article
Share with your professional network
Recent Articles

AWS - Application Load Balancer
Elastic Load Balancing (ELB) ELB ဆိုတာကတော့ request တွေကို တစ်နေရာတည်းမှ လက်ခံကာ Amazon EC2 instances၊ containers, etc.....

Terraform Day 3: Benefits of Terraform State
Terraform ကိုလေ့လာ တဲ့အခါ ကျွန််တော်တို့ရဲ့ Project Folder ထဲမှာ terraform.tfstate ဆိုတဲ့ ဖိုင်လေးကို တွေ့ဖူးကြပါလိမ့်မယ...

Terraform Day 2: Essential IaC Principles You Must Know
မနေ့ကတော့ Terraform အကြောင်း အကြမ်းဖျင်း Concept ကို ပြောပြခဲ့ပြီးပြီဆိုတော့ ဒီနေ့မှာတော့ Terraform ကို Professional ကျက...

TCP/IP Protocol
အားလုံးပဲမင်္ဂလာပါ။ ဒီနေ့ ကျွန်တော်တို့ TCP/IP Protocol အကြောင်း ဆွေးနွေးသွားပါမယ်။ ပထမဆုံးအနေနဲ့ TCP/IP ရဲ့ History လေး...

Terraform Day 1: Introduction to IAC and Terraform
ကျွန်တော်တို့ cloud အကြောင်း စပြောကြပြီဆိုရင် အရင်ဆုံး ခေါင်းထဲရောက်လာတာ Console ထဲဝင်၊ UI ကနေ ခလုတ်လေးတွေ လိုက်နှိပ်ပြီ...

