I am currently working on an AWS CDK stack that involves creating a Fargate task (using
ApplicationLoadBalancedFargateService
) from a docker container. The container houses a web application that needs to connect to a database. Upon deploying the CDK stack, I noticed that it takes approximately seven minutes for the database instance to be created. However, the Fargate task is launched much quicker, resulting in the task being stopped as it's unable to connect to the database which hasn't been fully created yet. This cycle repeats four times until the database is finally created.
My question - is there a way within the CDK code to delay the start of the Fargate task until the database creation process is completed?
For reference, here is a snippet of the CDK code with version 2.30.0 of the aws-cdk
library:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as ec2 from "aws-cdk-lib/aws-ec2";
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ecsp from 'aws-cdk-lib/aws-ecs-patterns';
import * as secretManager from "aws-cdk-lib/aws-secretsmanager";
import { Credentials, DatabaseInstance, DatabaseInstanceEngine, DatabaseSecret, PostgresEngineVersion } from 'aws-cdk-lib/aws-rds';
import { SecurityGroup } from 'aws-cdk-lib/aws-ec2';
// Rest of the CDK code omitted for brevity