เขียน Terraform ตั้งค่า IAM Amplify ฉบับเข้าใจง่าย

หากเพื่อน ๆ น้อง ๆ คนไหนกำลังแวะมาอ่านบทความนี้ อยากบอกว่าบทความนี้จะมาอธิบายและขั้นตอนการติดต่อรวมถึงการใช้งานพื้นฐาน โดยจะอธิบายองค์ประกอบคร่าว ๆ
Terraform คือ ?
Terraform เป็นเครื่องมือที่ใช้จัดการ infrastructure ได้อย่างปลอดภัยและมีประสิทธิภาพ โดยใช้ configuration syntax ที่สั้นและเข้าใจง่าย (infrastructure as code)
องค์ประกอบของ Terraform ประกอบด้วย
providers - ชื่อผู้ให้บริการ เช่น aws, google, azure เป็นต้น
resources - ทรัพยากรเฉพาะที่มีการจัดเตรียมเช่น aws_iam_role สำหรับ aws
variable - ใช้ในการประกาศตัวแปรอินพุต
output - ใช้ในการประกาศตัวแปรเอาต์พุตที่จะเก็บไฟล์สถานะ Terraform
local - กำหนดค่าให้กับนิพจน์ ตัวแปรเหล่านี้เป็นตัวแปรชั่วคราวในเครื่องที่ทำงานร่วมกับโมดูล
module - คอนเทนเนอร์สำหรับทรัพยากรหลายรายการที่ใช้ร่วมกัน
data - รวบรวมข้อมูลจากผู้ให้บริการระยะไกลและบันทึกเป็นแหล่งข้อมูล
Amplify คือ ?
AWS Amplify เป็นเครื่องมือที่ใช้จัดการ เว็บไซต์และแอปพลิเคชั่นแบบเต็มรูปแบบ พร้อมใช้งานง่ายต่อการทำงาน โดยสามารถทำงานได้ทั้ง Server-Side และ Client-Side
ขั้นตอนการเตรียมตัว
อย่างแรกสิ่งที่จำเป็นจะต้องเตรียมความพร้อมและต้องมีได้แก่
- Terraform
- Vscode (ส่วนตัวหากเพื่อน ๆ คนไหนมีโปรแกรมที่ชื่นชอบ IDE ก็เลือกตัวนั้นได้เลย)
- Single Sign-On AWS & Setup Profile AWS ให้เรียบร้อย
- สร้างโฟลเดอร์ Project
mkdir <project_name> # สร้างโฟลเดอร์
cd <project_name> # เข้าโฟลเดอร์
touch main.tf # สร้างไฟล์
touch variables.tf # สร้างไฟล์
touch version.tf # สร้างไฟล์
ขั้นตอนการเขียน
อย่างแรกเตรียมการ ใช้งาน terraform อ้างอิงว่าเราต้องการใช้งาน aws ก่อน โดยทำการเรียกใช้งาน
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
อย่างสอง ประกาศตัวแปรอินพุต โดยเราจะจำลอง Default ว่า amplify-demo-role
variable "role_name" {
description = "Role Name"
type = string
default = "amplify-demo-role"
}
อย่างสาม ใช้ในการจัดการทรัพยากร โดยเราจะทำการสร้าง data เรียกใช้ aws_iam_policy_document
data "aws_iam_policy_document" "assume_role" {
}
เราจะต้องตั้งค่า statement ก่อน โดยกำหนด policy ให้ทำอะไรบ้าง เราต้องการให้ทำงานบน amplify ได้ตาม Default ก็ให้ทำการ Setting ตามนี้
data "aws_iam_policy_document" "assume_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["amplify.amazonaws.com"]
}
}
}
ขั้นตอนนี้จะเป็นการเรียกใช้งาน resource "aws_iam_role" และประกาศตัวแปรสำคัญ 3 ส่วนได้แก่ name, assume_role_policy, managed_policy_arns
data "aws_iam_policy_document" "assume_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["amplify.amazonaws.com"]
}
}
}
resource "aws_iam_role" "this" {
name = var.role_name
assume_role_policy = join("", data.aws_iam_policy_document.assume_role.*.json)
}
ขั้นตอนการใช้งาน
terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 4.16"...
- Installing hashicorp/aws v4.17.0...
- Installed hashicorp/aws v4.17.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
terraform validate
Success! The configuration is valid.
terraform apply
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_iam_role.this will be created
+ resource "aws_iam_role" "this" {
+ id = "amplify-demo-role-XXXXXXXXXXXXXX"
+ policy_arn = "arn:aws:iam::XXXXXXXX:role/amplify-demo-role"
+ tagroles = "amplify-demo-role"
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value:
Enter a value: yes
aws_iam_role.this: Creating...
aws_iam_role.this: Still creating... [10s elapsed]
aws_iam_role.this: Still creating... [20s elapsed]
aws_iam_role.this: Still creating... [30s elapsed]
aws_iam_role.this: Creation complete after 36s [id=i-01e03375ba238b384]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
ดูผลลัพธ์บน AWS Role

เท่านี้ก็เป็นอันเสร็จสิ้น 👍 🥳🥳🥳🥳🥳🥳🥳
หากท่านชอบและรู้สึกว่ามีประโยชน์ รบกวนแชร์และแบ่งปันให้ผู้ที่สนใจและควรจะได้รับการเข้าถึงบทความนี้ แล้วพบกันใหม่ครับ