1
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?

More than 3 years have passed since last update.

SapperをAWS Fargateにデプロイしてみる

Posted at

SapperをDockerizeして Google Cloud Runにデプロイする記事は海外のサイトにいくつかあったりしたのですが、できればAWSのサービスでそれができたらなーっと思い挑戦してみました。

今回のデプロイにあたって【AWS】 Fargate CLI + Terraform で Docker コンテナを動かす簡単なチュートリアル
の記事がとても参考になりました!
TerraformとFargateを使ったことない方がいれば是非こちらの記事を最初にご覧ください!

構成

  • Terraform
  • Fargate CLI
  • Sapper

TerraformとFargate CLIのインストール

Homebrewからインストール

brew install terraform
brew install fargatecli

Sapperの新規プロジェクト作成

npx degit "sveltejs/sapper-template#rollup" my-app
cd my-app
npm install
npm run dev & open http://localhost:3000

SapperのDockerfileを作成

FROM mhart/alpine-node:12

WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --production

FROM mhart/alpine-node:slim-12

WORKDIR /app
COPY --from=0 /app .
COPY . .

EXPOSE 3000
CMD ["node", "__sapper__/build"]

main.tfを作成

こちらは参考にさせていただいた【AWS】 Fargate CLI + Terraform で Docker コンテナを動かす簡単なチュートリアル

の構成とほぼ一緒なので、割愛します。

Fargateにデプロイ

terraform init
terraform apply
fargatecli lb create sample_app --cluster sample_app  --port HTTP:80 --security-group-id `terraform output sample_app_lb_security_group` # ロードバランサーの作成
fargatecli service create sample_app --cluster sample_app --lb sample_app --num 1 --port HTTP:3000 --cpu 256 --memory 512 --security-group-id `terraform output sample_app_ecs_security_group` --env KEY=VALUE # サービスの作成 環境変数を使ってる場合は--envで環境変数を作成時に指定できる。

サービス作成時にオプションを指定しない場合は最小構成で作成されます。

LBのステータス確認

fargatecli lb list --cluster sample_app # STATUS Activeになればサイトが表示されています。

STATUSがProvisioningの場合はしばらく待って再度確認しましょう。

後から環境変数追加

fargate service env set sample_app --env APP_KEY=APP_VALUE

削除

fargatecli service scale sample_app 0 --cluster sample_app
fargatecli service destroy sample_app --cluster sample_app
fargatecli lb destroy sample_app
terraform destroy

最後に

こんな感じで簡単にFargateにデプロイすることができました。
できれば、CircleCIとも組み合わせてテスト、自動デプロイまで出来ればなっと思っているので、出来たら記事にしたいと思います。

1
0
2

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
1
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?