<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Discord on よ〜んの雑記</title><link>https://mu7889yoon.github.io/tags/discord/</link><description>Recent content in Discord on よ〜んの雑記</description><generator>Hugo</generator><language>ja-jp</language><lastBuildDate>Sat, 26 Sep 2026 13:17:52 +0900</lastBuildDate><atom:link href="https://mu7889yoon.github.io/tags/discord/index.xml" rel="self" type="application/rss+xml"/><item><title>Discord Gatewayに常時接続するBotを、Fargateで安く動かす</title><link>https://mu7889yoon.github.io/posts/discord-gateway-bot-fargate-spot/</link><pubDate>Sat, 26 Sep 2026 13:17:52 +0900</pubDate><guid>https://mu7889yoon.github.io/posts/discord-gateway-bot-fargate-spot/</guid><description>&lt;p&gt;よ〜んです。&lt;/p&gt;
&lt;p&gt;以前、&lt;a href="https://mu7889yoon.github.io/posts/alb-less-ecs/"&gt;ALBなしでECSを使う構成&lt;/a&gt;を紹介しました。今回はその考え方を、Discord Gatewayへ常時接続するBotに当てはめます。&lt;/p&gt;
&lt;p&gt;BotをAWSで動かすとき、一番安い選択肢はおそらくLightsail(EC2)です。次に、少し使い方に工夫がいるLambda MicroVMsという選択肢もあります。&lt;/p&gt;
&lt;p&gt;そしてECS。普通に構成すると周辺リソースが増えて、Botを1つ動かすだけのスコープではオーバースペックになりそうに見えます。&lt;/p&gt;
&lt;p&gt;今回は、Discord Gatewayへ常時接続するBotを題材に、ECSをまたALBなしで小さく動かす構成をご紹介します。&lt;/p&gt;
&lt;h2 id="discordgateway-bot"&gt;Discord　Gateway Bot&lt;a class="headline-hash" href="#discordgateway-bot" aria-label="「Discord　Gateway Bot」見出しへのリンク"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Discord Botには、HTTPで受け取ったInteractionに応答するものや、Gatewayへ接続してイベントを受け取るものがあります。&lt;/p&gt;
&lt;p&gt;今回扱うのは後者です。Botのプロセスが起動している間、Discord Gatewayへ接続し続けます。&lt;/p&gt;
&lt;p&gt;つまりLambdaとは相性がよくありません。常時接続が必要なら、プロセスが動き続ける実行環境を用意する必要があります。&lt;/p&gt;
&lt;p&gt;&lt;a href="https://docs.discord.com/developers/events/gateway"&gt;Gateway - Documentation - Discord&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="lightsail"&gt;Lightsail&lt;a class="headline-hash" href="#lightsail" aria-label="「Lightsail」見出しへのリンク"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;安い。IPv6専用なら月額3.50 USD、IPv4付きなら5 USDからです（&lt;a href="https://aws.amazon.com/lightsail/pricing/"&gt;料金表&lt;/a&gt;）。&lt;/p&gt;
&lt;p&gt;ただし、3.50 USDのIPv6専用プランではDiscord Botを動かせません。Gateway接続先の&lt;code&gt;gateway.discord.gg&lt;/code&gt;にAAAAレコードがないため、IPv4付きプランが必要です。&lt;/p&gt;
&lt;p&gt;Dockerで常時稼働できますが、更新はSSHや自前のCI/CDで行います。&lt;/p&gt;
&lt;p&gt;EC2については費用が上がりやすいため、今回は候補から外します。&lt;/p&gt;
&lt;h2 id="lambda-microvms"&gt;Lambda MicroVMs&lt;a class="headline-hash" href="#lambda-microvms" aria-label="「Lambda MicroVMs」見出しへのリンク"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;使うときだけ起動でき、実行時間を抑えられます。&lt;/p&gt;
&lt;p&gt;ただし、最大8時間で、もちろん停止中はGatewayのイベントを受け取れません。&lt;/p&gt;
&lt;p&gt;常時反応するBotには不向きで、起動用のスラッシュコマンドなどといった経路も必要です。&lt;/p&gt;
&lt;h2 id="ecs-fargate"&gt;ECS　/ Fargate&lt;a class="headline-hash" href="#ecs-fargate" aria-label="「ECS　/ Fargate」見出しへのリンク"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;コンテナのデプロイを自動化しつつ、Botを常時稼働できます。&lt;/p&gt;
&lt;p&gt;受信HTTPがないのでALBは不要。Public SubnetからPublic IPでGatewayへ接続します（&lt;a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/networking-outbound.html"&gt;通信経路&lt;/a&gt;）。&lt;/p&gt;
&lt;p&gt;NAT Gatewayも置かず、Security Groupはインバウンドなし・外向きTCP 443のみです。&lt;/p&gt;
&lt;h3 id="gravitonとspot"&gt;GravitonとSpot&lt;a class="headline-hash" href="#graviton%e3%81%a8spot" aria-label="「GravitonとSpot」見出しへのリンク"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;タスクはARM64で動かします。Fargate Spotは、通常のFargate料金より割引された料金でタスクを実行できる仕組みです。ECSではARM64とFARGATE_SPOTを組み合わせられます。&lt;/p&gt;
&lt;p&gt;今回のタスク定義は0.25 vCPU、メモリ512 MiB、desired countは1です。Botの処理量に対して大きすぎないサイズから始めています。&lt;/p&gt;
&lt;p&gt;ただし、Fargate Spotは中断される可能性があります。Botが切断から再接続できること、短い停止を許容できることが前提です。可用性が重要なBotの場合は普通に実行すればいいと思います。&lt;/p&gt;</description></item></channel></rss>