The Container Does not Exist in the Task Definition
ECS Serviceを作成する次のTerraformのコードをapply
すると、The container does not exist in the task definition
というエラーになります。
resource "aws_ecs_task_definition" "fastapi" {
family = "${var.app_name}-fastapi"
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
execution_role_arn = aws_iam_role.ecs_execution_role.arn
task_role_arn = aws_iam_role.ecs_execution_role.arn
cpu = 256
memory = 512
container_definitions = jsonencode([
{
name = "${var.app_name}-fastapi"
image = "${data.aws_ecr_repository.fastapi.repository_url}:latest"
command = ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
portMappings = [
{
hostPort = 80
containerPort = 80
protocol = "tcp"
}
],
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-region = "ap-northeast-1"
awslogs-stream-prefix = "ecs"
awslogs-group = aws_cloudwatch_log_group.ecs.name
}
},
environment = []
}
])
}
resource "aws_ecs_service" "fastapi" {
name = "${var.app_name}-fastapi"
cluster = aws_ecs_cluster.main.name
launch_type = "FARGATE"
desired_count = 1
task_definition = aws_ecs_task_definition.fastapi.arn
network_configuration {
subnets = [for s in aws_subnet.privates : s.id]
security_groups = [aws_security_group.ecs.id]
}
load_balancer {
target_group_arn = aws_lb_target_group.main.arn
container_name = "my-fastapi"
container_port = "80"
}
lifecycle {
ignore_changes = [
desired_count,
]
}
depends_on = [aws_lb_listener_rule.main]
}
│ Error: creating ECS Service (fastapi): InvalidParameterException: The container my-fastapi does not exist in the task definition.
│
│ with aws_ecs_service.fastapi,
│ on ecs.tf line 40, in resource "aws_ecs_service" "fastapi":
│ 40: resource "aws_ecs_service" "fastapi" {
│
解決法
aws_ecs_task_definition
で定義するコンテナ名は、aws_ecs_service
のload_balancer
設定のcontainer_name
のものと一致させる必要があります。次のようにaws_ecs_service
を修正するとapply
できるようになります。
resource "aws_ecs_service" "fastapi" {
name = "${var.app_name}-fastapi"
cluster = aws_ecs_cluster.main.name
launch_type = "FARGATE"
desired_count = 1
task_definition = aws_ecs_task_definition.fastapi.arn
network_configuration {
subnets = [for s in aws_subnet.privates : s.id]
security_groups = [aws_security_group.ecs.id]
}
load_balancer {
target_group_arn = aws_lb_target_group.main.arn
- container_name = "my-fastapi"
+ container_name = "${var.app_name}-fastapi"
container_port = "80"
}
lifecycle {
ignore_changes = [
desired_count,
]
}
depends_on = [aws_lb_listener_rule.main]
}
参考
AlloyDB
Amazon Cognito
Amazon EC2
Amazon ECS
Amazon QuickSight
Amazon RDS
Amazon Redshift
Amazon S3
API
Autonomous Vehicle
AWS
AWS API Gateway
AWS Chalice
AWS Control Tower
AWS IAM
AWS Lambda
AWS VPC
BERT
BigQuery
Causal Inference
ChatGPT
Chrome Extension
CircleCI
Classification
Cloud Functions
Cloud IAM
Cloud Run
Cloud Storage
Clustering
CSS
Data Engineering
Data Modeling
Database
dbt
Decision Tree
Deep Learning
Descriptive Statistics
Differential Equation
Dimensionality Reduction
Discrete Choice Model
Docker
Economics
FastAPI
Firebase
GIS
git
GitHub
GitHub Actions
Google
Google Cloud
Google Search Console
Hugging Face
Hypothesis Testing
Inferential Statistics
Interval Estimation
JavaScript
Jinja
Kedro
Kubernetes
LightGBM
Linux
LLM
Mac
Machine Learning
Macroeconomics
Marketing
Mathematical Model
Meltano
MLflow
MLOps
MySQL
NextJS
NLP
Nodejs
NoSQL
ONNX
OpenAI
Optimization Problem
Optuna
Pandas
Pinecone
PostGIS
PostgreSQL
Probability Distribution
Product
Project
Psychology
Python
PyTorch
QGIS
R
ReactJS
Regression
Rideshare
SEO
Singer
sklearn
Slack
Snowflake
Software Development
SQL
Statistical Model
Statistics
Streamlit
Tabular
Tailwind CSS
TensorFlow
Terraform
Transportation
TypeScript
Urban Planning
Vector Database
Vertex AI
VSCode
XGBoost