In this step, we will explore packaging and launching our Spring Boot container on AWS Fargate (Amazon ECS Serverless compute engine) alongside an AWS X-Ray daemon sidecar, and configuring an internal Application Load Balancer (ALB) for private traffic routing.
The application runs entirely within the Private Subnet with Egress provisioned in step 4:
fcaj-products-cluster): Logical grouping managing container services and tasks.ProductsTaskDef): Specifies container resource allocation and behaviors:
web): Runs the Spring Boot app pulled from ECR fcaj-products-service:1.0.0, listens on port 8080, and streams logs to CloudWatch.xray-daemon): Runs public.ecr.aws/xray/aws-xray-daemon:latest, listens on UDP port 2000, and relays application trace data to AWS X-Ray.AWSXRayDaemonWriteAccess policy.fcaj-products-service): Maintains desired task count (desiredCount: 1), with automatic recovery enabled by the deployment circuit breaker. Tasks run without public IPs for maximum security.InternalAlb): Placed within Private Subnets, listening on port 80 and forwarding requests to container port 8080 guided by the health check at /actuator/health.ecs-stack.ts)Navigate to IaC-ECS-CDK/cdk/lib and open ecs-stack.ts (or create it). We will construct each component of the Stack step by step below:
First, import the required AWS CDK constructs and define the EcsStackProps interface to accept dependency inputs (vpc, table, repository):
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
interface EcsStackProps extends cdk.StackProps {
vpc: ec2.IVpc;
table: dynamodb.ITable;
repository: ecr.IRepository;
}
export class EcsStack extends cdk.Stack {
public readonly alb: elbv2.ApplicationLoadBalancer;
public readonly albListener: elbv2.ApplicationListener;
public readonly fargateService: ecs.FargateService;
constructor(scope: Construct, id: string, props: EcsStackProps) {
super(scope, id, props);
Create the ECS Cluster attached to our private VPC:
// 1. Initialize ECS Cluster within the VPC
const cluster = new ecs.Cluster(this, 'ProductsServiceCluster', {
clusterName: 'fcaj-products-cluster',
vpc: props.vpc,
});
Allocate container resources (CPU 512, RAM 1024 MiB) and grant the Task Role permissions to read/write DynamoDB and stream traces to AWS X-Ray:
// 2. Define Fargate Task Definition (CPU 512, RAM 1024 MiB)
const taskDefinition = new ecs.FargateTaskDefinition(this, 'ProductsTaskDef', {
cpu: 512,
memoryLimitMiB: 1024,
});
// Grant DynamoDB and X-Ray permissions to Task Role
props.table.grantReadWriteData(taskDefinition.taskRole);
taskDefinition.taskRole.addManagedPolicy(
iam.ManagedPolicy.fromAwsManagedPolicyName('AWSXRayDaemonWriteAccess')
);
Configure two containers running concurrently within the same Task:
web container: Pulls image from ECR (tag 1.0.0), maps port 8080, and configures CloudWatch logging.xray-daemon: Collects trace data over UDP port 2000. // 3. Add primary Spring Boot Web Container
const appContainer = taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromEcrRepository(props.repository, '1.0.0'),
logging: new ecs.AwsLogDriver({ streamPrefix: 'products-service' }),
environment: {
SPRING_PROFILES_ACTIVE: 'default',
AWS_REGION: this.region,
AWS_XRAY_DAEMON_ADDRESS: '127.0.0.1:2000',
},
});
appContainer.addPortMappings({ containerPort: 8080 });
// 4. Add AWS X-Ray Daemon Sidecar Container
const xrayContainer = taskDefinition.addContainer('xray-daemon', {
image: ecs.ContainerImage.fromRegistry('public.ecr.aws/xray/aws-xray-daemon:latest'),
cpu: 32,
memoryLimitMiB: 256,
logging: new ecs.AwsLogDriver({ streamPrefix: 'xray' }),
});
xrayContainer.addPortMappings({ containerPort: 2000, protocol: ecs.Protocol.UDP });
Instantiate the service with desiredCount: 1, hosted in PRIVATE_WITH_EGRESS subnets with automated rollback on deployment failure:
// 5. Initialize ECS Fargate Service in Private Subnets
this.fargateService = new ecs.FargateService(this, 'ProductsFargateService', {
cluster,
taskDefinition,
serviceName: 'fcaj-products-service',
desiredCount: 1,
assignPublicIp: false,
circuitBreaker: {
rollback: true,
},
minHealthyPercent: 100,
maxHealthyPercent: 200,
vpcSubnets: {
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
},
});
Provision the internal Application Load Balancer (internetFacing: false), establish listener port 80, and route traffic to service port 8080 verified by health checks at /actuator/health:
// 6. Initialize Internal Application Load Balancer
this.alb = new elbv2.ApplicationLoadBalancer(this, 'InternalAlb', {
vpc: props.vpc,
internetFacing: false,
vpcSubnets: {
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
},
});
this.albListener = this.alb.addListener('AlbListener', {
port: 80,
open: true,
});
// Forward traffic to ECS Tasks with Health Check configuration
this.albListener.addTargets('EcsTargets', {
port: 8080,
protocol: elbv2.ApplicationProtocol.HTTP,
targets: [this.fargateService],
healthCheck: {
path: '/actuator/health',
healthyHttpCodes: '200',
interval: cdk.Duration.seconds(30),
},
});
}
}
ecs-stack.tsBringing all pieces together, the full ecs-stack.ts file is structured as follows:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
interface EcsStackProps extends cdk.StackProps {
vpc: ec2.IVpc;
table: dynamodb.ITable;
repository: ecr.IRepository;
}
export class EcsStack extends cdk.Stack {
public readonly alb: elbv2.ApplicationLoadBalancer;
public readonly albListener: elbv2.ApplicationListener;
public readonly fargateService: ecs.FargateService;
constructor(scope: Construct, id: string, props: EcsStackProps) {
super(scope, id, props);
const cluster = new ecs.Cluster(this, 'ProductsServiceCluster', {
clusterName: 'fcaj-products-cluster',
vpc: props.vpc,
});
const taskDefinition = new ecs.FargateTaskDefinition(this, 'ProductsTaskDef', {
cpu: 512,
memoryLimitMiB: 1024,
});
props.table.grantReadWriteData(taskDefinition.taskRole);
taskDefinition.taskRole.addManagedPolicy(
iam.ManagedPolicy.fromAwsManagedPolicyName('AWSXRayDaemonWriteAccess')
);
const appContainer = taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromEcrRepository(props.repository, '1.0.0'),
logging: new ecs.AwsLogDriver({ streamPrefix: 'products-service' }),
environment: {
SPRING_PROFILES_ACTIVE: 'default',
AWS_REGION: this.region,
AWS_XRAY_DAEMON_ADDRESS: '127.0.0.1:2000',
},
});
appContainer.addPortMappings({ containerPort: 8080 });
const xrayContainer = taskDefinition.addContainer('xray-daemon', {
image: ecs.ContainerImage.fromRegistry('public.ecr.aws/xray/aws-xray-daemon:latest'),
cpu: 32,
memoryLimitMiB: 256,
logging: new ecs.AwsLogDriver({ streamPrefix: 'xray' }),
});
xrayContainer.addPortMappings({ containerPort: 2000, protocol: ecs.Protocol.UDP });
this.fargateService = new ecs.FargateService(this, 'ProductsFargateService', {
cluster,
taskDefinition,
serviceName: 'fcaj-products-service',
desiredCount: 1,
assignPublicIp: false,
circuitBreaker: {
rollback: true,
},
minHealthyPercent: 100,
maxHealthyPercent: 200,
vpcSubnets: {
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
},
});
this.alb = new elbv2.ApplicationLoadBalancer(this, 'InternalAlb', {
vpc: props.vpc,
internetFacing: false,
vpcSubnets: {
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
},
});
this.albListener = this.alb.addListener('AlbListener', {
port: 80,
open: true,
});
this.albListener.addTargets('EcsTargets', {
port: 8080,
protocol: elbv2.ApplicationProtocol.HTTP,
targets: [this.fargateService],
healthCheck: {
path: '/actuator/health',
healthyHttpCodes: '200',
interval: cdk.Duration.seconds(30),
},
});
}
}
