タスク定義の例 - Amazon Elastic Container Service

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

タスク定義の例

例とスニペットをコピーして、独自のタスク定義の作成を開始できます。

例をコピーして、コンソールで [JSON 経由の設定] オプションを使用するときに貼り付けることができます。アカウント ID を使用するなど、例を必ずカスタマイズしてください。スニペットはタスク定義 JSON に含めることができます。詳細については、コンソールを使用したタスク定義の作成およびタスク定義パラメータを参照してください。

その他のタスク定義の例については、「」のAWS 「サンプルタスク定義」を参照してください GitHub。

ウェブサーバー

以下は、Web サーバーをセットアップするために、Fargate 起動タイプで Linux コンテナを使用するタスク定義の例です。

{ "containerDefinitions": [ { "command": [ "/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\"" ], "entryPoint": [ "sh", "-c" ], "essential": true, "image": "httpd:2.4", "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group" : "/ecs/fargate-task-definition", "awslogs-region": "us-east-1", "awslogs-stream-prefix": "ecs" } }, "name": "sample-fargate-app", "portMappings": [ { "containerPort": 80, "hostPort": 80, "protocol": "tcp" } ] } ], "cpu": "256", "executionRoleArn": "arn:aws:iam::012345678910:role/ecsTaskExecutionRole", "family": "fargate-task-definition", "memory": "512", "networkMode": "awsvpc", "runtimePlatform": { "operatingSystemFamily": "LINUX" }, "requiresCompatibilities": [ "FARGATE" ] }

以下は、Web サーバーをセットアップするために、Fargate 起動タイプで Windows コンテナを使用するタスク定義の例です。

{ "containerDefinitions": [ { "command": ["New-Item -Path C:\\inetpub\\wwwroot\\index.html -Type file -Value '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p>'; C:\\ServiceMonitor.exe w3svc"], "entryPoint": [ "powershell", "-Command" ], "essential": true, "cpu": 2048, "memory": 4096, "image": "mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019", "name": "sample_windows_app", "portMappings": [ { "hostPort": 80, "containerPort": 80, "protocol": "tcp" } ] } ], "memory": "4096", "cpu": "2048", "networkMode": "awsvpc", "family": "windows-simple-iis-2019-core", "executionRoleArn": "arn:aws:iam::012345678910:role/ecsTaskExecutionRole", "runtimePlatform": {"operatingSystemFamily": "WINDOWS_SERVER_2019_CORE"}, "requiresCompatibilities": ["FARGATE"] }

splunk ログドライバー

以下のスニペットでは、タスク定義で、リモートサービスにログを送信する splunk ログドライバーを使用する方法を示しています。Splunk トークンパラメータは、機密データとして扱われる可能性があるため、シークレットのオプションとして指定しています。詳細については、「コンテナへの機密データの受け渡し」を参照してください。

"containerDefinitions": [{ "logConfiguration": { "logDriver": "splunk", "options": { "splunk-url": "https://cloud.splunk.com:8080", "tag": "tag_name", }, "secretOptions": [{ "name": "splunk-token", "valueFrom": "arn:aws:secretsmanager:region:aws_account_id:secret:splunk-token-KnrBkD" }],

fluentd ログドライバー

以下のスニペットでは、タスク定義で、リモートサービスにログを送信する fluentd ログドライバーを使用する方法を示しています。fluentd-address 値は機密データとして扱われる可能性があるため、シークレットのオプションとして指定しています。詳細については、「コンテナへの機密データの受け渡し」を参照してください。

"containerDefinitions": [{ "logConfiguration": { "logDriver": "fluentd", "options": { "tag": "fluentd demo" }, "secretOptions": [{ "name": "fluentd-address", "valueFrom": "arn:aws:secretsmanager:region:aws_account_id:secret:fluentd-address-KnrBkD" }] }, "entryPoint": [], "portMappings": [{ "hostPort": 80, "protocol": "tcp", "containerPort": 80 }, { "hostPort": 24224, "protocol": "tcp", "containerPort": 24224 }] }],

gelf ログドライバー

以下のスニペットでは、タスク定義で、Gelf ログを入力として受け取る Logstash を実行しているリモートホストにログを送信する gelf ログドライバーを使用する方法を示しています。詳細については、「logConfiguration」を参照してください。

"containerDefinitions": [{ "logConfiguration": { "logDriver": "gelf", "options": { "gelf-address": "udp://logstash-service-address:5000", "tag": "gelf task demo" } }, "entryPoint": [], "portMappings": [{ "hostPort": 5000, "protocol": "udp", "containerPort": 5000 }, { "hostPort": 5000, "protocol": "tcp", "containerPort": 5000 } ] }],

外部インスタンスのワークロード

Amazon ECS タスク定義を登録するときは、requiresCompatibilitiesパラメーターを選択し、EXTERNALで、外部インスタンスで Amazon ECS ワークロードを実行するときに、タスク定義が使用できる互換性があることを検証します。コンソールを使用してタスク定義を登録する場合は、JSON エディターを使用する必要があります。詳細については、「コンソールを使用したタスク定義の作成」を参照してください。

重要

タスクにタスク実行 IAM ロールが必要な場合は、タスク定義で指定されていることを確認してください。

ワークロードをデプロイするときは、EXTERNAL起動タイプを、サービスの作成時またはスタンドアロンタスクの実行時に選択します。

以下は、タスク定義の例です。

Linux
{ "requiresCompatibilities": [ "EXTERNAL" ], "containerDefinitions": [{ "name": "nginx", "image": "public.ecr.aws/nginx/nginx:latest", "memory": 256, "cpu": 256, "essential": true, "portMappings": [{ "containerPort": 80, "hostPort": 8080, "protocol": "tcp" }] }], "networkMode": "bridge", "family": "nginx" }
Windows
{ "requiresCompatibilities": [ "EXTERNAL" ], "containerDefinitions": [{ "name": "windows-container", "image": "mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019", "memory": 256, "cpu": 512, "essential": true, "portMappings": [{ "containerPort": 80, "hostPort": 8080, "protocol": "tcp" }] }], "networkMode": "bridge", "family": "windows-container" }

Amazon ECR イメージとタスク定義 IAM ロール

以下のスニペットでは、aws-nodejs-sample という名前の Amazon ECR イメージで、123456789012.dkr.ecr.us-west-2.amazonaws.com レジストリの v1 タグを使用しています。このタスクのコンテナは、arn:aws:iam::123456789012:role/AmazonECSTaskS3BucketRole ロールから IAM アクセス許可を継承します。詳細については、「タスク IAM ロール」を参照してください。

{ "containerDefinitions": [ { "name": "sample-app", "image": "123456789012.dkr.ecr.us-west-2.amazonaws.com/aws-nodejs-sample:v1", "memory": 200, "cpu": 10, "essential": true } ], "family": "example_task_3", "taskRoleArn": "arn:aws:iam::123456789012:role/AmazonECSTaskS3BucketRole" }

コマンドを使用したエントリポイント

以下のスニペットでは、エントリポイントとコマンド引数を使用する Docker コンテナの構文を示しています。このコンテナは google.com に対して ping を 508 回実行してから終了します。

{ "containerDefinitions": [ { "memory": 32, "essential": true, "entryPoint": ["ping"], "name": "alpine_ping", "readonlyRootFilesystem": true, "image": "alpine:3.4", "command": [ "-c", "4", "example.com" ], "cpu": 16 } ], "family": "example_task_2" }

コンテナの依存関係

このスニペットでは、コンテナの依存関係が指定されている複数のコンテナを含むタスク定義の構文を示しています。次のタスク定義では、app コンテナ が開始される前に、envoy コンテナが必要とされるコンテナのヘルスチェックパラメータにより決定される正常なステータスに達する必要があります。詳しくは、「コンテナの依存関係」を参照してください。

{ "family": "appmesh-gateway", "runtimePlatform": { "operatingSystemFamily": "LINUX" }, "proxyConfiguration":{ "type": "APPMESH", "containerName": "envoy", "properties": [ { "name": "IgnoredUID", "value": "1337" }, { "name": "ProxyIngressPort", "value": "15000" }, { "name": "ProxyEgressPort", "value": "15001" }, { "name": "AppPorts", "value": "9080" }, { "name": "EgressIgnoredIPs", "value": "169.254.170.2,169.254.169.254" } ] }, "containerDefinitions": [ { "name": "app", "image": "application_image", "portMappings": [ { "containerPort": 9080, "hostPort": 9080, "protocol": "tcp" } ], "essential": true, "dependsOn": [ { "containerName": "envoy", "condition": "HEALTHY" } ] }, { "name": "envoy", "image": "840364872350.dkr.ecr.region-code.amazonaws.com/aws-appmesh-envoy:v1.15.1.0-prod", "essential": true, "environment": [ { "name": "APPMESH_VIRTUAL_NODE_NAME", "value": "mesh/meshName/virtualNode/virtualNodeName" }, { "name": "ENVOY_LOG_LEVEL", "value": "info" } ], "healthCheck": { "command": [ "CMD-SHELL", "echo hello" ], "interval": 5, "timeout": 2, "retries": 3 } } ], "executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole", "networkMode": "awsvpc" }

Windows のサンプルのタスク定義

Amazon ECS で Windows コンテナを使い始める際に役立つタスク定義サンプルを以下に示します。

例 Windows 用の Amazon ECS コンソールサンプルアプリケーション

次のタスク定義は、Amazon ECS の初回実行ウィザードで生成される Amazon ECS コンソールのサンプルアプリケーションです。microsoft/iis Windows コンテナイメージを使用するために移植されています。

{ "family": "windows-simple-iis", "containerDefinitions": [ { "name": "windows_sample_app", "image": "mcr.microsoft.com/windows/servercore/iis", "cpu": 1024, "entryPoint":["powershell", "-Command"], "command":["New-Item -Path C:\\inetpub\\wwwroot\\index.html -Type file -Value '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p>'; C:\\ServiceMonitor.exe w3svc"], "portMappings": [ { "protocol": "tcp", "containerPort": 80 } ], "memory": 1024, "essential": true } ], "networkMode": "awsvpc", "memory": "1024", "cpu": "1024" }