ကျွန်တော် sharing လုပ်ချင်တာက AWS Cloud Infrastructure ကို Terraform နဲ့ Ansible ကိုသုံးပြီး Automate လုပ်တာကို sharing လုပ်ချင်တာပါ.
Terraform က DevOps Automation Tools တွေထဲက Infrastructure Provisioning Tool ( IaC ) တစ်ခုပါ.
Terraform ကို Infrastructure, Platform, and services တွေကို provision လုပ်တဲ့နေရာမှာသုံးတာများပါတယ်.
Terraform ကိုသုံးပြီး ကျွန်တော်တို့ Developer team တွေလိုအပ်တဲ့ Infrastructure တွေကို လွယ်ကူစွာ provision လုပ်ပေးလို့ရပါတယ်.
Terraform ကို Cloud Infrastructure Automation အတွက်အသုံးများပါတယ်.
ကျွန်တော်တို့ Terraform နဲ့ provisioning လုပ်လို့ရတဲ့ resources category ( providers )တွေကများပါတယ်.

Comparison Between Ansible and Terraform
ကျွန်တော် Terraform အကြောင်းကို sharing မလုပ်ခင်မှာ Ansible and Terraform တို့ရဲ့ ခြားနားချက်တွေကို အရင် sharing လုပ်ချင်ပါတယ်.
ကျွန်တော်တို့ Terraform ကို Ansible နဲ့နှိုင်းယှဉ်ကြည့်ချင်အားဖြင့် မတူညီတဲ့ အသုံးပြုမူတွေ, အားသာချက် အားနည်းချက်တွေကိုပိုပြီးမြင်စေပါတယ်.
ကျွန်တော် အရင်က Ansible အကြောင်းကိုလဲအနည်းငယ် sharing လုပ်ဖူးပါတယ်. Ansible ကို CaC ( Configuration as Code ) အနေနဲ့အသုံးပြုတာများပါတယ်.
Ansible ကို ansible modules တွေကိုသုံးပြီး IaC အနေနဲ့သုံးလဲရပါတယ်. Ansible ကို Terraform နဲ့တွဲသုံးလဲရပါတယ်.
Ansible နဲ့ပါတ်သက်ပြီး ကျွန်တော်အရင်က sharing လုပ်ထားတာတွေရှိပါတယ်.
Ansible ကို IaC tool အနေနဲ့သုံးရတာက Terraform လောက်တော့အဆင်မပြေပါဘူး.
Ansible က configuration management အတွက်သုံးတာများပါတယ်.
Language style အနေနဲ့ ansible က hybrid ပါ, procedural and declarative programming paradigms ကိုသုံးထားပါတယ်.
Terraform က declarative programming paradigm ကိုသုံးထားပါတယ်.
procedural and declarative programming paradigms တွေရဲ့ ခြားနားချက်ကို ကျွန်တော်တို့ လေ့လာကြည့်ရအောင်.
procedural
procedural programming paradigm မှာကျွန်တော်တို့ လိုချင်တဲ့ results or desired state ကိုရဖို့ လုပ်ရမဲ့ အဆင့်တွေကို step by step သတ်မှတ်ပေးရပါတယ်.
ကျွန်တော်တို့ ansible ကိုသုံးပြီး aws မှာ ec2 instance တစ်ခုကို create လုပ်မယ်ဆိုရင် ကျွန်တော်တို့ automate လုပ်ချင်တဲ့ infrastructure ရဲ့ vpc, subnet, availability zone, security group, etc… တို့ကိုအရင်သတ်မှတ်ပေးရပါတယ်.
ကျွန်တော်တို့ ec2 instance တစ်ခုကို create မလုပ်ခင်မှာ လိုအပ်တဲ့ step တွေကို step by step သတ်မှတ်ပေးထားဖို့လိုပါတယ်.
Ansible က procedural programming paradigms ကိုသုံးထားလို့ဖြစ်ပါတယ်.
Ansible ad hoc command တွေက procedural programming paradigms ကိုသုံးပါတယ်.
Ansible module တွေက declarative programming paradigm ကိုသုံးပါတယ်. Ansible modules တွေကိုသုံးပြီး ကျွန်တော်တို့ Ansible ကို declarative ပုံစံသုံးလို့ရပါတယ်.
declarative
declarative programming paradigm မှာ ကျွန်တော်တို့ လိုချင်တဲ့ results or desired state ကိုသတ်မှတ်ပေးရင်ရပါပြီး, ကျွန်တော်တို့ လိုချင်တဲ့ results or desired state ကိုရဖို့လိုအပ်တဲ့ အဆင့်တွေကို step by step သတ်မှတ်ပေးဖို့မလိုပါဘူး.
ကျွန်တော်တို့ Terraform ကိုသုံးပြီး aws မှာ ec2 instance တစ်ခုကို create လုပ်မယ်ဆိုရင် ကျွန်တော်တို့ automate လုပ်ချင်တဲ့ Infrastructure ရဲ့ vpc, subnet, availability zone, security group, etc… တို့ကိုအရင်သတ်မှတ်ထားပေဖို့မလိုပါဘူး.
Terraform က resources တွေရဲ့ dependencies ကို Implicit and Explicit dependencies သဘောတရားကိုသုံးပြီး manage လုပ်ပါတယ်.
Implicit dependencies ဆိုတာက Terraform နဲ့ automate လုပ်မဲ့ resources တွေရဲ့ အခြေအနေကို Terraform က auto detect လုပ်တာကိုပြောတာပါ.
Explicit dependencies ဆိုတာက Terraform နဲ့ automate လုပ်မဲ့ resources တွေရဲ့ အခြေအနေကို ကိုယ်တိုင် manually သတ်မှတ်ပေးရတာကိုပြောတာပါ.
Terraform က ec2 instance တစ်ခု create လုပ်တဲ့အခါမှာ လိုအပ်တဲ့ vpc, subnet, availability zone, security group, etc… တို့ကိုအရင် သတ်မှတ်ထားလာ check လုပ်ပါတယ်, တကယ်လို့ မသတ်မှတ်ထားရသေရင် cloud provider api ကရတဲ့ resources တွေရဲ့ results တွေပေါ်ကိုမူတည်ပြီး auto သက်မှတ်ပေးပါတယ်.
Terraform က declarative language style ကိုသုံးထားလို့ ကျွန်တော်တို့ လိုချင်တဲ့ desired state ရဖို့လိုအပ်တဲ့အဆင့်တွေကို step by step သတ်မှတ်ပေးဖို့မလိုပါဘူး.
differences between Ansible and Terraform
Ansible and Terraform ရဲ့ခြားနားချက်ကို နမူနာကြည့်ပါ.

mutable and immutable infrastructure
mutable infrastructure
Ansible က mutable infrastructure ကို support လုပ်ပါတယ်. mutable infrastructure မှာ ကျွန်တော်တို့ web app တစ်ခုကို ansible ကိုသုံးပြီး deploy လုပ်ထားတယ်ဆိုပါတော့, web app ရဲ့ version 1.0 မှာ webserver ကို apache ကိုသုံးထားပါတယ်.
web app ရဲ့ version 2.0 မှာ webserver ကို nginx ကိုပြောင်းသုံးထားပါတယ်.
mutable infrastructure မှာ web app version 1.0 ကို deploy လုပ်ထားတဲ့ VM မှာဘဲ webserver ကို apache ကနေ ngnix ကိုupgrade လုပ်တာပါ. web app version 2.0 အတွက် VM အသစ် create လုပ်တာမဟုတ်ပါဘူး.
mutable infrastructure မှာ ကျွန်တော်တို့ upgrade လုပ်ခြင်တဲ့ အပိုင်းတွေကို upgrade လုပ်လို့ရပါတယ်.
immutable infrastructure
Terraform က immutable infrastructure ကို support လုပ်ပါတယ်.
immutable infrastructure မှာ ကျွန်တော်တို့ web app တစ်ခုကို Terraform ကိုသုံးပြီး deploy လုပ်ထားတယ်ဆိုပါတော့, web app ရဲ့ version 1.0 မှာ webserver ကို apache ကိုသုံးထားပါတယ်.
web app ရဲ့ version 2.0 မှာ webserver ကို nginx ကိုပြောင်းသုံးထားပါတယ်.
immutable infrastructure မှာ web app version 1.0 ကို deploy လုပ်ထားတဲ့ VM ကိုကျွန်တော်တို့ upgrade လုပ်ဖို့အဆင်မပြေပါဘူး, web app version 2.0 အတွက် VM အသစ်ကို create လုပ်ဖို့လိုပါတယ်.
immutable infrastructure မှာ ကျွန်တော်တို့ upgrade လုပ်ခြင်တဲ့ အပိုင်းတွေကို upgrade လုပ်ရတာ အဆင်မပြေပါဘူး.
immutable infrastructure မှာ ကျွန်တော်တို့ ရှိပြီးသာ infrastructure ကို upgrade မလုပ်ဘဲ infrastructure အသစ်တစ်ခုကိုတည်ဆောက်ပါတယ်.
ကျွန်တော်တို့ အသစ်တည်ဆောက်ထားတဲ့ infrastructure ကလိုချင်တဲ့ result or desired state ရပြီးဆိုရင် infrastructure အဟောင်းကို destroy လုပ်တဲ့နည်းကိုသုံးပါတယ်.
lifecycle or state management
Ansible and Terraform ရဲ့ခြားနားချက်က Terraform မှာ lifecycle management ရှိပါတယ်.
lifecycle management ဆိုတာက ကျွန်တော်တို့ aws မှာ ec2 instance တည်ဆောက်တဲ့နေရာမှာလိုအပ်တဲ့ resources (vpc, subnet, availability zone, security group, etc… ) တို့ရဲ့ state ကိုပြောတာပါ.
Terraform က resources တွေရဲ့ state ကိုမှတ်ထားပေးပါတယ်. တကယ်လို့ ကျွန်တော်တို့ Terraform နဲ့ provision လုပ်ထားတဲ့ resources တွေကိုပြန်ဖျက်ချင်ရင် Terraform က resources တွေရဲ့ state ကို auto မှတ်ထားပေးလို့ destroy လုပ်ရတာအဆင်ပြေပါတယ်.
Terraform ကိုသုံးပြီး aws မှာ ec2 instance တစ်ခုကို create လုပ်တယ်ထားပါတော့, Terraform က resources တွေရဲ့ state ကိုမှတ်ထားလို့ နောက်တစ်ကြိမ် Terraform code ကို run တဲ့အခါမှာ ec2 instance အသစ်တစ်ခုကို create မလုပ်ခင်မှာ ရှိပြီးသာ ec2 instance ကို destroy လုပ်ပါတယ်. ရှိပြီးသာ instance ကို destroy လုပ်ပြီးမှ instance အသစ်ကို create ပြန်လုပ်ပါတယ်.
Ansible ကိုသုံးပြီး aws မှာ ec2 instance တစ်ခုကို create လုပ်တယ်ထားပါတော့, Ansible မှာ Terraform လို resources တွေရဲ့ state ကိုမမှတ်ထားလို့ ansible playbook ကို ထက် run ရင် ec2 instance အသစ်တစ်ခုကို create ထက်လုပ်မှာပါ.
Ansible မှာလဲ lifecycle management ကိုအသုံးပြုချင်ရင်တော့ ansible playbook တွေကို resources တွေရဲ့ state တွေကို condition စစ်ပေးတဲ့ steps တွေကိုထည့်ရေးရမှာပါ.
idempotency
idempotency ဆိုတာကကျွန်တော်တို့အလုပ်တစ်ခုလုပ်ပြီး ကျွန်တော်တို့လုပ်တဲ့အလုပ်ကလိုချင်တဲ့ result or desired state တစ်ခုရတယ်ထားပါတော့, နောက်တစ်ခါကျွန်တော်တို့ကအရင်လုပ်တဲ့အလုပ်ကိုဘဲ ထက်လုပ်ရင် ရထားတဲ့ result or desired state ကမပြောင်းလဲဘဲဆက်ရှိနေရင် idempotent ရှိတယ်လို့ပြောလို့ရပါတယ်.
Terraform က idempotency ရှိပါတယ်. ကျွန်တော်တို့ Terraform နဲ့ provision လုပ်ထားတဲ့ resources တွေက desired state ရပြီးဆိုရင် နောက်တစ်ခါ Terraform နဲ့ provision ထက်လုပ်ရင် resources တွေရဲ့ state ကပြောင်းလဲမှာမဟုတ်ပါဘူး.
Terraform က resources တွေရဲ့ state ကို မှတ်ထားပါတယ်.
Terraform က configuration drift ( resources တွေရဲ့ state changes ဖြစ်တာကိုပြောတာပါ ) ကို auto သိပါတယ်.
Terraform code တွေကို run တဲ့အချိန်မှာ resources တွေရဲ့ state အခြေအနေကို Terraform နှိုင်းယှဉ်ပါတယ်, တကယ်လို့ resources တွေရဲ့ state က desired state ရှိပြီးတာဆိုရင် အပြောင်းအလဲမလုပ်ပါဘူး.
Ansible ကိုလဲ ansible modules တွေကိုသုံးပြီး idempotent ဖြစ်အောင်သုံးလို့ရပါတယ်.
templating
Terraform and Ansible နှစ်မျိုးလုံးက templating ကို support လုပ်ပါတယ်.
ကျွန်တော်တို့ က Terraform ကိုသုံးပြီး dev group အတွက် Infrastructure provisioning code ကိုရေးပေးထားပါတယ်ဆိုပါတော့,
ကျွန်တော်တို့ ရေးထားတဲ့ Infrastructure provisioing code ကို အခြား groups ( test, prod ) တို့မှာလဲပြန်သုံးလို့ရပါတယ်.
Terraform CLI
ကျွန်တော်တို့ Terraform ကိုလေ့လာတဲ့နေရာမှာ hands-on လေ့လာတာပိုကောင်းပါတယ်.
ကျွန်တော်က Terraform CLI ကို Linux OS မှာ install လုပ်ထားပါတယ်. Terraform CLI ကို Terraform Cloud နဲ့တွဲသုံးလို့ရပါတယ်.
ကျွန်တော်တို့ Terraform CLI ကို install လုပ်ကြည့်ရအောင်.ကျွန်တော်က ubuntu 20.04 မှာ install လုပ်ထားပါတယ်.
Terraform CLI ကို zip file or official package repository ကနေ install လုပ်လို့ရပါတယ်.
Terraform CLI ကို official repository က install လုပ်တာကိုနမူနာကြည့်ပါ.
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt install terraform

terraform --version
အခြား OS Environment ( Windows, macOS ) မှာ Terraform CLI ကို install လုပ်ချင်ရင်အောက်ပါ link မှာကြည့်ပါ.
Terraform Cloud
ကျွန်တော်တို့ Terraform မှာ provision လုပ်ထားတဲ့ resources တွေရဲ့ state ကို local or remote မှာသိမ်းထားလို့ရပါတယ်.
ကျွန်တော်တို့ Terraform Cloud ကိုသုံးပြီး Terraform ရဲ့ state ( life cycle ) ကို remote backend မှာသက်မှတ်လို့ရပါတယ်. Terraform Cloud ကိုသုံးပြီး CI/CD, group collaboration project တွေကိုလဲတည်ဆောက်လို့ရပါတယ်.
Terraform မှာ Terraform CLI , Terraform Cloud, Terraform Enterprise ဆိုပြီး Edition သုံးမျိုးလောက်ရှိပါတယ်.
- Terraform CLI က OSS ( open-source ) ပါ ကျွန်တော်တို့ local မှာ install လုပ်ပြီးသုံးလို့ရပါတယ်.
- Terraform Cloud က Terraform ရဲ့ SaaS ( Software as a Serive ) product ပါ, entry-level ကို free အသုံးပြုခွင့်ပေးထာပါတယ်.
- Terraform Enterprise က Terraform Cloud ရဲ့ self-hosted distribution ပါ, Enterprise တွေကသုံးတာများပါတယ်.
Terraform Edition တွေရဲ့ခြားနားချက်ကိုနမူနာကြည့်ပါ.

Terraform Cloud ကိုသုံးဖို့ Terraform Cloud မှာ account ကို အောက်ပါ link မှာ create လုပ်ပါ.
Terraform Cloud ကို remote backend အနေနဲ့သုံးဖို့ Terraform Cloud မှာ သက်မှတ်ဖို့လိုအပ်တာတွေကို ကြည့်ပါ.
- Organization
- Workspace
- API Token
Terraform Cloud မှာ Organization တစ်ခုကို သက်မှတ်ထားဖို့လိုပါတယ်.

ကျွန်တော်တို့ create လုပ်ထားတဲ့ Organization ထဲမှာ workspace တွေကို သက်မှတ်လို့ရပါတယ်.
ကျွန်တော်တို့ workspace ရဲ့ workflow ကို git , CLI, API စသည်ဖြင့်သက်မှတ်လို့ရပါတယ်.


ကျွန်တော်တို့ Terraform Cloud ကို remote backend အနေနဲ့သုံးဖို့လိုအပ်တဲ့ API Token ကို create လုပ်ပါ.



ကျွန်တော်တို့ Terraform Cloud ကနေရလာတဲ့ API Token ကို Terraform CLI မှာသက်မှတ်ပါ.
terraform login

ကျွန်တော်တို့ API Token ကို Terraform CLI ကနေသက်မှတ်ပြီးရင် Terraform Cloud ကို remote backend အနေနဲ့သုံးလို့ရပါပြီး.

Hands-on Lab
ကျွန်တော်တို့ Ansible and Terraform ကိုသုံးပြီး nginx web server တစ်ခုကို AWS EC2 ပေါ်မှာ Deploy လုပ်ကြည့်ရအောင်.
ကျွန်တော်က AWS Cloud9 IDE ကိုသုံးထားပါတယ်, ကိုယ်ကြိုက်နှစ်သက်ရာ IDE ကိုသုံးလဲရပါတယ်.
ကျွန်တော်တို့လိုအပ်တဲ့ Ansible, Terraform package တွေကို install လုပ်ဖို့လိုပါတယ်.
ကျွန်တော်တို့ AWS CLI ကိုလဲ Install လုပ်ထားဖို့လိုပါတယ်. ကျွန်တော်က AWS CLI ကို profile ခွဲပြီးသုံးထားပါတယ်.
AWS ACCESS KEY and SECRET ACCESS KEY ကို profile ခွဲပြီး သက်မှတ်ထားလို့ Terraform code မှာ profile ကိုထည့်ပြီးသုံးရတာ အဆင်ပြေပါတယ်.
တစ်ကယ်လို့ Terraform code မှာထည့်ရေးချင်ရင်လဲ ရေးလို့ရပါတယ်.
provider "aws" {
region = "us-west-2"
access_key = "my-access-key"
secret_key = "my-secret-key"
}
Environment Variables ကိုသုံးလဲရပါတယ်.
export AWS_ACCESS_KEY_ID="anaccesskey"
export AWS_SECRET_ACCESS_KEY="asecretkey"
export AWS_DEFAULT_REGION="us-west-2"
AWS CLI ကို install လုပ်တာနဲ့ပါတ်သက်ပြီး ကျွန်တော် အရင်က sharing လုပ်ထားတာရှိပါတယ်.
Ansible ကို install လုပ်တာနဲ့ပါတ်သက်ပြီး ကျွန်တော်အရင်က sharing လုပ်ထားတာရှိပါတယ်.
Ansible and Terraform ကို install လုပ်ပြီးရင် ကျွန်တော်တို့ AWS Cloud Infra မှာ nginx web server တစ်လုံးကို Automation ကိုသုံးပြီး Deploy လုပ်ကြည့်ရအောင်.
ကျွန်တော်တို့ Terraform code တွေကိုသုံးပြီး AWS EC2 မှာ instance တစ်ခုကို create လုပ်ပါမယ်.
ကျွန်တော်တို့ Ansible and Terraform မှာသုံးတဲ့ code တွေကို github မှာတင်ထားပါတယ်.
github_sample_code
ကျွန်တော်တို့ Terraform code ကိုမ run ခင်မှာ AWS EC2 keypair တစ်ခုကို create လုပ်ထားဖို့လိုပါတယ်.

ကျွန်တော်တို့ create လုပ်ထားတဲ့ pem key file ကို project directory မှာသက်မှတ်ထားပေးဖို့လိုပါတယ်.

ကျွန်တော်က Terraform ရဲ့ state ကို Terraform Cloud မှာ remote backend အနေနဲ့သုံးထားပါတယ်.
ကျွန်တော်တို့ Terraform Cloud ကို remote backend အနေနဲ့သုံးတဲ့နေရာမှာ execution mode ကို Local အနေနဲ့သုံးထားပါတယ်.
Terraform Cloud ရဲ့ workspace မှာသက်မှတ်လို့ရပါတယ်.

ကျွန်တော်တို့ local backend ကိုသုံးလဲရပါတယ်.

terraform init
ကျွန်တော်တို့ terraform init က လိုအပ်တဲ့ terraform provider plugins, modules တွေကို install လုပ်တာပါ.
terraform plan

ကျွန်တော်တို့ terraform plan ကိုသုံးပြီး terraform က automate လုပ်မဲ့ execution plan ကိုကြည့်လို့ရပါတယ်.
ကျွန်တော်တို့ terraform code ထဲကနေ ansible playbook တွေကိုသုံးပြီး nginx server ကို ansible နဲ့ install လုပ်မှာပါ.
Ansible ကို terraform code ထဲကနေ local-exec ကိုသုံးပြီးခေါ်တဲ့နေရာမှာ localhost မှာ ansible ကိုအရင် install လုပ်ထားဖို့လိုပါတယ်.
ansible --version

ကျွန်တော်တို့ terraform code ကို run ကြည့်ရအောင်.
terraform apply


ကျွန်တော်တို့ AWS EC2 console မှာ check လုပ်ကြည့်ရအောင်, webserver ဆိုတဲ့ EC2 instance တစ်ခုကို တွေ့ရပါမယ်.

ကျွန်တော်တို့ terraform ရဲ့ state file ကို terraform cloud မှာ ကျွန်တော်တို့ သက်မှတ်ထားတဲ့ workspace မှာကြည့်လို့ရပါတယ်.

ကျွန်တော်တို့ terraform code ကို run လို့ရလာတဲ့ ec2 ရဲ့ public_ip ကိုကိုယ်ကြိုက်ရာ browser မှာ check လုပ်ကြည့်ရအောင်. ကျွန်တော်က google chrome ကိုသုံးထားပါတယ်.

ဒါဆိုရင် ကျွန်တော်တို့ terraform and ansible ကိုသုံးပြီး AWS Cloud မှာ web server တစ်လုံးကို deploy လုပ်တာ အဆင်ပြေပါပြီး.
ကျွန်တော်တို့ hands-on lab ကို စမ်းပြီးရင် terraform destroy ကိုသုံးပြီး resources တွေကို delete လုပ်လို့ရပါတယ်.
terraform destroy
ကျွန်တော်တို့ Terraform and Ansible ကိုတွဲသုံးပြီး Automation ( IaC + CaC ) ကိုသုံးရတာအဆင်ပြေပါတယ်.
Ansible နဲ့ပါတ်သက်ပြီး ကျွန်တော်အရင် sharing လုပ်ထားတာတွေရှိပါတယ်.
ကျွန်တော်တို့ Terraform and Ansible ကိုဆက်လေ့လာလိုလျှင်.
https://www.ansiblefordevops.com/