关于Terraform中main.tf和tfstate文件的创建和运维方法

简要描述

这次使用Terraform

    • main.tf

 

    変数を格納するvariables.tf

我想要解释一下关于其创建和操作方法的内容。

假设

    • 東京リージョンを使用

 

    • AWSを使用

 

    S3バケットとDynamoDBの作成方法について理解している

使用容器方式進行Terraform部署,可以避免在多個項目中意識版本差異的問題。如果您希望使用容器,也可以參考這篇文章。

 

目录结构

构成如下:

├── main.tf
└── variables.tf
    • variables.tf

 

    main.tf

我们按照这个顺序开始创建吧。

variables.tf 文件。

Note: The response provided is in simplified Chinese.

我們將在variables.tf中記錄共用的變數。

# ------------------------------
# Variables
# ------------------------------

# プリフィックスを設定
variable "prefix" {
  default = "tf-pg"
}

# プロジェクトを識別する一意の識別子を設定
variable "project" {
  default = "terraform-playground"
}

# プロジェクトのオーナーを設定
variable "owner" {
  default = "shun198"
}

主.tf

在main.tf中

    • tfstateファイルの管理方法

 

    • 使用するプロバイダ(今回はAWS)およびそのプロバイダのバージョン制約

 

    Terraformのバージョン制約

我将继续记录各种等等的内容。

# ------------------------------
# Terraform configuration
# ------------------------------
terraform {
  # tfstateファイルを管理するようbackend(s3)を設定
  backend "s3" {
    bucket         = "terraform-playground-for-cicd"
    key            = "terrafrom-playground.tfstate"
    region         = "ap-northeast-1"
    encrypt        = true
    dynamodb_table = "terraform-playground-tf-state-lock"
  }
  # プロバイダを設定
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  # Terraformのバージョン制約
  required_version = ">= 1.2.0"
}

# ------------------------------
# Provider
# ------------------------------
# プロバイダ(AWS)を指定
provider "aws" {
  region = "ap-northeast-1"
}

# ------------------------------
# Locals
# ------------------------------
locals {
  # variables.tfから変数を取得
  # terraformのworkspaceの一覧から該当するworkspace(dev,stg,prdなど)を取得
  prefix = "${var.prefix}-${terraform.workspace}"
  common_tags = {
    Environmnet = terraform.workspace
    Project     = var.project
    Owner       = var.owner
    ManagedBy   = "Terraform"
  }
}

# ------------------------------
# Current AWS Region(ap-northeast-1)
# ------------------------------
# 現在のAWS Regionの取得方法
data "aws_region" "current" {}

关于tfstate文件的管理方法

使用Terraform时,我们通过tfstate文件来管理与基础设施相关的信息。考虑到多人协作开发的情况,

    • S3バケット

 

    DynamoDB

一般来说,使用同一个tfstate文件在团队中共享进行开发是普遍的做法。

S3桶

本次我们将在S3存储桶中管理tfstate文件。
我将存储桶命名为terraform-playground-for-cicd。
由于存储桶名必须是唯一的,请在创建除此名称以外的S3存储桶后,写下存储桶名。

创建桶时,请将其设置为私有。
スクリーンショット 2023-01-08 8.10.49.png
スクリーンショット 2023-01-08 8.12.23.png

DynamoDB 可表述为 “亚马逊 DynamoDB”。

为了避免多人同时执行terraform并更新tfstate时发生冲突,可以使用DynamoDB对状态进行锁定。本次使用terraform-playground-tf-state-lock作为表名,LockID作为主键。

スクリーンショット 2023-01-08 8.21.29.png
  backend "s3" {
    bucket         = "terraform-playground-for-cicd"
    key            = "terrafrom-playground.tfstate"
    region         = "ap-northeast-1"
    encrypt        = true
    dynamodb_table = "terraform-playground-tf-state-lock"
  }

 

当地人民

与variables类似,可以设置变量。
与variables不同,locals可以将多个变量组合在一起使用。
本次将创建可在所有服务中共享的标签。

locals {
  # variables.tfから変数を取得
  # terraformのworkspaceの一覧から該当するworkspace(dev,stg,prdなど)を取得
  prefix = "${var.prefix}-${terraform.workspace}"
  common_tags = {
    Environmnet = terraform.workspace
    Project     = var.project
    Owner       = var.owner
    ManagedBy   = "Terraform"
  }
}
スクリーンショット 2023-01-08 9.43.34.png

工作空间

在Terraform中,当在同一AWS账户上管理多个环境时,可以使用workspace。本次我们将创建一个dev环境的workspace。

terraform workspace new dev

我会创建dev的工作空间,确认工作空间已经创建并切换到dev。

terraform workspace list
  default
* dev

关于如何获取当前的AWS Region的方法

按照公式文件上所述的内容。

data "aws_region" "current" {}

通过 使用这个选项 可以获取到当前的 AWS 区域,即 东京区域。
这很方便,因为不需要在 AZ 设置等中进行硬编码。

 

关于所指定的提供商区域。

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

只有一个选项,原文如下:

也许你可能会认为将region设为data.aws_region.current就可以了!但是由于会产生循环错误(双向引用错误),所以在这里可惜的是

region = "ap-northeast-1"

需要进行硬编码

初始化

完成以上设置后,请执行以下命令。

terraform init

总结

在使用Terraform进行多人开发时,需要做很多准备工作,所以要记住的东西比我想象的多,非常辛苦。希望您能把这篇文章当作一张备忘录来使用。

请在以下内容中提供一个中文的同义词:
参考

 

bannerAds