Deploy Message Publish
Application Scenario
Simple Message Notification (SMN) is a reliable and scalable message notification service provided by Huawei Cloud, supporting multiple message notification methods including email, SMS, HTTP/HTTPS, etc. Through message publishing, messages can be published to SMN topics, and terminals subscribed to the topic will receive message notifications. Message publishing supports multiple methods including direct message content, message structure, and message templates, meeting message publishing requirements for different scenarios. This best practice introduces how to use Terraform to automatically deploy message publishing, including creating SMN topics, subscriptions, message templates (optional), and publishing messages.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Resources
Resource/Data Source Dependencies
huaweicloud_smn_topic
├── huaweicloud_smn_subscription
└── huaweicloud_smn_message_publish
huaweicloud_smn_message_template
└── huaweicloud_smn_message_publishNote: Message publishing depends on SMN topics, and message templates can be optionally used. Subscriptions are used to receive message notifications. After messages are published, terminals subscribed to the topic will receive messages.
Operation Steps
1. Script Preparation
Prepare the TF file (such as main.tf) for writing the current best practice script in the specified workspace, ensuring that it (or other TF files in the same directory) contains the provider version declaration and Huawei Cloud authentication information required for deploying resources. For configuration details, refer to the introduction in Preparation Before Deploying Huawei Cloud Resources.
2. Create SMN Topic
Add the following script to the TF file (such as main.tf) to create an SMN topic:
Parameter Description:
name: Topic name, assigned by referencing the input variable
topic_namedisplay_name: Topic display name, assigned by referencing the input variable
topic_display_name, optional parameterenterprise_project_id: Enterprise project ID, assigned by referencing the input variable
enterprise_project_id, optional parameter
3. Create SMN Subscription
Add the following script to the TF file (such as main.tf) to create an SMN subscription:
Parameter Description:
topic_urn: Topic URN, assigned by referencing the SMN topic resource ID
protocol: Subscription protocol, assigned by referencing the input variable
subscription_protocol. Supportsemail,sms,http,https,functionstage, etc.endpoint: Subscription endpoint, assigned by referencing the input variable
subscription_endpoint. Fill in the corresponding endpoint address according to the protocol typeremark: Subscription remark, assigned by referencing the input variable
subscription_description, optional parameter
Note: Subscription protocol and endpoint must match. For example, SMS protocol requires a phone number, and email protocol requires an email address.
4. Create Message Template (Optional)
Add the following script to the TF file (such as main.tf) to create a message template (optional):
Parameter Description:
count: Resource count, creates resource when
template_nameis not emptyname: Template name, assigned by referencing the input variable
template_nameprotocol: Template protocol, assigned by referencing the input variable
template_protocolcontent: Template content, assigned by referencing the input variable
template_content
Note: Message templates are optional. If using templates to publish messages, message templates need to be created first. Template protocol must match subscription protocol.
5. Publish Message
Add the following script to the TF file (such as main.tf) to publish a message:
Parameter Description:
topic_urn: Topic URN, assigned by referencing the SMN topic resource
topic_urnsubject: Message subject, assigned by referencing the input variable
pulblish_subjectmessage: Message content, assigned by referencing the input variable
pulblish_message, mutually exclusive withmessage_structure, optional parametermessage_structure: Message structure, assigned by referencing the input variable
pulblish_message_structure, JSON format, allows sending different content to different protocol subscribers, mutually exclusive withmessage, optional parametermessage_template_name: Message template name, references message template resource name when
template_nameis not empty, optional parametertime_to_live: Maximum retention time of the message within the SMN system (seconds), assigned by referencing the input variable
pulblish_time_to_live, default is 3600 seconds, maximum 86400 seconds, optional parametertags: Message tags, assigned by referencing the input variable
pulblish_tags, optional parametermessage_attributes: Message attribute list, creates multiple message attributes through dynamic block
dynamic "message_attributes"based on input variablepulblish_message_attributes, optional parameter
Note: Message publishing supports three methods: direct message content (message), message structure (message_structure), and message template (message_template_name). Message and message structure are mutually exclusive. If using message templates, message content is not required. Message structure is in JSON format and can send different content to subscribers of different protocols.
6. Preset Input Parameters Required for Resource Deployment (Optional)
In this practice, some resources and data sources use input variables to assign configuration content. These input parameters need to be manually entered during subsequent deployment. Terraform also provides a method to preset these configurations through tfvars files, which can avoid repeated input each time.
Create a terraform.tfvars file in the working directory with the following example content:
Usage:
Save the above content as a
terraform.tfvarsfile in the working directory (this filename allows Terraform to automatically import the variable values in thistfvarsfile when executing terraform commands. For other names, you need to add.autobefore tfvars, such asvariables.auto.tfvars)Modify parameter values according to actual needs
When executing
terraform planorterraform apply, Terraform will automatically read the variable values in this file
In addition to using the terraform.tfvars file, you can also set variable values through the following methods:
Command-line parameters:
terraform apply -var="topic_name=tf_test_topic" -var="subscription_protocol=sms"Environment variables:
export TF_VAR_topic_name=tf_test_topicCustom-named variable files:
terraform apply -var-file="custom.tfvars"
Note: If the same variable is set through multiple methods, Terraform will use variable values according to the following priority: command-line parameters > variable files > environment variables > default values.
7. Initialize and Apply Terraform Configuration
After completing the above script configuration, execute the following steps to create resources:
Run
terraform initto initialize the environmentRun
terraform planto view the resource creation planAfter confirming that the resource plan is correct, run
terraform applyto start creating SMN topic, subscription, and publishing messagesRun
terraform showto view the created resources
Reference Information
Last updated