Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ecs-patterns): memory limit is not set at the container level #21201

Merged
merged 11 commits into from
Jul 25, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalanc
const containerName = taskImageOptions.containerName ?? 'web';
const container = this.taskDefinition.addContainer(containerName, {
image: taskImageOptions.image,
logging: logDriver,
cpu: props.cpu,
memoryLimitMiB: props.memoryLimitMiB,
environment: taskImageOptions.environment,
secrets: taskImageOptions.secrets,
logging: logDriver,
dockerLabels: taskImageOptions.dockerLabels,
});
container.addPortMappings({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ export class ApplicationMultipleTargetGroupsFargateService extends ApplicationMu
const containerName = taskImageOptions.containerName ?? 'web';
const container = this.taskDefinition.addContainer(containerName, {
image: taskImageOptions.image,
logging: this.logDriver,
cpu: props.cpu,
memoryLimitMiB: props.memoryLimitMiB,
environment: taskImageOptions.environment,
secrets: taskImageOptions.secrets,
logging: this.logDriver,
dockerLabels: taskImageOptions.dockerLabels,
});
if (taskImageOptions.containerPorts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServic
const containerName = taskImageOptions.containerName ?? 'web';
const container = this.taskDefinition.addContainer(containerName, {
image: taskImageOptions.image,
logging: logDriver,
cpu: props.cpu,
memoryLimitMiB: props.memoryLimitMiB,
environment: taskImageOptions.environment,
secrets: taskImageOptions.secrets,
logging: logDriver,
dockerLabels: taskImageOptions.dockerLabels,
});
container.addPortMappings({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ export class NetworkMultipleTargetGroupsFargateService extends NetworkMultipleTa
const containerName = taskImageOptions.containerName ?? 'web';
const container = this.taskDefinition.addContainer(containerName, {
image: taskImageOptions.image,
logging: this.logDriver,
cpu: props.cpu,
memoryLimitMiB: props.memoryLimitMiB,
environment: taskImageOptions.environment,
secrets: taskImageOptions.secrets,
logging: this.logDriver,
dockerLabels: taskImageOptions.dockerLabels,
});
if (taskImageOptions.containerPorts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,22 @@ export class QueueProcessingFargateService extends QueueProcessingServiceBase {
constructor(scope: Construct, id: string, props: QueueProcessingFargateServiceProps) {
super(scope, id, props);

const cpu = props.cpu || 256;
const memoryLimitMiB = props.memoryLimitMiB || 512;

// Create a Task Definition for the container to start
this.taskDefinition = new FargateTaskDefinition(this, 'QueueProcessingTaskDef', {
memoryLimitMiB: props.memoryLimitMiB || 512,
cpu: props.cpu || 256,
cpu,
memoryLimitMiB,
family: props.family,
});

const containerName = props.containerName ?? 'QueueProcessingContainer';

this.taskDefinition.addContainer(containerName, {
image: props.image,
cpu,
memoryLimitMiB,
command: props.command,
environment: this.environment,
secrets: this.secrets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,17 @@ export class ScheduledFargateTask extends ScheduledTaskBase {
this.taskDefinition = props.scheduledFargateTaskDefinitionOptions.taskDefinition;
} else if (props.scheduledFargateTaskImageOptions) {
const taskImageOptions = props.scheduledFargateTaskImageOptions;
const cpu = taskImageOptions.cpu || 256;
const memoryLimitMiB = taskImageOptions.memoryLimitMiB || 512;

this.taskDefinition = new FargateTaskDefinition(this, 'ScheduledTaskDef', {
memoryLimitMiB: taskImageOptions.memoryLimitMiB || 512,
cpu: taskImageOptions.cpu || 256,
memoryLimitMiB,
cpu,
});
this.taskDefinition.addContainer('ScheduledContainer', {
image: taskImageOptions.image,
memoryLimitMiB,
cpu,
command: taskImageOptions.command,
environment: taskImageOptions.environment,
secrets: taskImageOptions.secrets,
Expand Down
15 changes: 11 additions & 4 deletions packages/@aws-cdk/aws-ecs-patterns/test/ec2/l3s.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,8 @@ test('test Fargate loadbalanced construct', () => {
// WHEN
new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', {
cluster,
cpu: 1024,
memoryLimitMiB: 2048,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('test'),
environment: {
Expand All @@ -515,6 +517,11 @@ test('test Fargate loadbalanced construct', () => {
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
Match.objectLike({
Cpu: 1024,
DockerLabels: {
label1: 'labelValue1',
label2: 'labelValue2',
},
Environment: [
{
Name: 'TEST_ENVIRONMENT_VARIABLE1',
Expand All @@ -525,6 +532,7 @@ test('test Fargate loadbalanced construct', () => {
Value: 'test environment variable 2 value',
},
],
Image: 'test',
LogConfiguration: {
LogDriver: 'awslogs',
Options: {
Expand All @@ -533,12 +541,11 @@ test('test Fargate loadbalanced construct', () => {
'awslogs-region': { Ref: 'AWS::Region' },
},
},
DockerLabels: {
label1: 'labelValue1',
label2: 'labelValue2',
},
Memory: 2048,
}),
],
Cpu: '1024',
Memory: '2048',
});

Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@
}
}
},
"Memory": 512,
"Name": "web",
"PortMappings": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@
}
}
},
"Memory": 512,
"Name": "web",
"PortMappings": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 256,
"Environment": [
{
"Name": "QUEUE_NAME",
Expand Down Expand Up @@ -504,6 +505,7 @@
}
}
},
"Memory": 512,
"Name": "QueueProcessingContainer"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 512,
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"LogConfiguration": {
Expand All @@ -578,6 +579,7 @@
}
}
},
"Memory": 1024,
"Name": "web",
"PortMappings": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 512,
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"LogConfiguration": {
Expand All @@ -143,6 +144,7 @@
}
}
},
"Memory": 1024,
"Name": "web",
"PortMappings": [
{
Expand Down Expand Up @@ -775,6 +777,7 @@
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 512,
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"LogConfiguration": {
Expand All @@ -789,6 +792,7 @@
}
}
},
"Memory": 1024,
"Name": "web",
"PortMappings": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 512,
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"LogConfiguration": {
Expand All @@ -542,6 +543,7 @@
}
}
},
"Memory": 1024,
"Name": "web",
"PortMappings": [
{
Expand Down Expand Up @@ -799,6 +801,7 @@
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 512,
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"LogConfiguration": {
Expand All @@ -813,6 +816,7 @@
}
}
},
"Memory": 1024,
"Name": "web",
"PortMappings": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 512,
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"LogConfiguration": {
Expand All @@ -526,6 +527,7 @@
}
}
},
"Memory": 1024,
"Name": "web",
"PortMappings": [
{
Expand Down Expand Up @@ -775,6 +777,7 @@
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 512,
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"LogConfiguration": {
Expand All @@ -789,6 +792,7 @@
}
}
},
"Memory": 1024,
"Name": "web",
"PortMappings": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 512,
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"LogConfiguration": {
Expand All @@ -529,6 +530,7 @@
}
}
},
"Memory": 1024,
"Name": "web",
"PortMappings": [
{
Expand Down Expand Up @@ -775,6 +777,7 @@
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 512,
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"LogConfiguration": {
Expand All @@ -789,6 +792,7 @@
}
}
},
"Memory": 1024,
"Name": "web",
"PortMappings": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@
}
}
},
"Memory": 512,
"Name": "web",
"PortMappings": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 256,
"Environment": [
{
"Name": "QUEUE_NAME",
Expand Down Expand Up @@ -813,6 +814,7 @@
}
}
},
"Memory": 512,
"Name": "QueueProcessingContainer"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 256,
"Environment": [
{
"Name": "QUEUE_NAME",
Expand Down Expand Up @@ -513,6 +514,7 @@
}
}
},
"Memory": 512,
"Name": "QueueProcessingContainer"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 256,
"Environment": [
{
"Name": "QUEUE_NAME",
Expand Down Expand Up @@ -504,6 +505,7 @@
}
}
},
"Memory": 512,
"Name": "QueueProcessingContainer"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 256,
"Environment": [
{
"Name": "TRIGGER",
Expand Down Expand Up @@ -323,6 +324,7 @@
}
}
},
"Memory": 512,
"Name": "ScheduledContainer"
}
],
Expand Down
Loading