指定 aws ECS/Fargate 容器依赖项无法部署
Posted
技术标签:
【中文标题】指定 aws ECS/Fargate 容器依赖项无法部署【英文标题】:specifying aws ECS/Fargate container dependency fails to deply 【发布时间】:2020-10-08 05:29:44 【问题描述】:我使用 aws-cdk Python 语言获得了以下代码,但未能部署,出现 redis_container not available 错误,我做错了什么,我希望先启动 redis 容器,然后再启动其余容器。可能我对容器依赖的理解不正确??
ecs_redis_task = ecs.FargateTaskDefinition(self,
id = 'redis',
cpu=512,
memory_limit_mib =1024
)
redis_container = ecs_redis_task.add_container(id = 'redis_container',
image = img_.from_ecr_repository(repository=repo_, tag='redis_5.0.5')
)
redis_container.add_port_mappings(
'containerPort' : 6379
)
redis_dependency = ecs.ContainerDependency(container = redis_container, condition = ecs.ContainerDependencyCondition.HEALTHY)
ecs_webserver_task = ecs.FargateTaskDefinition(self,
id = 'webserver',
cpu=256,
memory_limit_mib =512
)
webserver_container = ecs_webserver_task.add_container(id = 'webserver_container',
image = img_.from_ecr_repository(repository=repo_, tag='airflow_1.10.9')
)
webserver_container.add_port_mappings(
'containerPort' : 8080
)
webserver_container.add_container_dependencies(redis_dependency)
如果我删除依赖代码,它可以正常部署!
错误:
12/24 | 2:46:51 PM | CREATE_FAILED | AWS::ECS::TaskDefinition | webserver (webserverEE139216) Cannot depend on container + 'redis_container' because it does not exist (Service: AmazonECS; Status Code: 400; Error Code: ClientException; Request ID: 81828979-9e65-474e-ab0e-b163168d5613)
【问题讨论】:
确切的错误是什么 添加了错误,它没有提供太多 这是一个不允许的循环依赖。 @shariqmaws 你能指出循环依赖在哪里吗? 【参考方案1】:我刚刚尝试了这段代码,它按预期工作,在任务定义中添加了依赖项,你的代码唯一改变的是图像1:
from aws_cdk import (
#aws_s3 as s3,
aws_ecs as ecs,
core
)
class HelloCdkStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
#bucket = s3.Bucket(self, "MyFirstBucket", versioned=True,)
ecs_redis_task = ecs.FargateTaskDefinition(self, id='redis', cpu=512, memory_limit_mib=1024)
redis_container = ecs_redis_task.add_container(id = 'redis_container', image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"),)
redis_container.add_port_mappings(
'containerPort' : 6379
)
redis_dependency = ecs.ContainerDependency(container = redis_container, condition = ecs.ContainerDependencyCondition.HEALTHY)
ecs_webserver_task = ecs.FargateTaskDefinition(self, id='webserver', cpu=256, memory_limit_mib=512)
webserver_container = ecs_webserver_task.add_container(id = 'webserver_container', image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"),)
webserver_container.add_port_mappings(
'containerPort' : 8080
)
webserver_container.add_container_dependencies(redis_dependency)
cdk 合成器后的 CloudFormation:
"webserverEE139216":
"Type": "AWS::ECS::TaskDefinition",
"Properties":
"ContainerDefinitions": [
"DependsOn": [
"Condition": "HEALTHY",
"ContainerName": "redis_container"
],
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"Name": "webserver_container",
"PortMappings": [
"ContainerPort": 8080,
"Protocol": "tcp"
]
],
【讨论】:
感谢您的回答,我能够解决问题,看起来 ecr.Repository.from_repository_name() 存在问题,它没有返回正确的参考:arn 看起来像arn:$Token[AWS::Partition.3]:ecr:$Token[AWS::Region.4]:$Token[AWS::AccountId.0]:repository/airflow
已更改现在它到 ecr.Repository.from_repository_arn() 参考工作正常。 arn:aws:ecr:us-east-1:xxxxxxxxxxxx:repository/airflow
以上是关于指定 aws ECS/Fargate 容器依赖项无法部署的主要内容,如果未能解决你的问题,请参考以下文章