为Event Notifications设置 Terraform
使用 Terraform 配置文件(如 provider.tf
来声明用于部署的Event Notifications资源。
准备工作
确保具备以下先决条件:
-
安装 Terraform。 有关更多信息,请参阅 下载 Terraform。
-
将 Terraform 部署到特定文件夹或将其添加到
PATH
。 -
检查 Terraform 版本:
terraform –version
-
您需要 IBM Cloud 帐户。 如果您没有账户,请创建一个IBM Cloud账户。 有关如何创建帐户的详细信息,请参阅创建 IBM Cloud帐户。
-
登录到 IBM Cloud 帐户。
在 IBM Cloud 中使用 Terraform
要与 Terraform 和IBM Cloud 配合使用,需要一个插件来扩展当前功能。 有关如何安装和配置IBM Cloud插件的更多信息,请参见 terraform-provider-ibm。
以下说明使用 Provider1.38.2。 有关更多信息,请参阅 IBM Cloud提供商。
-
从主目录创建隐藏目录:
mkdir $HOME/.terraform.d/plugins
-
下载插件并将其移至您创建的目录。 有关更多信息,请参阅 terraform-provider-ibm。
mv $HOME/Downloads/terraform-provider-ibm_v1.38.2 $HOME/.terraform.d/plugins/
-
使用 IBM Cloud API 密钥连接到 IBM Cloud 帐户。
-
Log in to IBM Cloud and go to the 托管 tile and select 访问(IAM), and then select IBM Cloud API Keys.
-
创建 IBM Cloud API 密钥并保存密码。
部署资源
创建Event Notifications资源。 有关更多信息,请参阅 Event Notifications 入门。
构建基础架构即代码
tf-template
目录包含以下 Terraform 配置文件:
provider.tf
provider.tf
将创建所需的提供程序。
terraform {
required_providers {
ibm = {
source = "IBM-Cloud/ibm"
version = "1.38.2"
}
}
}
variables.tf
variables.tf
存储 Terraform 在运行时所需的密码或其他值。
variable "instance_id" {
type = string
default = "235b109c-0a2c-438b-8758-1a4b9db044db"
description = "Event Notifications service instanceId."
}
variable "destination_name" {
type = string
default = "Smart Home Android app"
description = "Destination name."
}
variable "destination_description" {
type = string
default = "Smart House device status"
description = "Description for the destination."
}
variable "webhook_type" {
default = "webhook"
type = string
description = "Type of the destination"
}
variable "webhook_verb" {
default = "POST"
type = string
description = "Type of verb fro the destination"
}
variable "webhook_URL" {
type = string
description = "URL of the webhook"
default = "https://hello.demo.com/hello"
}
其余文件将声明要部署的资源。
instance.tf
instance.tf
供应 Event Notifications 实例。
resource "ibm_resource_instance" "terraform_demo" {
plan = "lite"
location = "us-south"
name = "terraform_demo"
service = "event-notifications"
}
destinations.tf
destinations.tf
使用 Event Notifications 来创建目标。
resource "ibm_en_destination" "destination1" {
type = var.webhook_type
name = var.destination_name
description = var.destination_description
instance_guid = ibm_resource_instance.terraform_demo.guid
config {
params {
url = var.webhook_URL
verb = var.webhook_verb
custom_headers = {
"authorization" = "secure"
}
sensitive_headers = ["authorization"]
}
}
}
topics.tf
topics.tf
使用 Event Notifications 来创建主题。
resource "ibm_en_topic" "topic1" {
instance_guid = ibm_resource_instance.terraform_demo.guid
description = "Check the status of front door"
name = "Front Door Status"
}
subscriptions.tf
subscriptions.tf
使用 Event Notifications 来创建预订。
resource "ibm_en_subscription" "subscription1" {
instance_guid = ibm_resource_instance.terraform_demo.guid
name = "Android app Subscription"
description = "Subscribe to Android app"
topic_id = ibm_en_topic.topic1.topic_id
destination_id = ibm_en_destination.destination1.destination_id
attributes {
add_notification_payload = true
signing_enabled = true
}
}
运行 Terraform
在运行任何 Terraform 脚本之前,请学习以下 Terraform 命令:
Terraform启动
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.38.2
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计划
Terraform计划命令会将声明的资源与状态文件进行比较,以打印要创建、更改或删除的资源。 此步骤向您显示 main.tf
文件的影响。
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_en_destination.destination1 will be created
+ resource "ibm_en_destination" "destination1" {
+ description = "Smart House device status"
+ destination_id = (known after apply)
+ id = (known after apply)
+ instance_guid = (known after apply)
+ name = "Smart Home Android app"
+ subscription_count = (known after apply)
+ subscription_names = (known after apply)
+ type = "webhook"
+ updated_at = (known after apply)
+ config {
+ params {
+ custom_headers = {
+ "authorization" = "secure"
}
+ sensitive_headers = [
+ "authorization",
]
+ url = "https://hello.demo.com/hello"
+ verb = "POST"
}
}
}
# ibm_en_subscription.subscription1 will be created
+ resource "ibm_en_subscription" "subscription1" {
+ description = "Subscribe to Android app"
+ destination_id = (known after apply)
+ destination_name = (known after apply)
+ destination_type = (known after apply)
+ from = (known after apply)
+ id = (known after apply)
+ instance_guid = (known after apply)
+ name = "Android app Subscription"
+ subscription_id = (known after apply)
+ topic_id = (known after apply)
+ topic_name = (known after apply)
+ updated_at = (known after apply)
+ attributes {
+ add_notification_payload = true
+ signing_enabled = true
}
}
# ibm_en_topic.topic1 will be created
+ resource "ibm_en_topic" "topic1" {
+ description = "Check the status of front door"
+ id = (known after apply)
+ instance_guid = (known after apply)
+ name = "Front Door Status"
+ source_count = (known after apply)
+ subscription_count = (known after apply)
+ subscriptions = (known after apply)
+ topic_id = (known after apply)
+ updated_at = (known after apply)
}
# 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 = "event-notifications"
+ service_endpoints = (known after apply)
+ state = (known after apply)
+ status = (known after apply)
+ sub_type = (known after apply)
+ tags = (known after apply)
+ target_crn = (known after apply)
+ type = (known after apply)
+ update_at = (known after apply)
+ update_by = (known after apply)
}
导出 API 密钥
运行应用命令前,请导出您的IBM CloudAPI 密钥:
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_en_destination.destination1 will be created
+ resource "ibm_en_destination" "destination1" {
+ description = "Smart House device status"
+ destination_id = (known after apply)
+ id = (known after apply)
+ instance_guid = (known after apply)
+ name = "Smart Home Android app"
+ subscription_count = (known after apply)
+ subscription_names = (known after apply)
+ type = "webhook"
+ updated_at = (known after apply)
+ config {
+ params {
+ custom_headers = {
+ "authorization" = "secure"
}
+ sensitive_headers = [
+ "authorization",
]
+ url = "https://hello.demo.com/hello"
+ verb = "POST"
}
}
}
# ibm_en_subscription.subscription1 will be created
+ resource "ibm_en_subscription" "subscription1" {
+ description = "Subscribe to Android app"
+ destination_id = (known after apply)
+ destination_name = (known after apply)
+ destination_type = (known after apply)
+ from = (known after apply)
+ id = (known after apply)
+ instance_guid = (known after apply)
+ name = "Android app Subscription"
+ subscription_id = (known after apply)
+ topic_id = (known after apply)
+ topic_name = (known after apply)
+ updated_at = (known after apply)
+ attributes {
+ add_notification_payload = true
+ signing_enabled = true
}
}
# ibm_en_topic.topic1 will be created
+ resource "ibm_en_topic" "topic1" {
+ description = "Check the status of front door"
+ id = (known after apply)
+ instance_guid = (known after apply)
+ name = "Front Door Status"
+ source_count = (known after apply)
+ subscription_count = (known after apply)
+ subscriptions = (known after apply)
+ topic_id = (known after apply)
+ updated_at = (known after apply)
}
# 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 = "standard"
+ 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 = "event-notifications"
+ service_endpoints = (known after apply)
+ state = (known after apply)
+ status = (known after apply)
+ sub_type = (known after apply)
+ tags = (known after apply)
+ 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.
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: Still creating... [40s elapsed]
ibm_resource_instance.terraform_demo: Still creating... [50s elapsed]
ibm_resource_instance.terraform_demo: Creation complete after 52s [id=crn:v1:bluemix:public:event-notifications:us-south:a/4a74f2c31f554afc88156b73a1d577c6:24547d88-e455-548f-cb6e-ge921g6d3025::]
ibm_en_topic.topic1: Creating...
ibm_en_destination.destination1: Creating...
ibm_en_destination.destination1: Still creating... [10s elapsed]
ibm_en_topic.topic1: Still creating... [10s elapsed]
ibm_en_topic.topic1: Creation complete after 12s [id=24547d88-e455-548f-cb6e-ge921g6d3025/7960f1fe-7991-4993-ad0c-38f12c78b3c8]
ibm_en_destination.destination1: Creation complete after 13s [id=24547d88-e455-548f-cb6e-ge921g6d3025/878ce8e6-75f9-41e6-913f-6b42f6945588]
ibm_en_subscription.subscription1: Creating...
ibm_en_subscription.subscription1: Creation complete after 4s [id=24547d88-e455-548f-cb6e-ge921g6d3025/b2d88ff9-da10-412a-9f7a-19f509ea83fb]
Apply complete! Resources: 4 added, 0 changed, 0 destroyed..
正在验证资源
通过在 resources中选择资源列表,验证 IBM Cloud 中的资源。
有关 IBM Cloud 产品和服务的 API 和 SDK 参考,教程和常见问题解答的更多信息,请参阅 文档。