Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Commit 1dc0035

Browse files
committed
Updated runner.py to read list of regions from environment variables
1 parent 660f98d commit 1dc0035

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

bin/runner.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,41 @@
1414
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1515
import boto3
1616
import uuid
17+
import os
1718

1819

1920
# Will be passed as environment variable to the Fargate docker containers.
2021
# Useful with the Elasticsearch example as the test suite will read the cluster URL from this variable.
2122
ENDPOINT_UNDER_TEST = 'http://strawberry.banana.com'
23+
TASK_COUNT = int(os.getenv('TASK_COUNT', 3))
2224

23-
# List of regions where we have deployed the CloudFormation stack
24-
regions = [
25-
#{
26-
# 'name': 'us-east-1',
27-
# 'stackName': 'dlt-fargate',
28-
# 'taskCount': 3
29-
#},
30-
#{
31-
# 'name': 'us-east-2',
32-
# 'stackName': 'dlt-fargate',
33-
# 'taskCount': 3
34-
#},
35-
{
36-
'name': 'us-west-2',
37-
'stackName': 'dlt-fargate',
38-
'taskCount': 3
39-
}
40-
]
25+
def get_regions_from_environment_variables():
26+
regions = []
27+
region_count = 1
28+
next_region = os.environ.get('REGION_' + str(region_count))
29+
while next_region:
30+
regions.append({
31+
'name': next_region,
32+
'stackName': os.environ.get('REGION_' + str(region_count) + '_STACK_NAME')
33+
})
34+
region_count += 1
35+
next_region = os.environ.get('REGION_' + str(region_count))
36+
37+
return regions
4138

4239

4340
def start_distributed_load_test():
4441
run_id = str(uuid.uuid4())
4542
print('Started new load test with runId = {}'.format(run_id))
4643

44+
print('Getting list of regions from environment variables')
45+
regions = get_regions_from_environment_variables()
46+
4747
for region in regions:
4848

49+
if not region['stackName']:
50+
continue
51+
4952
cloud_formation = boto3.client('cloudformation', region_name=region['name'])
5053
ecs = boto3.client('ecs', region_name=region['name'])
5154

@@ -70,7 +73,7 @@ def start_distributed_load_test():
7073
response = ecs.run_task(
7174
cluster=stack_outputs['FargateClusterName'],
7275
taskDefinition=stack_outputs['TaskDefinitionArn'],
73-
count=region['taskCount'],
76+
count=TASK_COUNT,
7477
startedBy=run_id,
7578
group=run_id,
7679
launchType='FARGATE',

cloudformation/master.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Resources:
6969
TemplateURL: "https://s3.amazonaws.com/load-testing-using-aws-fargate/artifacts/templates/pipeline.yaml"
7070
Parameters:
7171
DockerRegistryArn: !GetAtt DockerRegistry.Arn
72+
MasterStackId: !Ref AWS::StackId
7273
GitRepositoryName: !Ref GitRepositoryName
7374
DockerRegistryURL: !Join
7475
- ''

cloudformation/pipeline.yaml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Parameters:
2727
ContainersPerRegion:
2828
Type: Number
2929
Default: 3
30+
MasterStackId:
31+
Type: String
3032

3133
Resources:
3234
CodeRepository:
@@ -41,6 +43,8 @@ Resources:
4143
VersioningConfiguration:
4244
Status: Enabled
4345

46+
# Builds, tags and pushes the docker image
47+
# into the ECR Registry.
4448
CodeBuild:
4549
Type: AWS::CodeBuild::Project
4650
Properties:
@@ -77,20 +81,30 @@ Resources:
7781
- docker push $DOCKER_REGISTRY_URL:latest
7882
- docker push $DOCKER_REGISTRY_URL:$CODEBUILD_RESOLVED_SOURCE_VERSION
7983
80-
# Leverage CodeBuild to kick start the test suite.
81-
# It could very well be replaced with a Lambda function
84+
# CodeBuild project that runs the runner.py python script
85+
# to start the load tests. The script will schedule the
86+
# execution of Fargate tasks into the different regions.
8287
LoadTestRunner:
8388
Type: AWS::CodeBuild::Project
8489
Properties:
8590
Description: Runs the load tests in Fargate
86-
TimeoutInMinutes: 240 # 2 hours
8791
ServiceRole: !GetAtt CodeBuildRole.Arn
8892
Artifacts:
8993
Type: CODEPIPELINE
9094
Environment:
9195
Type: LINUX_CONTAINER
9296
ComputeType: BUILD_GENERAL1_SMALL
9397
Image: "aws/codebuild/python:3.6.5"
98+
EnvironmentVariables:
99+
-
100+
Name: TASK_COUNT
101+
Value: !Ref ContainersPerRegion
102+
-
103+
Name: REGION_1
104+
Value: !Ref AWS::Region
105+
-
106+
Name: REGION_1_STACK_NAME
107+
Value: !Ref MasterStackId
94108
Source:
95109
Type: CODEPIPELINE
96110
BuildSpec: !Sub |

0 commit comments

Comments
 (0)