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?

AWS LambdaとEventBridgeを使ってDiscordに画像を送ってみた

Posted at

背景

「毎日なにかを定期的に行ってくれる仕組みを作ってみたい」ということで、
AWS LambdaとEventBridge画像送信アプリを作ることにしました。

画像は「Lorem Picsum」を使わせてもらっています。

概要

  • AWS LambdaとEventBridgeで毎日送信する

    • 日に一度動けばよかったので、サーバーを借りるより安価な上記2つを使います
  • Discord webhookを使う

    • ウェブクライアントの代わりです

コード

import os
import urllib.request
from discordwebhook import Discord

def lambda_handler(event, context):
    target_url = os.environ.get('discordUrl')

    with urllib.request.urlopen('https://picsum.photos/300/300') as req:
        data = req.read()

        discord = Discord(url=target_url)
        response = discord.post(
          content="おはよう、今日の写真はこれ。",
          file={
            "test.png": data 
          }
        )

デプロイ

aws consoleから、LambdaとEventBridgeをよしなに設定します。

結果

想定した通り、Discordの特定チャンネルに画像が毎朝届くようになりました。

ちょっと脇道

「Lorem Picsum」へのリクエストを見てみる

さて、今回使用した「Lorem Picsum」へのリクエストを見てみると、以下の動きを見せました。

  1. https://picsum.photos/300/300」にGETリクエスト
  2. 「302 Found」のレスポンスとhttps://fastly.picsum.photos/id/xxx/~~~ への「Location」を受け取る
  3. https://fastly.picsum.photos/id/xxx/~~~ というLocationにGETリクエスト
  4. 「200 OK」のレスポンスと「画像データ」を受け取る

どうやら「picsum.photos」のドメイン直下に画像の本体は存在せず、fastlyというサービスを介して画像を配信しているようですね。
不特定多数のリクエストをさばく意思が感じられます。
「Lorem Picsum」のインフラ代はどこから来ているんでしょうね。

まとめ

検証と実装で3時間くらいでした。
AWS LambdaとEventBridgeを使えば、サクッと作れていいですね。
このツール自体に大きな価値はないですが、きれいな写真が送られてきたときはちょっと気分がよくなりました。

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?