Cấu hình ‘user_data’ trong resource AWS Terraform

Cấu hình ‘user_data’ trong resource AWS TerraformCuongquach.com | Mục cấu hình ‘user_data‘ thường gặp trong các resource Terraform liên quan đến máy chủ Instance như : aws_instace, aws_launch_configuration, … Với nội dung là cung cấp đoạn shell script sẽ được chạy khi Instance khởi tạo. Chúng ta sẽ cùng điểm qua một số lưu ý khi cấu hình ‘user_data‘ với Terraform nhé.
cau-hinh-user-data-trong-resource-aws-terraform

Cấu hình ‘user_data’ trong resource AWS Terraform

Cách 1:
– Bạn sẽ sử dụng EOF để khởi tạo một đoạn script nhiều dòng mà bạn muốn truyền vào phần cấu hình ‘user_data‘ .

provider "aws" {
    region = "us-east-1"
}

resource "aws_key_pair" "terraform-demo" {
  key_name   = "terraform-demo"
  public_key = "${file("terraform-demo.pub")}"
}

resource "aws_instance" "my-instance" {
    ami = "ami-04169656fea786776"
    instance_type = "t2.nano"
    key_name = "${aws_key_pair.terraform-demo.key_name}"
    user_data = << EOF
        #!/bin/bash
        sudo yum update
        sudo yum install -y httpd
        sudo systemctl start httpd
        sudo systemctl enable httpd
        echo "<h1>Deployed via Terraform - Terraform.Cuongquach.com</h1>" | sudo tee /var/www/html/index.html
    EOF
    tags = {
        Name = "terraform.cuongquach.com"   
    }
}

Cách 2:
– Sử dụng hàm file() trong Terraform, để chỉ định file shell script có nội dung mà bạn muốn thực thi khi khởi tạo Instance.
– Ví dụ mình tạo một thư mục cùng cấp với file Terraform đang cấu hình resource, để chứa file shell script.

# mkdir templates
# cd templates
# vi install_httpd.sh

#!/bin/bash
sudo yum update
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
echo "<h1>Deployed via Terraform - Terraform.Cuongquach.com</h1>" | sudo tee /var/www/html/index.html
# tree ./
./
├── main.tf
├── output.tf
├── templates
│   └── install_httpd.sh
└── variables.tf

1 directory, 4 files

– Ở trong phần cấu hình resource ta sử dụng function file() khi cấu hình mục ‘user_data‘.

provider "aws" {
    region = "us-east-1"
}

resource "aws_key_pair" "terraform-demo" {
  key_name   = "terraform-demo"
  public_key = "${file("terraform-demo.pub")}"
}

resource "aws_instance" "my-instance" {
    ami = "ami-04169656fea786776"
    instance_type = "t2.nano"
    key_name = "${aws_key_pair.terraform-demo.key_name}"
    user_data = "${file("templates/install_httpd.sh")}"
    tags = {
        Name = "terraform.cuongquach.com"   
    }
}

– Rồi khi bạn chạy ‘terraform plan‘ hay ‘terraform apply‘ sẽ thấy thông tin hash của file script.

# terraform plan
...
      + user_data   = "9d882ebd765036457001b78c0171433ab225c5ee"
...

Đơn giản phải không nào ?! Có lẽ sẽ còn nhiều thủ thuật khác, nhưng về cơ bản ta có thể áp dụng 2 cách trên khi cấu hình mục ‘user_data’ cho một số resource trong Terraform.

Nguồn: https://cuongquach.com/

Previous articleRansomware là gì ? Cách phòng tránh Ransomware
Next articleTạo/xoá thông tin Jenkins Node trên Jenkins bằng CLI
Bạn đang theo dõi website "https://cuongquach.com/" nơi lưu trữ những kiến thức tổng hợp và chia sẻ cá nhân về Quản Trị Hệ Thống Dịch Vụ & Mạng, được xây dựng lại dưới nền tảng kinh nghiệm của bản thân mình, Quách Chí Cường. Hy vọng bạn sẽ thích nơi này !