AWS App RunnerのサービスをAWS Copilotで起動させてみた

こんにちは。サービスグループの武田です。コンテナ化されたWebアプリケーションなどを手軽に実行できる新しいサービス、AWS App RunnerがAWS Copilotからも起動できるとのことで試してみました。
2021.05.19

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

こんにちは。サービスグループの武田です。

コンテナ化されたWebアプリケーションなどを手軽に実行できる新しいサービス、AWS App Runnerがローンチされましたね!

ドキュメントを読んでいるとAWS Copilotからも簡単に起動できるよといったことが書かれていたため、試してみました。

実行環境

手元の環境にはAWS Copilotがインストールされていなかったので、今回はAWS CloudShellで実行しました。ところで、CloudShellにプリインストールされているCopilotはv1.2.0です。

$ copilot --version
copilot version: v1.2.0

リリースを追ってみたところ、App Runnerがサポートされるのはv1.7.0以降のようです。というわけでアップデートしましょう。

$ sudo curl -Lo /usr/local/bin/copilot https://github.com/aws/copilot-cli/releases/latest/download/copilot-linux \
   && sudo chmod +x /usr/local/bin/copilot \
   && copilot --version 
copilot version: v1.7.0

またCopilotはconfigファイルがないとデプロイに失敗するため、先にファイルを作成しておきます。

$ mkdir .aws
$ echo '[default]' >> .aws/config

やってみた

冒頭に掲載した吉田のエントリ同様、nginxのコンテナをサービスとして実行してみます。アプリケーション名をhello-app-runner、サービス名をnginxとします。

$ copilot init
Note: It's best to run this command in the root of your Git repository.
Welcome to the Copilot CLI! We're going to walk you through some questions
to help you get set up with a containerized application on AWS. An application is a collection of
containerized services that operate together.
  What would you like to name your application? [? for help] hello-app-runner
  Which workload type best represents your architecture?  [Use arrows to move, type to filter, ? for more help]
  > Request-Driven Web Service  (App Runner)
    Load Balanced Web Service   (Internet to ECS on Fargate)
    Backend Service             (ECS on Fargate)
    Scheduled Job               (Scheduled event to State Machine to Fargate)
  What do you want to name this Request-Driven Web Service? [? for help] nginx
  What's the location of the image to use? [? for help] public.ecr.aws/nginx/nginx:latest
  Which port do you want customer traffic sent to? [? for help] (80)

ここでいったん処理が走るので少し待ちます。

テスト環境としてデプロイするかの質問にはyを入力しました。ここで作らなくても後から任意の環境を作成できます。以下続き。

Ok great, we'll set up a Request-Driven Web Service named nginx in application hello-app-runner listening on port 80.

✔ Created the infrastructure to manage services and jobs under application hello-app-runner.

✔ Wrote the manifest for service nginx at copilot/nginx/manifest.yml
Your manifest contains configurations like your container size and port (:80).

✔ Created ECR repositories for service nginx.

All right, you're all set for local development.

  Would you like to deploy a test environment? [? for help] (y/N) y

Deploy: Yes


✔ Proposing infrastructure changes for the hello-app-runner-test environment.
- Creating the infrastructure for the hello-app-runner-test environment.  [create complete]  [82.8s]
  - An IAM Role for AWS CloudFormation to manage resources                [create complete]  [21.9s]
  - An ECS cluster to group your services                                 [create complete]  [10.9s]
  - Enable long ARN formats for the authenticated AWS principal           [create complete]  [2.4s]
  - An IAM Role to describe resources in your environment                 [create complete]  [21.9s]
  - A security group to allow your containers to talk to each other       [create complete]  [4.4s]
  - An Internet Gateway to connect to the public internet                 [create complete]  [16.4s]
  - Private subnet 1 for resources with no internet access                [create complete]  [18.6s]
  - Private subnet 2 for resources with no internet access                [create complete]  [18.6s]
  - Public subnet 1 for resources that can access the internet            [create complete]  [18.6s]
  - Public subnet 2 for resources that can access the internet            [create complete]  [18.6s]
  - A Virtual Private Cloud to control networking of your AWS resources   [create complete]  [16.4s]
✔ Created environment test in region ap-northeast-1 under application hello-app-runner.
Environment test is already on the latest version v1.4.0, skip upgrade.
✔ Proposing infrastructure changes for stack hello-app-runner-test-nginx
- Creating the infrastructure for stack hello-app-runner-test-nginx        [create complete]  [266.6s]
  - An IAM role to control permissions for the containers in your service  [create complete]  [17.6s]
  - An App Runner service to run and manage your containers                [create complete]  [239.8s]
✔ Deployed nginx, you can access it at https://zhmva6fvw3.ap-northeast-1.awsapprunner.com.

デプロイが完了したのでアクセスしてみましょう。

アクセスできました!

作成されるマニフェストファイル

先ほどのコマンドで作成されるマニフェストファイルを掲載します。これをベースに環境の設定などが調整できます。

copilot/nginx/manifest.yml

# The manifest for the "nginx" service.
# Read the full specification for the "Request-Driven Web Service" type at:
# https://aws.github.io/copilot-cli/docs/manifest/rd-web-service/

# Your service name will be used in naming your resources like log groups, App Runner services, etc.
name: nginx
# The "architecture" of the service you're running.
type: Request-Driven Web Service

image:
  # The name of the Docker image.
  location: public.ecr.aws/nginx/nginx:latest
  # Port exposed through your container to route traffic to it.
  port: 80

# http:
#   healthcheck:
#     path: /
#     healthy_threshold: 3
#     unhealthy_threshold: 5
#     interval: 10s
#     timeout: 5s

# Number of CPU units for the task.
cpu: 1024
# Amount of memory in MiB used by the task.
memory: 2048

# Optional fields for more advanced use-cases.
#
# variables:                    # Pass environment variables as key value pairs.
#   LOG_LEVEL: info
#
# tags:                         # Pass tags as key value pairs.
#   project: project-name

# You can override any of the values defined above by environment.
# environments:
#   test:
#     variables:
#       LOG_LEVEL: debug        # Log level for the "test" environment.

注意点

Copilotの仕様上、環境を作成すると自動的にVPCやECSクラスターが自動的に作成されます。おそらくこれを抑制する手段は今のところありません。使わないリソースの作成を避けたい方は、CopilotではなくマネジメントコンソールないしはAWS CLIなどを利用した方がよいでしょう。とはいえ、料金が発生するわけではありません。管理方法なども含め検討してください。

まとめ

簡単ではありますがCopilotを試してみました。普段からCopilotを利用されている方は、使い慣れたコマンドで操作できてうれしいですね。