在 App Configuration 中使用 Terraform
本教程说明如何使用 Terraform 来配置文件,例如 provider.tf
以声明 App Configuration 资源进行部署。
准备工作
确保存在以下先决条件:
-
安装 Terraform。 有关更多信息,请参阅 下载 Terraform。
-
将 Terraform 部署到特定文件夹或将其添加到
PATH
。 -
检查 Terraform 的版本:
terraform –version
-
您需要 IBM Cloud 帐户。 如果您没有帐户,请 创建 IBM Cloud 帐户。
-
登录到 IBM Cloud 帐户。
-
使用 IBM Cloud API密钥连接到您的 IBM Cloud 账户。 在 IBM Cloud 控制台中,依次选择管理 > 访问(IAM ),然后选择 API密钥。 创建一个 IBM Cloud API密钥并保存密码。 有关更多信息,请参阅管理用户 API 密钥。
有关terraform提供程序插件的更多信息,请参阅 安装 IBM Cloud 提供程序插件。
构建基础架构即代码
创建包含以下 Terraform 配置文件的 tf-template
目录:
provider.tf
provider.tf
将创建所需的提供程序。
terraform {
required_providers {
ibm = {
source = "IBM-Cloud/ibm"
version = "1.57.0"
}
}
}
instance.tf
instance.tf
供应 App Configuration 实例。
resource "ibm_resource_instance" "terraform_demo" {
plan = "lite"
location = "us-south"
name = "terraform_demo"
service = "apprapp"
}
variables.tf
variables.tf
存储 Terraform 在运行时所需的密码或其他值。
variable "collection_name" {
type = string
default = "terraform_collection"
description = "Collection name"
}
variable "collection_id" {
type = string
default = "collection123"
description = "Collection ID"
}
variable "environment_id" {
type = string
default = "dev"
description = "Environment ID"
}
variable "featureFlag_name" {
type = string
default = "terraform_featureFlag"
description = "Feature flag name"
}
variable "featureFlag_type" {
type = string
default = "BOOLEAN"
description = "Feature flag type"
}
variable "featureFlag_id" {
type = string
default = "featureFlag123"
description = "Feature flag ID"
}
variable "featureFlag_enabled" {
type = string
default = "true"
description = "Feature flag enabled value"
}
variable "featureFlag_disabled" {
type = string
default = "false"
description = "Feature flag disabled value"
}
variable "featureFlag_rollout" {
type = string
default = "50"
description = "Feature flag rollout percentage value"
}
variable "segment_name" {
type = string
default = "terraform_segment"
description = "Segment name"
}
variable "segment_id" {
type = string
default = "s6"
description = "Segment ID"
}
variable "segment_description" {
type = string
default = "testing segment create"
description = "Description for the segment"
}
variable "attribute_values" {
type = list(any)
default = ["India", "UK"]
description = "Values for segment attribute"
}
collections.tf
collections.tf
使用 App Configuration 来创建集合。
resource "ibm_app_config_collection" "app_config_collection" {
guid = ibm_resource_instance.terraform_demo.guid
name = var.collection_name
description = "Description for the collection"
collection_id = var.collection_id
}
featureFlags.tf
featureFlags.tf
使用 App Configuration 来创建功能标志。
resource "ibm_app_config_feature" "app_config_feature" {
guid = ibm_resource_instance.terraform_demo.guid
name = var.featureFlag_name
type = var.featureFlag_type
feature_id = var.featureFlag_id
enabled_value = var.featureFlag_enabled
environment_id = var.environment_id
disabled_value = var.featureFlag_disabled
rollout_percentage = var.featureFlag_rollout
}
segments.tf
segments.tf
使用 App Configuration 来创建段。
resource "ibm_app_config_segment" "app_config_create_segment" {
guid = ibm_resource_instance.terraform_demo.guid
name = var.segment_name
description = var.segment_description
tags = "t1"
segment_id = var.segment_id
rules {
attribute_name = "country"
operator = "contains"
values = var.attribute_values
}
}
运行 Terraform
在运行任何 Terraform 脚本之前,请了解以下 Terraform 命令:
Terraform init
Terraform init 命令通过确保正确配置目录来准备环境:
bash-5.1# terraform init
Initializing the backend...
Initializing provider plugins...
- Reusing previous version of ibm-cloud/ibm from the dependency lock file
- Using previously-installed ibm-cloud/ibm v1.56.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.
土地整治计划
Terraform plan 命令将声明的资源与状态文件进行比较,以打印要创建,改变或破坏的资源。 此步骤向您显示资源文件的影响。
bash-5.1# terraform plan
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:
# ibm_app_config_collection.app_config_collection will be created
+ resource "ibm_app_config_collection" "app_config_collection" {
+ collection_id = "collection123"
+ created_time = (known after apply)
+ description = "Description for the collection"
+ features_count = (known after apply)
+ guid = (known after apply)
+ href = (known after apply)
+ id = (known after apply)
+ name = "terraform_collection"
+ properties_count = (known after apply)
+ updated_time = (known after apply)
}
# ibm_app_config_feature.app_config_feature will be created
+ resource "ibm_app_config_feature" "app_config_feature" {
+ created_time = (known after apply)
+ disabled_value = "false"
+ enabled = (known after apply)
+ enabled_value = "true"
+ environment_id = "dev"
+ feature_id = "featureFlag123"
+ guid = (known after apply)
+ href = (known after apply)
+ id = (known after apply)
+ name = "terraform_featureFlag"
+ rollout_percentage = 50
+ segment_exists = (known after apply)
+ type = "BOOLEAN"
+ updated_time = (known after apply)
}
# ibm_app_config_segment.app_config_create_segment will be created
+ resource "ibm_app_config_segment" "app_config_create_segment" {
+ created_time = (known after apply)
+ description = "testing segment create"
+ guid = (known after apply)
+ href = (known after apply)
+ id = (known after apply)
+ name = "terraform_segment"
+ segment_id = "s6"
+ tags = "testing"
+ updated_time = (known after apply)
+ rules {
+ attribute_name = "country"
+ operator = "contains"
+ values = [
+ "India",
+ "UK",
]
}
}
# ibm_resource_instance.terraform_demo will be created
+ resource "ibm_resource_instance" "terraform_demo" {
+ account_id = (known after apply)
+ allow_cleanup = (known after apply)
+ created_at = (known after apply)
+ created_by = (known after apply)
+ crn = (known after apply)
+ dashboard_url = (known after apply)
+ deleted_at = (known after apply)
+ deleted_by = (known after apply)
+ extensions = (known after apply)
+ guid = (known after apply)
+ id = (known after apply)
+ last_operation = (known after apply)
+ location = "us-south"
+ locked = (known after apply)
+ name = "terraform_demo"
+ plan = "lite"
+ plan_history = (known after apply)
+ resource_aliases_url = (known after apply)
+ resource_bindings_url = (known after apply)
+ resource_controller_url = (known after apply)
+ resource_crn = (known after apply)
+ resource_group_crn = (known after apply)
+ resource_group_id = (known after apply)
+ resource_group_name = (known after apply)
+ resource_id = (known after apply)
+ resource_keys_url = (known after apply)
+ resource_name = (known after apply)
+ resource_plan_id = (known after apply)
+ resource_status = (known after apply)
+ restored_at = (known after apply)
+ restored_by = (known after apply)
+ scheduled_reclaim_at = (known after apply)
+ scheduled_reclaim_by = (known after apply)
+ service = "apprapp"
+ service_endpoints = (known after apply)
+ state = (known after apply)
+ status = (known after apply)
+ sub_type = (known after apply)
+ tags = [
+ "terraform-learning-19aug",
]
+ target_crn = (known after apply)
+ type = (known after apply)
+ update_at = (known after apply)
+ update_by = (known after apply)
}
Plan: 4 to add, 0 to change, 0 to destroy.
导出 API 密钥
在运行 apply 命令之前,导出 IBM Cloud API 密钥:
export IBMCLOUD_API_KEY={Your IBM Cloud API Key}
运行 Terraform apply
Terraform apply 命令实现在 plan 步骤期间声明的更改,并将资源部署到 IBM Cloud。
bash-5.1# 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:
# ibm_app_config_collection.app_config_collection will be created
+ resource "ibm_app_config_collection" "app_config_collection" {
+ collection_id = "collection123"
+ created_time = (known after apply)
+ description = "Description for the collection"
+ features_count = (known after apply)
+ guid = (known after apply)
+ href = (known after apply)
+ id = (known after apply)
+ name = "terraform_collection"
+ properties_count = (known after apply)
+ updated_time = (known after apply)
}
# ibm_app_config_feature.app_config_feature will be created
+ resource "ibm_app_config_feature" "app_config_feature" {
+ created_time = (known after apply)
+ disabled_value = "false"
+ enabled = (known after apply)
+ enabled_value = "true"
+ environment_id = "dev"
+ feature_id = "featureFlag123"
+ guid = (known after apply)
+ href = (known after apply)
+ id = (known after apply)
+ name = "terraform_featureFlag"
+ rollout_percentage = 50
+ segment_exists = (known after apply)
+ type = "BOOLEAN"
+ updated_time = (known after apply)
}
# ibm_app_config_segment.app_config_create_segment will be created
+ resource "ibm_app_config_segment" "app_config_create_segment" {
+ created_time = (known after apply)
+ description = "testing segment create"
+ guid = (known after apply)
+ href = (known after apply)
+ id = (known after apply)
+ name = "terraform_segment"
+ segment_id = "s6"
+ tags = "testing"
+ updated_time = (known after apply)
+ rules {
+ attribute_name = "country"
+ operator = "contains"
+ values = [
+ "India",
+ "UK",
]
}
}
# ibm_resource_instance.terraform_demo will be created
+ resource "ibm_resource_instance" "terraform_demo" {
+ account_id = (known after apply)
+ allow_cleanup = (known after apply)
+ created_at = (known after apply)
+ created_by = (known after apply)
+ crn = (known after apply)
+ dashboard_url = (known after apply)
+ deleted_at = (known after apply)
+ deleted_by = (known after apply)
+ extensions = (known after apply)
+ guid = (known after apply)
+ id = (known after apply)
+ last_operation = (known after apply)
+ location = "us-south"
+ locked = (known after apply)
+ name = "terraform_demo"
+ plan = "lite"
+ plan_history = (known after apply)
+ resource_aliases_url = (known after apply)
+ resource_bindings_url = (known after apply)
+ resource_controller_url = (known after apply)
+ resource_crn = (known after apply)
+ resource_group_crn = (known after apply)
+ resource_group_id = (known after apply)
+ resource_group_name = (known after apply)
+ resource_id = (known after apply)
+ resource_keys_url = (known after apply)
+ resource_name = (known after apply)
+ resource_plan_id = (known after apply)
+ resource_status = (known after apply)
+ restored_at = (known after apply)
+ restored_by = (known after apply)
+ scheduled_reclaim_at = (known after apply)
+ scheduled_reclaim_by = (known after apply)
+ service = "apprapp"
+ service_endpoints = (known after apply)
+ state = (known after apply)
+ status = (known after apply)
+ sub_type = (known after apply)
+ tags = [
+ "terraform-learning-19aug",
]
+ target_crn = (known after apply)
+ type = (known after apply)
+ update_at = (known after apply)
+ update_by = (known after apply)
}
Plan: 4 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
ibm_resource_instance.terraform_demo: Creating...
ibm_resource_instance.terraform_demo: Still creating... [10s elapsed]
ibm_resource_instance.terraform_demo: Still creating... [20s elapsed]
ibm_resource_instance.terraform_demo: Still creating... [30s elapsed]
ibm_resource_instance.terraform_demo: Creation complete after 31s
ibm_app_config_collection.app_config_collection: Creating...
ibm_app_config_segment.app_config_create_segment: Creating...
ibm_app_config_feature.app_config_feature: Creating...
ibm_app_config_collection.app_config_collection: Creation complete after 2s
ibm_app_config_segment.app_config_create_segment: Creation complete after 2s
ibm_app_config_feature.app_config_feature: Creation complete after 2s
Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
正在验证资源
通过在 resources中选择资源列表,验证 IBM Cloud 中的资源。
有关 IBM Cloud 产品和服务的 API 和 SDK 参考,教程和常见问题解答的更多信息,请参阅 文档。