LoginSignup
0
1

EIPを付与していないEC2インスタンスの起動時に割り振られたIPアドレスをslack通知する

Last updated at Posted at 2023-11-24

起動時にワンショットだけ起動するサービスを仕込んでおきます。以下ubuntuにて。

/etc/systemd/system/notify-slack.service

[Unit]
Description=Send IP to Slack at Startup
After=network.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/notify_ip_address_to_slack.sh

[Install]
WantedBy=multi-user.target

notify_ip_address_to_slack.sh:

#!/bin/bash

# パブリックIPを取得
IP_ADDRESS=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)

# Slackに送信するメッセージを作成
MESSAGE="ステージング環境が起動しました。IPアドレス: $IP_ADDRESS"

# SlackのWebhook URL
WEBHOOK_URL="https://hooks.slack.com/services/XXX/XXX/XXX"

# SlackにPOSTリクエストを送信
curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"$MESSAGE\"}" $WEBHOOK_URL

有効にする

$ sudo systemctl daemon-reload
$ sudo systemctl enable notify-slack.service
0
1
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
1