Configuración de Terraform para Event Notifications
Utilice Terraform para configurar archivos como provider.tf
para declarar Event Notifications recursos para el despliegue.
Antes de empezar
Asegúrese de que se cumplen los siguientes requisitos previos:
-
Instale Terraform. Para obtener más información, consulte Descargar Terraform.
-
Despliegue Terraform en una carpeta específica o añádalo a
PATH
. -
Compruebe la versión de Terraform:
terraform –version
-
Necesita una cuenta de IBM Cloud. Si no tienes una cuenta, crea una cuenta IBM Cloud. Para obtener más información sobre cómo crear una cuenta, consulteCrear una cuenta de IBM Cloud.
-
Inicie sesión en la cuenta de IBM Cloud.
Trabajar con Terraform en IBM Cloud
Para trabajar con Terraform y el IBM Cloud, se requiere un plug-in para ampliar las funciones actuales. Para obtener más información sobre cómo instalar y configurar el complemento IBM Cloud, consulte terraform-provider-ibm.
En las siguientes instrucciones se utiliza el Proveedor 1.38.2. Para obtener más información, consulte IBM Cloud Proveedor.
-
Cree un directorio oculto desde el directorio inicial:
mkdir $HOME/.terraform.d/plugins
-
Descargue y coloque el plug-in en el directorio que ha creado. Para obtener más información, consulte terraform-provider-ibm.
mv $HOME/Downloads/terraform-provider-ibm_v1.38.2 $HOME/.terraform.d/plugins/
-
Conéctese a su cuenta ' IBM Cloud ' con la clave API ' IBM Cloud.
-
Inicie sesión en IBM Cloud y vaya al azulejo Gestionado y seleccione Acceso (IAM), y luego seleccione IBM Cloud API Keys.
-
Cree una IBM Cloud API Key y guarda la contraseña.
Despliegue de recursos
Cree un recurso de Event Notifications. Para obtener más información, consulte Iniciación a Event Notifications.
Creación de Infrastructure-as-Code
El directorio tf-template
contiene los siguientes archivos de configuración de Terraform:
provider.tf
provider.tf
crea los proveedores necesarios.
terraform {
required_providers {
ibm = {
source = "IBM-Cloud/ibm"
version = "1.38.2"
}
}
}
variables.tf
variables.tf
almacena contraseñas u otros valores que son necesarios para Terraform en tiempo de ejecución.
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"
}
Los archivos restantes declaran los recursos que se van a desplegar.
instance.tf
instance.tf
proporciona una instancia de Event Notifications.
resource "ibm_resource_instance" "terraform_demo" {
plan = "lite"
location = "us-south"
name = "terraform_demo"
service = "event-notifications"
}
destinations.tf
destinations.tf
utiliza Event Notifications para crear destinos.
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
utiliza Event Notifications para crear temas.
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
utiliza Event Notifications para crear suscripciones.
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
}
}
Ejecución de Terraform
Antes de ejecutar cualquier script Terraform, aprenda los siguientes comandos Terraform:
Terraform init
El comando Terraform init prepara el entorno para asegurarse de que el directorio está configurado correctamente:
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 plan
El comando plan de Terraform compara los recursos declarados con el archivo de estado para imprimir los recursos que deben crearse, alterarse o eliminarse. Este paso le muestra el impacto del archivo 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)
}
Exportación de una clave de API
Exporte su IBM Cloud API Key antes de ejecutar el comando apply:
export IBMCLOUD_API_KEY={Your IBM Cloud API Key}
Ejecución de Terraform apply
El comando apply de Terraform implementa los cambios declarados durante el paso plan y despliega el recurso en el 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..
Validación de recursos
Valide los recursos dentro del " IBM Cloud " seleccionando la lista de recursos en " recursos.
Para obtener más información sobre referencias de API y SDK, tutoriales y preguntas frecuentes, para los productos y servicios de IBM Cloud, consulte Documentación.