Skip to content

Commit

Permalink
Add ECS task definition, plus script for port forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Hobson committed Sep 2, 2021
1 parent 9d076a2 commit 4cda7cc
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
72 changes: 72 additions & 0 deletions ecs/milmove_load_testing_ecs.json.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"containerDefinitions": [
{
"command": [
"-f",
"/app/locustfiles/prime.py",
"--host",
"exp",
"--web-port",
"4000"
],
"essential": true,
"image": "__ACCOUNTID__.dkr.ecr.__REGION__.amazonaws.com/loadtesting:exp",
"readonlyRootFilesystem": false,
"linuxParameters": {
"initProcessEnabled": true
},
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group" : "/ecs/loadtesting/exp-web",
"awslogs-region": "__REGION__",
"awslogs-stream-prefix": "exp-web"
}
},
"name": "exp-web-loadtesting",
"portMappings": [
{
"containerPort": 4000,
"hostPort": 4000,
"protocol": "tcp"
}
],
"environment": [
{
"name": "DEVLOCAL_AUTH",
"value": "true"
},
{
"name": "STATIC_TLS_FILE_PATH",
"value": "/tmp"
}
],
"secrets": [
{
"name": "MOVE_MIL_EXP_TLS_CERT",
"valueFrom": "arn:__PARTITION__:ssm:__REGION__:__ACCOUNTID__:parameter/load-testing/exp-tls-cert"
},
{
"name": "MOVE_MIL_EXP_TLS_KEY",
"valueFrom": "arn:__PARTITION__:ssm:__REGION__:__ACCOUNTID__:parameter/load-testing/exp-tls-key"
}
],
"ulimits": [
{
"name":"nofile",
"softLimit": 10000,
"hardLimit": 10000
}
]
}
],
"cpu": "256",
"taskRoleArn": "arn:__PARTITION__:iam::__ACCOUNTID__:role/ecs-task-role-exp-web-loadtesting",
"executionRoleArn": "arn:__PARTITION__:iam::__ACCOUNTID__:role/ecs-task-execution-role-exp-web-loadtesting",
"family": "exp-web-loadtesting",
"memory": "512",
"networkMode": "awsvpc",
"requiresCompatibilities": [
"FARGATE"
]
}
77 changes: 77 additions & 0 deletions scripts/aws-session-port-forward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import os
import subprocess
import sys

cluster = "loadtesting"
port_number = "4000"

result = subprocess.run(["aws", "ecs", "list-tasks", "--cluster", cluster], stdout=subprocess.PIPE)

tasks = json.loads(result.stdout)
if "taskArns" not in tasks or len(tasks["taskArns"]) == 0:
print("Cannot find any running tasks in cluster: {}", cluster, file=sys.stderr)
sys.exit(1)

if len(tasks["taskArns"]) != 1:
print("More than one task running in cluster: {}", cluster, file=sys.stderr)
sys.exit(1)

task_arn = tasks["taskArns"][0]

result = subprocess.run(
["aws", "ecs", "describe-tasks", "--cluster", cluster, "--tasks", task_arn], stdout=subprocess.PIPE
)

try:
task_details = json.loads(result.stdout)
except Exception as e:
print("Cannot parse describe tasks for taskArn: {}", task_arn, file=sys.stderr)
print(sys.exc_info()[0], file=sys.stderr)
print(e, file=sys.stderr)
sys.exit(1)

if "tasks" not in task_details or len(task_details["tasks"]) == 0:
print("Cannot find task detail for taskArn: {}", task_arn, file=sys.stderr)
sys.exit(1)

if len(task_details["tasks"]) != 1:
print("More than one task detail for taskArn: {}", task_arn, file=sys.stderr)
sys.exit(1)

task = task_details["tasks"][0]

if "containers" not in task or len(task["containers"]) == 0:
print("Cannot find containers for taskArn: {}", task_arn, file=sys.stderr)
sys.exit(1)

if len(task["containers"]) != 1:
print("More than one container for taskArn: {}", task_arn, file=sys.stderr)
sys.exit(1)

container = task["containers"][0]

task_id = task_arn.split("/")[-1]
container_id = container["runtimeId"]

target = "ecs:{cluster}_{task_id}_{container_id}".format(cluster=cluster, task_id=task_id, container_id=container_id)

parameters = json.dumps({"portNumber": [port_number], "localPortNumber": [port_number]})

print("Hit 'Control-C' to exit")
# replace this process with the session
os.execlp(
"aws",
"aws",
"ssm",
"start-session",
"--target",
target,
"--document-name",
"AWS-StartPortForwardingSession",
"--parameters",
parameters,
)

0 comments on commit 4cda7cc

Please sign in to comment.