Skip to content

Commit e6073fc

Browse files
authored
chore(apprunner): add validation cpu and memory for app runner (#25877)
This PR adds the same validation for the App Runner's CPU and Memory values as [CloudFormation's input patterns](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apprunner-service-instanceconfiguration.html#cfn-apprunner-service-instanceconfiguration-cpu). Closes #25872 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 12fe60e commit e6073fc

File tree

2 files changed

+96
-8
lines changed

2 files changed

+96
-8
lines changed

packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,19 @@ export class Cpu {
6161
*
6262
* @param unit custom CPU unit
6363
*/
64-
public static of(unit: string) { return new Cpu(unit); }
64+
public static of(unit: string): Cpu {
65+
const numericPatterns = ['256', '512', '1024', '2048', '4096'];
66+
const unitPatterns = ['0.25 vCPU', '0.5 vCPU', '1 vCPU', '2 vCPU', '4 vCPU'];
67+
const allowedPatterns = numericPatterns.concat(unitPatterns);
68+
const isValidValue = allowedPatterns.some(
69+
(pattern) => pattern === unit,
70+
);
71+
if (!isValidValue) {
72+
throw new Error('CPU value is invalid');
73+
};
74+
75+
return new Cpu(unit);
76+
}
6577

6678
/**
6779
*
@@ -126,7 +138,19 @@ export class Memory {
126138
*
127139
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apprunner-service-instanceconfiguration.html#cfn-apprunner-service-instanceconfiguration-memory
128140
*/
129-
public static of(unit: string) { return new Memory(unit); }
141+
public static of(unit: string): Memory {
142+
const numericPatterns = ['512', '1024', '2048', '3072', '4096', '6144', '8192', '10240', '12288'];
143+
const unitPatterns = ['0.5 GB', '1 GB', '2 GB', '3 GB', '4 GB', '6 GB', '8 GB', '10 GB', '12 GB'];
144+
const allowedPatterns = numericPatterns.concat(unitPatterns);
145+
const isValidValue = allowedPatterns.some(
146+
(pattern) => pattern === unit,
147+
);
148+
if (!isValidValue) {
149+
throw new Error('Memory value is invalid');
150+
};
151+
152+
return new Memory(unit);
153+
}
130154

131155
/**
132156
*

packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ test('custom IAM access role and instance role are allowed', () => {
899899
});
900900
});
901901

902-
test('cpu and memory properties are allowed', () => {
902+
test('cpu and memory properties as unit values are allowed', () => {
903903
// GIVEN
904904
const app = new cdk.App();
905905
const stack = new cdk.Stack(app, 'demo-stack');
@@ -925,7 +925,7 @@ test('cpu and memory properties are allowed', () => {
925925
});
926926
});
927927

928-
test('custom cpu and memory units are allowed', () => {
928+
test('cpu and memory properties as numeric values are allowed', () => {
929929
// GIVEN
930930
const app = new cdk.App();
931931
const stack = new cdk.Stack(app, 'demo-stack');
@@ -934,14 +934,14 @@ test('custom cpu and memory units are allowed', () => {
934934
source: apprunner.Source.fromEcrPublic({
935935
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
936936
}),
937-
cpu: apprunner.Cpu.of('Some vCPU'),
938-
memory: apprunner.Memory.of('Some GB'),
937+
cpu: apprunner.Cpu.of('1024'),
938+
memory: apprunner.Memory.of('3072'),
939939
});
940940
// THEN
941941
Template.fromStack(stack).hasResourceProperties('AWS::AppRunner::Service', {
942942
InstanceConfiguration: {
943-
Cpu: 'Some vCPU',
944-
Memory: 'Some GB',
943+
Cpu: '1024',
944+
Memory: '3072',
945945
},
946946
NetworkConfiguration: {
947947
EgressConfiguration: {
@@ -951,6 +951,70 @@ test('custom cpu and memory units are allowed', () => {
951951
});
952952
});
953953

954+
test('invalid cpu property as unit value is not allowed', () => {
955+
// GIVEN
956+
const app = new cdk.App();
957+
const stack = new cdk.Stack(app, 'demo-stack');
958+
// WHEN
959+
expect(() => {
960+
new apprunner.Service(stack, 'DemoService', {
961+
source: apprunner.Source.fromEcrPublic({
962+
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
963+
}),
964+
cpu: apprunner.Cpu.of('1000 vCPU'),
965+
memory: apprunner.Memory.of('3 GB'),
966+
});
967+
}).toThrow('CPU value is invalid');
968+
});
969+
970+
test('invalid cpu property as numeric value is not allowed', () => {
971+
// GIVEN
972+
const app = new cdk.App();
973+
const stack = new cdk.Stack(app, 'demo-stack');
974+
// WHEN
975+
expect(() => {
976+
new apprunner.Service(stack, 'DemoService', {
977+
source: apprunner.Source.fromEcrPublic({
978+
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
979+
}),
980+
cpu: apprunner.Cpu.of('1'),
981+
memory: apprunner.Memory.of('3 GB'),
982+
});
983+
}).toThrow('CPU value is invalid');
984+
});
985+
986+
test('invalid memory property as unit value is not allowed', () => {
987+
// GIVEN
988+
const app = new cdk.App();
989+
const stack = new cdk.Stack(app, 'demo-stack');
990+
// WHEN
991+
expect(() => {
992+
new apprunner.Service(stack, 'DemoService', {
993+
source: apprunner.Source.fromEcrPublic({
994+
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
995+
}),
996+
cpu: apprunner.Cpu.of('1 vCPU'),
997+
memory: apprunner.Memory.of('3000 GB'),
998+
});
999+
}).toThrow('Memory value is invalid');
1000+
});
1001+
1002+
test('invalid memory property as numeric value is not allowed', () => {
1003+
// GIVEN
1004+
const app = new cdk.App();
1005+
const stack = new cdk.Stack(app, 'demo-stack');
1006+
// WHEN
1007+
expect(() => {
1008+
new apprunner.Service(stack, 'DemoService', {
1009+
source: apprunner.Source.fromEcrPublic({
1010+
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
1011+
}),
1012+
cpu: apprunner.Cpu.of('1 vCPU'),
1013+
memory: apprunner.Memory.of('3'),
1014+
});
1015+
}).toThrow('Memory value is invalid');
1016+
});
1017+
9541018
test('environment variable with a prefix of AWSAPPRUNNER should throw an error', () => {
9551019
// GIVEN
9561020
const app = new cdk.App();

0 commit comments

Comments
 (0)