Linux システムで Terraform をインストール、設定、実行して、Alibaba Cloud リソースをプロビジョニングします。
Terraform を使用した Infrastructure as Code (IaC) によるインフラストラクチャ管理は、主に次の手順で構成されます。
-
Terraform の設定。Alibaba Cloud プロバイダーの認証を設定します。
-
Terraform 設定ファイルの作成。RAM ユーザーやポリシーなど、作成するリソースを定義します。
-
リソースの初期化と作成。プロバイダープラグインをダウンロードし、宣言されたリソースをデプロイします。
-
リソースの表示と管理。要件の変更に応じて、デプロイされたリソースを確認、変更、または更新します。
-
リソースの破棄。不要になったリソースを削除します。
1. Terraform のインストール
Terraform は、宣言型の設定ファイルを通じてクラウドリソースを管理するためのコードとしてのインフラストラクチャ (IaC) ツールです。Terraform コマンドを実行する前に Terraform をインストールしてください。インストール手順の詳細については、「Terraform のインストール」をご参照ください。
2. Terraform の設定
Terraform は、Alibaba Cloud プロバイダーを使用して Alibaba Cloud の認証を行います。複数の認証方法がサポートされています。詳細については、「Terraform 認証」をご参照ください。
次の例では、環境変数で設定された RAM ユーザーの AccessKey ペアを使用します。
export ALICLOUD_ACCESS_KEY="<yourAccessKeyID>"
export ALICLOUD_SECRET_KEY="<yourAccessKeySecret>"
export ALICLOUD_REGION="cn-beijing"
3. Terraform 設定ファイルの作成
設定ファイルでは、RAM ユーザー、ECS インスタンス、OSS バケットなど、デプロイするインフラストラクチャリソースを定義します。
-
ram という名前の作業ディレクトリと、main.tf という名前の設定ファイルを作成します。
# 作業ディレクトリを作成し、そのディレクトリに移動します。 mkdir ram && cd ram # 設定ファイルを作成して編集します。 touch main.tf && vim main.tfプロジェクトごとに作業ディレクトリを分けることで、ステートファイルが分離され、バージョン管理が簡素化されます。
-
Terraform 設定ファイルを作成します。この例では、RAM ユーザーを作成し、そのユーザーに ECS の管理権限を付与します。
この例では、次のリソースを使用します。
重要責任を明確にし、誤用を防ぐために、同じ RAM ユーザーに対してコンソールログインと AccessKey ペアによるアクセスの両方を有効にすることは避けてください。
リソース
説明
RAM ユーザーの作成
RAM ユーザーのコンソールへのログインを許可
RAM ユーザーの AccessKey ペアの作成
アクセスポリシーの作成
RAM ユーザーへの権限付与
次のコードを main.tf にコピーします。
Escキーを押して挿入モードを終了し、:wqと入力して Enter キーを押し、ファイルを保存します。variable "user_name" { default = "terraform_user_test" } variable "user_password" { default = "!Test@123456" } variable "user_display_name" { default = "TestAccount" } variable "user_mobile" { default = "86-18688888888" } variable "user_email" { default = "example@example.com" } resource "alicloud_ram_user" "user" { name = var.user_name display_name = var.user_display_name mobile = var.user_mobile email = var.user_email comments = "Created by Terraform" force = true } resource "alicloud_ram_login_profile" "profile" { user_name = alicloud_ram_user.user.name password = var.user_password } resource "alicloud_ram_access_key" "ak" { user_name = alicloud_ram_user.user.name secret_file = "accesskey.txt" } resource "alicloud_ram_policy" "policy" { policy_name = "tf-example-policy" policy_document = <<EOF { "Statement": [ { "Action": "ecs:*", "Effect": "Allow", "Resource":"*" } ], "Version": "1" } EOF description = "This is a policy test." } resource "alicloud_ram_user_policy_attachment" "attach" { policy_name = alicloud_ram_policy.policy.policy_name policy_type = alicloud_ram_policy.policy.type user_name = alicloud_ram_user.user.name }
4. リソースの初期化と作成
リソースを作成する前に、作業ディレクトリを初期化します。
4.1 Terraform の初期化
terraform init を実行して、作業ディレクトリを初期化します。terraform init は Alibaba Cloud プロバイダープラグインをダウンロードします。このコマンドは、他の Terraform コマンドより先に実行する必要があります。
4.2 リソースの作成
-
terraform planを実行して、terraform applyが作成、変更、または破棄するリソースをプレビューします。# alicloud_ram_user.user will be created + resource "alicloud_ram_user" "user" { + comments = "Created by Terraform" + display_name = "TestAccount" + email = "example@example.com" + force = true + id = (known after apply) + mobile = "86-18688888888" + name = "terraform_user_test" } # alicloud_ram_user_policy_attachment.attach will be created + resource "alicloud_ram_user_policy_attachment" "attach" { + id = (known after apply) + policy_name = "tf-example-policy" + policy_type = (known after apply) + user_name = "terraform_user_test" } Plan: 5 to add, 0 to change, 0 to destroy. -
terraform applyを実行すると、terraform planが生成した実行計画に基づいてリソースが作成されます。作成プロセス中にプロンプトが表示されたら、yes と入力してリソースの作成を続行してください。変数に値を渡す方法の詳細については、「変数」をご参照ください。Plan: 5 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: yes alicloud_ram_user.user: Creating... alicloud_ram_policy.policy: Creating... alicloud_ram_policy.policy: Creation complete after 0s [id=tf-example-policy] alicloud_ram_user.user: Creation complete after 1s [id=20xxx18] alicloud_ram_login_profile.profile: Creating... alicloud_ram_access_key.ak: Creating... alicloud_ram_user_policy_attachment.attach: Creating... alicloud_ram_user_policy_attachment.attach: Creation complete after 0s [id=user:tf-example-policy:Custom:terraform_user_test] alicloud_ram_login_profile.profile: Creation complete after 0s [id=terraform_user_test] alicloud_ram_access_key.ak: Creation complete after 1s [id=LTxxxGr] Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
5. リソースの表示と管理
デプロイ後、インフラストラクチャを管理および保守します。
5.1 リソースの表示
-
terraform showを実行して、リソースの詳細を表示します。[root@iZ2***tZ ram]# terraform show # alicloud_ram_access_key.ak: resource "alicloud_ram_access_key" "ak" { id = "LTAxxxxxxxxxxxxxxxxxxxiGr" secret = (sensitive value) secret_file = "accesskey.txt" status = "Active" user_name = "terraform_user_test" } # alicloud_ram_login_profile.profile: resource "alicloud_ram_login_profile" "profile" { id = "terraform_user_test" mfa_bind_required = true password = (sensitive value) password_reset_required = false user_name = "terraform_user_test" } # alicloud_ram_policy.policy: resource "alicloud_ram_policy" "policy" { attachment_count = 0 default_version = "v1" description = "This is a policy test." document = <<-EOT { "Statement": [ { "Action": "ecs:*", "Effect": "Allow", "Resource":"*" } ], "Version": "1" } EOT } -
terraform state listを実行して、作成されたすべてのリソースを一覧表示します。[root@iZxxxmtZ ram]# terraform state list alicloud_ram_access_key.ak alicloud_ram_login_profile.profile alicloud_ram_policy.policy alicloud_ram_user.user alicloud_ram_user_policy_attachment.attach -
terraform state show <resource type>.<resource name>を実行して、特定のリソースの詳細を表示します。[root@iZxxx tZ ram]# terraform state show alicloud_ram_user.user # alicloud_ram_user.user: resource "alicloud_ram_user" "user" { comments = "Created by Terraform" display_name = "TestAccount" email = "example@example.com" force = true id = "208475129836212018" mobile = "86-18688888888" name = "terraform_user_test" } -
Alibaba Cloud 管理コンソールでリソースを確認します。
5.2 リソースの管理
Terraform は、リソースのステータスとプロパティを terraform.tfstate ファイルに保存します。ステートの管理には terraform state コマンドを使用します。ステートメカニズムの概要。
5.3 リソースの変更
-
main.tf のリソース定義を編集します。たとえば、RAM ユーザーの権限を読み取り専用の ECS アクセスに制限します。
-
vim main.tfコマンドを実行し、i キーを押して編集モードに入ります。 -
次のコードをコピーして、main.tf ファイル内の alicloud_ram_policy セクションを置き換えます。
resource "alicloud_ram_policy" "policy" { policy_name = "tf-example-policy" policy_document = <<EOF { "Statement": [ { "Action": [ "ecs:Get*", "ecs:List*", "ecs:Describe*" ], "Effect": "Allow", "Resource":"*" } ], "Version": "1" } EOF description = "This is a policy test." } -
Esc キーを押し、:wq と入力してファイルを保存します。
-
-
terraform planを実行して変更をプレビューします。Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the follow ~ update in-place Terraform will perform the following actions: # alicloud_ram_policy.policy will be updated in-place ~ resource "alicloud_ram_policy" "policy" { id = "tf-example-policy" name = "tf-example-policy" ~ policy_document = <<-EOT { "Statement": [ { - "Action": "ecs:*", + "Action": [ + "ecs:Get*", + "ecs:List*", + "ecs:Describe*" + ], "Effect": "Allow", "Resource":"*" } ], "Version": "1" } EOT # (8 unchanged attributes hidden) # (1 unchanged block hidden) } Plan: 0 to add, 1 to change, 0 to destroy. -
計画が正しければ、
terraform applyを実行します。プロンプトが表示されたら、yesと入力して変更を適用します。Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the follow ~ update in-place Terraform will perform the following actions: # alicloud_ram_policy.policy will be updated in-place ~ resource "alicloud_ram_policy" "policy" { id = "tf-example-policy" name = "tf-example-policy" ~ policy_document = <<-EOT { "Statement": [ { - "Action": "ecs:*", + "Action": [ + "ecs:Get*", + "ecs:List*", + "ecs:Describe*" + ], "Effect": "Allow", "Resource":"*" } ], "Version": "1" } EOT # (8 unchanged attributes hidden) # (1 unchanged block hidden) } Plan: 0 to add, 1 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: yes alicloud_ram_policy.policy: Modifying... [id-tf-example-policy] alicloud_ram_policy.policy: Modifications complete after 0s [id-tf-example-policy] Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
6. リソースの破棄
terraform destroy を実行して、現在の作業ディレクトリで作成したすべてのリソースを破棄します。
ポリシーに複数のバージョンがある場合、terraform destroy コマンドでは直接破棄できません。alicloud_ram_policy で force を true に設定すると、すべてのポリシーバージョンが削除されます。これにより、RAM ユーザーまたはロールとの関連付けも削除されます。注意して使用してください。
Plan: 0 to add, 0 to change, 5 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
alicloud_ram_access_key.ak: Destroying... [id=LTxxxGr]
alicloud_ram_login_profile.profile: Destroying... [id=terraform_user_test]
alicloud_ram_user_policy_attachment.attach: Destroying... [id=user:tf-example-policy:Custom:terraform_user_test]
alicloud_ram_user_policy_attachment.attach: Destruction complete after 0s
alicloud_ram_policy.policy: Destroying... [id=tf-example-policy]
alicloud_ram_login_profile.profile: Destruction complete after 0s
alicloud_ram_policy.policy: Destruction complete after 1s
alicloud_ram_access_key.ak: Destruction complete after 1s
alicloud_ram_user.user: Destroying... [id=2xxx18]
alicloud_ram_user.user: Destruction complete after 1s
完全な例
次の完全なコードは、このチュートリアルのすべてのリソースを組み合わせたものです。コピーして直接実行できます。
variable "user_name" {
default = "terraform_user_test"
}
variable "user_password" {
default = "!Test@123456"
}
variable "user_display_name" {
default = "TestAccount"
}
variable "user_mobile" {
default = "86-18688888888"
}
variable "user_email" {
default = "example@example.com"
}
resource "alicloud_ram_user" "user" {
name = var.user_name
display_name = var.user_display_name
mobile = var.user_mobile
email = var.user_email
comments = "Created by Terraform"
force = true
}
resource "alicloud_ram_login_profile" "profile" {
user_name = alicloud_ram_user.user.name
password = var.user_password
}
resource "alicloud_ram_access_key" "ak" {
user_name = alicloud_ram_user.user.name
secret_file = "accesskey.txt"
}
resource "alicloud_ram_policy" "policy" {
policy_name = "tf-example-policy"
policy_document = <<EOF
{
"Statement": [
{
"Action": "ecs:*",
"Effect": "Allow",
"Resource":"*"
}
],
"Version": "1"
}
EOF
description = "This is a policy test."
}
resource "alicloud_ram_user_policy_attachment" "attach" {
policy_name = alicloud_ram_policy.policy.policy_name
policy_type = alicloud_ram_policy.policy.type
user_name = alicloud_ram_user.user.name
}