0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Terraformマスターへの道#2 OCI Resource Managerでメール通知してみた

0
Posted at

はじめに

#1ではTerraformの構成ファイルを作成し、Resource Managerを使ってオブジェクトストレージを作成・削除をしました。
今回はOCIに閉じない連携ツールをTerraformで作成しよう!ということで、OCI Notificationsのメール通知を作ってみます。

基本的な内容は前回と同じく、OCI Resource Managerを使ってTerraformを実行し、NotificationsのTopicとEmail Subscriptionを作成します。

今回やること

  1. Notifications Topicを作成するTerraform作成
  2. Email Subscriptionを作成
  3. Resource ManagerでPlan / Applyの実行
  4. 確認メール承認
  5. Topicにテストメッセージ送信
  6. メール受信確認
  7. Destroyで削除

Terraformファイルの作成

main.tfを作成します。

terraform {
  required_providers {
    oci = {
      source  = "oracle/oci"
      version = ">= 6.0.0"
    }
  }
}

variable "region" {
  description = "OCI region"
  type        = string
}

variable "compartment_ocid" {
  description = "Compartment OCID"
  type        = string
  default     = "<compartment OCID ★自分のコンパートメントOCIDを入力>"
}

variable "topic_name" {
  description = "Notifications topic name"
  type        = string
}

variable "subscription_email" {
  description = "Email address for Notifications subscription"
  type        = string
}

provider "oci" {
  region = var.region
}

resource "oci_ons_notification_topic" "sample" {
  compartment_id = var.compartment_ocid
  name           = var.topic_name
  description    = "Terraform sample topic for email notification"
}

resource "oci_ons_subscription" "email" {
  compartment_id = var.compartment_ocid
  topic_id       = oci_ons_notification_topic.sample.id
  protocol       = "EMAIL"
  endpoint       = var.subscription_email
}

output "topic_id" {
  value = oci_ons_notification_topic.sample.id
}

output "topic_name" {
  value = oci_ons_notification_topic.sample.name
}

output "subscription_id" {
  value = oci_ons_subscription.email.id
}

output "subscription_state" {
  value = oci_ons_subscription.email.state
}

Resource Managerにアップロードするため、作成したTerraformファイルをzip化します。
zip terraform-notifications-email.zip

Resource ManagerでStackを作成

OCIコンソールで、Terraform構成ファイルとして先ほど作成したterraform-notifications-email.zipをアップロードします。
Stack名を入力します。
terraform-notifications-email-sample
image.png

次にTerraform変数を入力します。

項目 入力値
CompartmentID <デフォルト値>
Region <デフォルト値>
Subscription_email 通知を受け取りたいメールアドレス
TopicName NotificationTest

image.png

内容を確認し、作成します。
image.png

作成されました。
image.png

Planを実行

Stack作成後、まずPlanを実行します。
image.png

image.png

今回の例では、以下の2つが作成予定になっていればOKです。
image.png
よく見るとログ内にはこのようなメッセージがあるはずです。

# oci_ons_notification_topic.sample will be created
# oci_ons_subscription.email will be created

Applyを実行

Planの内容を確認したら、Applyを実行します。
image.png

image.png

Applyが成功すると、Notifications TopicとEmail Subscriptionが作成されます。
image.png
image.png

このタイミングで指定のメールアドレスに通知が届いていました。
image.png

NotificationsのTopicを確認する

OCIコンソールで以下に移動します。
対象のコンパートメントを選択し、Terraformで作成したTopicが表示されていることを確認します。
image.png

サブスクリプションから状況を確認できます。
Apply時にメールは受信できているのに、サブスクリプションが「保留中」になっています。。。
image.png

受信メールの承認

もう一度届いたメールを見てみます。
「サブスクリプションを確認してください」と書いてあります。
image.png

メール内のボタンから、サブスクリプションを確認していきます。
ボタンを押すとこの画面に飛び、サブスクリプションの確認に成功しました。
image.png

確認が完了すると、コンソール上でもサブスクリプションが「アクティブ」になります。
image.png
ここでようやく、Topicに送信されたメッセージがメールに配信される状態になります。

テストメッセージの送信

Topic詳細画面からテストメッセージを送信します。
image.png
このトピックには、terraform-notifications-email-sampleで作成したメールアドレスしかサブスクリプションに登録されていないので、さっき確認メールを受信したメールアドレスにテストメッセージが届くはずです。

例として、以下のようなメッセージを送信します。
メッセージの公開という少しわかりづらいメニューから、公開します。

Terraformで作成したOCI Notifications Topicからのテスト通知です。

image.png

ちゃんとメールは届いていました!
image.png

Destroyで削除する

確認が終わったら、Resource ManagerのStack詳細画面に戻り、Destroyを実行します。
image.png

Destroyが成功すると、Terraformで作成したTopicとSubscriptionが削除されます。
image.png

OCIコンソールの通知部分からも該当のトピックが削除されていることが確認できます。
image.png

まとめ

メール承認が必要な部分はありましたが、全体的にTerraformの基本を抑えていればつまずくことなく実行できそうです。
メール通知先は通知部分から追加・編集ができるので、Terraformをいじらなくてもいいのは便利でした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?