forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-load-balancer.test.ts
138 lines (117 loc) · 3.7 KB
/
http-load-balancer.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import '@aws-cdk/assert-internal/jest';
import * as ecs from '@aws-cdk/aws-ecs';
import * as cdk from '@aws-cdk/core';
import { Container, Environment, HttpLoadBalancerExtension, Service, ServiceDescription } from '../lib';
describe('http load balancer', () => {
test('should be able to add an HTTP load balancer to a service', () => {
// GIVEN
const stack = new cdk.Stack();
// WHEN
const environment = new Environment(stack, 'production');
const serviceDescription = new ServiceDescription();
serviceDescription.add(new Container({
cpu: 256,
memoryMiB: 512,
trafficPort: 80,
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
}));
serviceDescription.add(new HttpLoadBalancerExtension());
new Service(stack, 'my-service', {
environment,
serviceDescription,
});
// THEN
expect(stack).toHaveResource('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Cpu: 256,
Essential: true,
Image: 'nathanpeck/name',
Memory: 512,
Name: 'app',
PortMappings: [
{
ContainerPort: 80,
Protocol: 'tcp',
},
],
Ulimits: [
{
HardLimit: 1024000,
Name: 'nofile',
SoftLimit: 1024000,
},
],
},
],
Cpu: '256',
Family: 'myservicetaskdefinition',
Memory: '512',
NetworkMode: 'awsvpc',
RequiresCompatibilities: [
'EC2',
'FARGATE',
],
TaskRoleArn: {
'Fn::GetAtt': [
'myservicetaskdefinitionTaskRole92ACD903',
'Arn',
],
},
});
expect(stack).toHaveResource('AWS::ElasticLoadBalancingV2::LoadBalancer');
expect(stack).toHaveResource('AWS::ElasticLoadBalancingV2::Listener');
});
test('allows scaling on request count for the HTTP load balancer', () => {
// GIVEN
const stack = new cdk.Stack();
// WHEN
const environment = new Environment(stack, 'production');
const serviceDescription = new ServiceDescription();
serviceDescription.add(new Container({
cpu: 256,
memoryMiB: 512,
trafficPort: 80,
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
}));
serviceDescription.add(new HttpLoadBalancerExtension({ requestsPerTarget: 100 }));
new Service(stack, 'my-service', {
environment,
serviceDescription,
autoScaleTaskCount: {
maxTaskCount: 5,
},
});
// THEN
expect(stack).toHaveResourceLike('AWS::ApplicationAutoScaling::ScalingPolicy', {
PolicyType: 'TargetTrackingScaling',
TargetTrackingScalingPolicyConfiguration: {
PredefinedMetricSpecification: {
PredefinedMetricType: 'ALBRequestCountPerTarget',
},
TargetValue: 100,
},
});
});
test('should error when adding scaling policy if scaling target has not been configured', () => {
// GIVEN
const stack = new cdk.Stack();
// WHEN
const environment = new Environment(stack, 'production');
const serviceDescription = new ServiceDescription();
serviceDescription.add(new Container({
cpu: 256,
memoryMiB: 512,
trafficPort: 80,
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
}));
serviceDescription.add(new HttpLoadBalancerExtension({ requestsPerTarget: 100 }));
// THEN
expect(() => {
new Service(stack, 'my-service', {
environment,
serviceDescription,
});
}).toThrow(/Auto scaling target for the service 'my-service' hasn't been configured. Please use Service construct to configure 'minTaskCount' and 'maxTaskCount'./);
});
});