Cấu hình Terraform sử dụng Module trên Terraform Registry

Cấu hình sử dụng Module trên Terraform RegistryCuongquach.com | Khi sử dụng Terraform để cấu hình quản lý các resource hạ tầng như Cloud hay On-Premise, sẽ có những lúc bạn lười chảy ke chả muốn viết một cái module terraform nào cả ? Hay lười tìm hiểu các resource cấu hình sắp xếp ra sao,.. Vậy bạn cũng có thể sử dụng một Module Public trên Terraform Registry để nhanh chóng khởi tạo các resource hạ tầng mình mong muốn đấy.

cau-hinh-terraform-xai-module-tren-terraform-registry

Terraform Registry là gì ?

Link: https://registry.terraform.io/

Terraform Registry là gì ?
Terraform Registry là gì ?

Terraform Registry là dịch vụ online của Hashicorp Terraform chứa toàn bộ nguồn của các Provider và các bản module terraform đã được các nhà phát triển khác lập trình rồi được Hashicorp chứng thực kiểm tra chất lượng.

Từ Terraform Registry, bạn có thể sử dụng trực tiếp các bản module đã được public một cách dễ dàng và thoải mái. Vì mặc định Terraform sẽ luôn tích hợp kết nối tới Terraform Registry để tìm các Provider và Module khi được chỉ định đúng cú pháp.

Khi thực hiện terraform init hay terraform get , thì Terraform sẽ đi tải các Provider từ Terraform Registry và tương tự với Module nào bạn muốn xài từ Terraform Registry.

Trong bài viết này, ta chỉ đề cập đến Public Terraform Registry. Không đề cập Private Terraform Registry.

Terraform Registry Modules

Link: https://registry.terraform.io/browse/modules

Tìm kiếm các module đã được public và đảm bảo chất lượng ở trên Registry mà bạn muốn sử dụng.

Cú pháp:

<NAMESPACE>/<NAME>/<PROVIDER>

Ví dụ:

terraform-aws-modules/vpc/aws

Ví dụ module VPC với Provider AWS
https://registry.terraform.io/modules/terraform-aws-modules/vpc/aws/2.48.0

Bạn sẽ chỉ cần đơn giản chỉ định thông tin modulephiên bản mà bạn muốn sử dụng. Sau đó đọc tài liệu của nhà phát triển phát hành để hiểu các thông tin Input Params cần đưa vào sử dụng.

provider "aws" {
  region = "ap-southeast-1"
}

data "aws_security_group" "default" {
  name   = "default"
  vpc_id = module.vpc.vpc_id
}

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "2.48.0"

  name = "simple-vpc-cuongquach-lab"

  cidr = "10.0.0.0/16"

  azs             = ["ap-southeast-1a", "ap-southeast-1b", "ap-southeast-1c"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]

  enable_ipv6 = true

  enable_nat_gateway = false
  single_nat_gateway = false

  public_subnet_tags = {
    Name = "overridden-name-public"
  }

  tags = {
    Owner       = "user"
    Environment = "dev"
  }

  vpc_tags = {
    Name = "simple-vpc-cuongquach-lab"
  }
}

Quá trình chứng thực với AWS , thì mình export ENV Credentials IAM Key. Thực hiện terraform init, planapply nào. Phần plan output dài quá , nên mình cắt bớt đoạn output .

# export AWS_ACCESS_KEY_ID="xxxx"
# export AWS_SECRET_ACCESS_KEY="xxxx"
# export AWS_DEFAULT_REGION="ap-southeast-1"
#  terraform init
Initializing modules...
Downloading terraform-aws-modules/vpc/aws 2.48.0 for vpc...
- vpc in .terraform/modules/vpc/examples/simple-vpc
- vpc.vpc in .terraform/modules/vpc

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (hashicorp/aws) 3.4.0...

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.

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create
 <= read (data resources)

Terraform will perform the following actions:

  # data.aws_security_group.default will be read during apply
  # (config refers to values not yet known)
 <= data "aws_security_group" "default"  {
      + arn         = (known after apply)
      + description = (known after apply)
      + id          = (known after apply)
      + name        = "default"
      + tags        = (known after apply)
      + vpc_id      = (known after apply)
    }

  # module.vpc.aws_egress_only_internet_gateway.this[0] will be created
  + resource "aws_egress_only_internet_gateway" "this" {
      + id     = (known after apply)
      + tags   = {
          + "Environment" = "dev"
          + "Name"        = "simple-vpc-cuongquach-lab"
          + "Owner"       = "user"
        }
      + vpc_id = (known after apply)
    }

  # module.vpc.aws_internet_gateway.this[0] will be created
  + resource "aws_internet_gateway" "this" {
      + arn      = (known after apply)
      + id       = (known after apply)
      + owner_id = (known after apply)
      + tags     = {
          + "Environment" = "dev"
          + "Name"        = "simple-vpc-cuongquach-lab"
          + "Owner"       = "user"
        }
      + vpc_id   = (known after apply)
    }

...
...
...
Plan: 24 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

Khá là đơn giản để sử dụng các Module đã được publish và kiểm chứng bởi Hashicorp trên Terraform Registry phải không nào ?!

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

Previous articleTìm hiểu cấu hình Workspace trong Terraform
Next articleCấu hình quy định phiên bản Terraform và Terraform Provider
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 !