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

Amplifyでもコンテナ!
2020.12.11

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

大阪オフィスの小倉です。

Amplify CLIでFargateを使用したサーバレスコンテナアプリのデプロイが出来るようなりました。

https://aws.amazon.com/jp/about-aws/whats-new/2020/12/amplify-cli-enables-serverless-container-deployments-using-aws-fargate/

やってみた

以下のサイトの手順に従って試してみました。

https://aws.amazon.com/jp/blogs/mobile/zero-effort-container-deployment-for-graphql-and-rest-apis-and-web-hosting-with-amplify-cli/

Amplify CLIのアップデート

最初にCLIをアップデートします。

npm install -g @aws-amplify/cli@beta

Amplifyプロジェクトの作成

プロジェクト用のディレクトリを作成し、Amplifyプロジェクトを初期化します

mkdir amplify-containerized
cd amplify-containerized
amplify init

amplify initで質問される設定内容はデフォルトでよいとの事です。

コンテナベースのデプロイを有効化する

以下のコマンドを実行して設定を開始します。

amplify configure project

init時と同じ様な設定を再度質問されるので、そのまま進めていくと、最後に

? Do you want to enable container-based deployments? (y/N)

が聞かれるので、yを入力します。

APIを追加する

amplify add api

を実行して、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
    • (デフォルト値のまま)
  • What image would you like to use (Use arrow keys)
    • ExpressJS - REST template

ここまで実行すると、プロジェクトディレクトリ内に新しいファイル、ディレクトリが追加されます。

アプリケーションのコードを編集する

参考記事に従い、src/index.jsを編集します。(以下は参考記事からの引用です)

const express = require("express");
const bodyParser = require('body-parser');
const port = process.env.PORT || 3001;

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// Enable CORS for all methods
app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*")
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
    next()
});

app.get("/", async (req, res, next) => {

    try {
        res.contentType("application/json").send({ 
            "randomNumber": Math.floor(Math.random() * 101) 
        })
    } catch (err) {
        next(err);
    }
});

app.listen(port, () => {
    console.log('Example app listening at http://localhost:' + port);
});

デプロイしてみる

amplify pushでデプロイできます。

デプロイすると多くのAWSリソースが作成されるので、しばらく時間がかかります。

デプロイ完了後、API GatewayのエンドポイントURLが表示されます

All resources are updated in the cloud

REST API endpoint: https://XXXXXX.execute-api.ap-northeast-1.amazonaws.com

アクセスしてみると、エンドポイントからレスポンスが返ってくることが確認できます。

curl https://XXXXXX.execute-api.ap-northeast-1.amazonaws.com
{"randomNumber":58}%

何が起きているのか

amplify publishで作成されたCloudFormationスタックの内容をざっと確認してみました。

  • VPC関連
  • ECS関連
    • ECR
    • ECSクラスター、サービス、タスク
    • CloudMap
  • ビルドデプロイ関連
    • CodePipeline
    • CodeBuild
  • エンドポイント
    • API Gateway

あたりが作成されていました。

パイプラインはこんな感じです。

参考記事にも書かれていますが、S3にコードが送信されると、

  • コンテナのビルド
  • ECRへPUSH
  • ECRのサービス更新(要求タスク数等)

をCodePipelineでまとめてやってくれるようです。

また、API Gatewayについては、HTTP APIで作成されていました。

VPCリンクやCloudMap等も作成されていたので、以下の記事で紹介されている HTTP APIの構成になっていると思われます。

知れば納得!HTTP API と REST API の VPC リンクは違うんやで。

まとめ

Amplify CLIでのコンテナアプリデプロイを試してみました。 コンテナアプリをVPC,ECS,デプロイパイプラインまで一気にデプロイしてくれるのはすごく楽ですね。

今回は試していませんが、docker-composeを使って複数コンテナを扱ったりもできるとの事ですので、 機会があれば試してみたいと思います。

参考

https://aws.amazon.com/jp/blogs/mobile/zero-effort-container-deployment-for-graphql-and-rest-apis-and-web-hosting-with-amplify-cli/

https://docs.amplify.aws/cli/usage/containers#supported-configurations