AWS Amplify で コンテナベースのデプロイを行い REST API を構築

AWS Amplify で コンテナベースのデプロイを行い REST API を構築

2021-07-264 min read

目次

  1. 概要
  2. 検証した環境
  3. やってみる
  4. パイプラインを確認
  5. 終了処理
  6. 参考

概要

AWS Amplify で コンテナベースのデプロイを行い REST API を構築した際のメモです。

検証した環境

  • amplify 5.1.2

やってみる

初期準備

初期準備を行います。

mkdir amplify-fargate
cd amplify-fargate
amplify init

amplify init すると質問が始まりますが、全部デフォルトのオプションで進めます。

Project information
| Name: amplifyfargate
| Environment: dev
| Default editor: Visual Studio Code
| App type: javascript
| Javascript framework: none
| Source Directory Path: src
| Distribution Directory Path: dist
| Build Command: npm run-script build
| Start Command: npm run-script start

AWS Profile setting
| Selected profile: default

Advanced: Container-based deployments
| Leverage container-based deployments: No

次のコマンドを実行してコンテナの設定を適用します。

amplify configure project

Do you want to enable container-based deployments? (y/N) と聞かれるのでyと答えます。

次のコマンドでAPIを追加します。

amplify add api

質問は次のように答えました。

  • Please select from one of the below mentioned services:
    • REST
  • Which service would you like to use
    • API Gateway + AWS Fargate (Container-based)
  • Provide a friendly name for your resource to be used as a label for this category in the project:
    • container
  • What image would you like to use
    • Docker Compose - ExpressJS + Flask template
  • When do you want to build & deploy the Fargate task
    • On every "amplify push" (Fully managed container source)
  • Do you want to restrict API access
    • No
  • Select which container is the entrypoint
    • python (expressでも良いがここではPythonで進めます)

設定が終わると設定ファイルが以下のように吐き出されます。

amplify/backend/
├── amplify-meta.json
├── api
│   └── container
│       ├── container-cloudformation-template.json
│       ├── dist
│       │   └── latest-build.zip
│       ├── parameters.json
│       └── src
│           ├── buildspec.yml
│           ├── docker-compose.yml
│           ├── express (省略)
│           └── python
│               ├── Dockerfile
│               ├── requirements.txt
│               └── src
│                   └── server.py
├── awscloudformation
│   ├── build
│   │   ├── api
│   │   │   └── container
│   │   │       └── container-cloudformation-template.json
│   │   └── awscloudformation
│   │       └── nested-cloudformation-stack.yml
│   └── nested-cloudformation-stack.yml
├── backend-config.json
├── function
│   └── container
│       └── amplify.state
└── tags.json

次に amplify/backend/api/container/src/python/src/server.py を次のように編集します。

from flask import Flask, jsonify
server = Flask(__name__)

@server.route('/hello')
def hello():
    return jsonify({'msg': 'Hello World'})

if __name__ == "__main__":
   server.run(host='0.0.0.0')

次のコマンドでデプロイを行います。

amplify push

3〜5分ほど待つデプロイが終わります。

https://xxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/hello

{
  "msg": "Hello World"
}

でレスポンスが返れば成功です。

パイプラインを確認

パイプラインを確認してみると

  1. s3へのアップロード
  2. build
  3. predeploy
  4. deploy

といった具合で実行されていることが確認できました。

終了処理

遊んだらリソースを削除しておきます。

次のコマンドで削除します。

amplify delete

これだけだと上手くいかず、S3やECRは手動で削除しました。

参考

Amplify CLIでFargateを利用したサーバーレスコンテナのデプロイが可能になりました #reinvent #amplify

Amplify CLI が、AWS Fargate を使用したサーバーレスコンテナのデプロイを実現

AWS Amplify の Fargate 対応で “うまくいかない” docker-compose.yml の書き方

AWS Amplify の API 機能が Fargate 対応したので PHP のフレームワーク (Laravel) を動かしてみた

Tags
javascript(109)
linux(54)
node.js(53)
amazon%20aws(47)
typescript(44)
%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0(36)
%E7%94%BB%E5%83%8F%E5%87%A6%E7%90%86(30)
html5(29)
php(24)
centos(24)
python(22)
%E7%AB%B6%E6%8A%80%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0(21)
mac(21)
mysql(20)
canvas(19)
opencv(17)
%E9%9B%91%E8%AB%87(16)
docker(16)
wordpress(15)
atcoder(14)
apache(12)
%E6%A9%9F%E6%A2%B0%E5%AD%A6%E7%BF%92(12)
%E3%83%87%E3%83%BC%E3%82%BF%E3%83%99%E3%83%BC%E3%82%B9(12)
amazon%20s3(12)
red%20hat(12)
prisma(12)
ubuntu(11)
github(10)
git(10)
vue.js(10)
%E7%94%BB%E5%83%8F%E5%87%A6%E7%90%86100%E6%9C%AC%E3%83%8E%E3%83%83%E3%82%AF(10)
mariadb(10)
react(9)
aws%20cdk(9)
css3(8)
%E5%8F%AF%E8%A6%96%E5%8C%96(8)
%E5%B0%8F%E3%83%8D%E3%82%BF(8)
nestjs(8)
amazon%20lightsail(7)
next.js(7)
%E3%83%96%E3%83%AD%E3%82%B0(6)
cms(6)
oracle(6)
perl(6)
gitlab(6)
iam(5)
amazon%20ec2(5)
%E8%B3%87%E6%A0%BC%E8%A9%A6%E9%A8%93(5)
aws%20amplify(5)
curl(4)
Author
githubzennqiita
ただの備忘録です。

※外部送信に関する公表事項