'AWS CDK Init Container dependency with Fargate Pattern
I'm trying to use the CDK to define a ApplicationLoadBalancedFargateService like this:
const cluster = new ecs.Cluster(this, 'Cluster', { vpc });
const loadBalancedService = new ecs_patterns.ApplicationLoadBalancedFargateService(this, "FargateService", {
cluster,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry("public.ecr.aws/XXXXXXXX/my-node-app:latest"),
environment: {
DATABASE_HOST: rdsCluster.attrEndpointAddress,
DATABASE_NAME: databaseName,
DATABASE_USERNAME: databaseCredentialsSecret.secretValueFromJson('username').toString(),
DATABASE_PASSWORD: databaseCredentialsSecret.secretValueFromJson('password').toString(),
}
},
});
I already have an RDS cluster created earlier in the app. I want to run an init container before bringing up this Fargate Service but I'm not quite sure how. I know there is a ContainerDependency in the aws-ecs construct but I don't think I can apply it here.
Solution 1:[1]
ECS Containers can have dependencies. In the CDK, you can add a dependency to a ContainerDefinition construct using its addContainerDependencies method.
As you are using the L3 ecs_patterns.ApplicationLoadBalancedFargateService construct, your first step is getting a reference to the underlying service's ContainerDefinition via the task definition's defaultContainer property:
const serviceContainer: : ecs.ContainerDefinition | undefined = loadBalancedService.taskDefinition.defaultContainer;
if (!serviceContainer) throw new Error('LB Service default container must be defined!');
Then add the init Container to the task definition:
const initContainer = loadBalancedService.taskDefinition.addContainer('init', {
image: ecs.ContainerImage.fromRegistry('init/my-image'),
essential: false,
});
Finally, create the dependency with a SUCCESS condition. This condition validates that a dependent container runs to completion (and exits with 0) before permitting other containers to start:
serviceContainer.addContainerDependencies({
container: initContainer,
condition: ecs.ContainerDependencyCondition.SUCCESS,
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
