Skip to content

Commit 88e4f1e

Browse files
jeanschmidtzxiiro
andauthored
Adding documentation to help develop ALI lambdas and some useful scripts (#6256)
Added a README.md on `terraform-aws-github-runner/modules/runners/lambdas/runners/README.md` with instruction on how to develop and troubleshoot lambdas development. --------- Co-authored-by: Thanh Ha <[email protected]>
1 parent 4e1f892 commit 88e4f1e

File tree

3 files changed

+266
-0
lines changed

3 files changed

+266
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# Runners Autoscaler Lambda Infrastucture - Typescript code
2+
3+
This folder contains the typescript code for the scaleUp and scaleDown lambdas
4+
5+
## Local Development
6+
7+
### Requirements
8+
9+
Node, pip, yarn, cmake
10+
11+
### Development main workflow
12+
13+
Most of the development is done using `yarn`, in order to setup the project, run:
14+
15+
```
16+
$ yarn install
17+
```
18+
19+
Next, you can run unit test by:
20+
21+
```
22+
$ yarn test
23+
```
24+
25+
after completing your changes, please run:
26+
27+
```
28+
$ yarn commit-check
29+
```
30+
31+
This should lint the code, format it, make sure it is building properly and run tests. If your command succeeds, it will be green on CI.
32+
33+
### Expectations
34+
35+
It is **required** to submit code:
36+
37+
* All unit tests are passing;
38+
* At least 80% unit test coverage;
39+
* No linting warnings are being thrown;
40+
* The code must be fully compilable to javascript;
41+
* Code formatting are according to current standards (prettier);
42+
43+
It is *advisable* to strive to when submitting code:
44+
45+
* Improve as much as possible unit test code coverage;
46+
47+
### Yarn commands
48+
49+
| Command | What do they do? |
50+
| -------------- | ------------------ |
51+
| test | Run unit tests |
52+
| lint | Run linting |
53+
| build | compiles typescript to javascript |
54+
| dist | build + create lambda zip `runners.zip` |
55+
| format | run prettier so code follows layout standard |
56+
| commit-check | format + lint + test + build |
57+
58+
### Makefile helpers
59+
60+
Those are primarly used for CI, but, it might be useful to understand, there are 3 commands:
61+
62+
| Command | Yarn Equivalents |
63+
| ------------ | ------------------ |
64+
| clean | - just clean temp/build files |
65+
| build | install + lint + format-check + build + test |
66+
| dist | install + dist |
67+
68+
### Troubleshoot/debug
69+
70+
Most of the code, to run properly, expects to connect to external services and have a series of environment setup. It is not really possible to simply run the code localy without mocking aggressively. If you can't easily troubleshoot or implement your changes relying on unit test (rare cases) it is possible to run your code in AWS EC2.
71+
72+
**WARNING: In practice, even with canary, we only have production environment, be aware that you can break things when running tests!**
73+
74+
So, it is not really recommended to do so, unless troubleshooting something that you have limited understanding and can't replicate locally.
75+
76+
#### Requirements needed:
77+
78+
* Admin access to the exact environment where lambdas run;
79+
* Access to all relevant secrets for production;
80+
* Create an EC2 instance (more details below);
81+
82+
#### Setup the test environment
83+
84+
Names of roles and details of the secrets are dependent if you are testing scaleDown or scaleUp lambdas. Please update commands below accordingly.
85+
86+
* Add the `AmazonSSMManagedEC2IntanceDefaultPolicy` and `AmazonSSMManagedInstanceCore` policy to `gh-ci-action-scale-down-lambda-role`:
87+
88+
```
89+
local$ aws attach-role-policy --role-name gh-ci-action-scale-down-lambda-role --policy-arn arn:aws:iam::aws:policy/service-role/AmazonSSMManagedEC2IntanceDefaultPolicy
90+
local$ aws attach-role-policy --role-name gh-ci-action-scale-down-lambda-role --policy-arn arn:aws:iam::aws:policy/service-role/AmazonSSMManagedInstanceCore
91+
```
92+
93+
* Create an instance profile for this role (if it does not exists already):
94+
95+
```
96+
local$ aws iam create-instance-profile --instance-profile-name gh-ci-action-scale-down-lambda-profile
97+
```
98+
99+
* Assign the lambda role to the instance profile:
100+
101+
```
102+
local$ aws iam add-role-to-instance-profile --role-name gh-ci-action-scale-down-lambda-role --instance-profile-name gh-ci-action-scale-down-lambda-profile
103+
```
104+
105+
* Go to web console (easier IMO) and update the trust relationships for the given role so EC2 can assume it:
106+
107+
```
108+
{
109+
"Version": "2012-10-17",
110+
"Statement": [
111+
{
112+
"Effect": "Allow",
113+
"Principal": {
114+
"Service": [
115+
"lambda.amazonaws.com",
116+
"ec2.amazonaws.com"
117+
]
118+
},
119+
"Action": "sts:AssumeRole"
120+
}
121+
]
122+
}
123+
```
124+
125+
* Create a AWS instance, please attach more disk space so you can troubleshoot and run things worry free.
126+
* Important to select the **SAME** vpc that your lambda is using: `$ aws lambda get-function-configuration --function-name gh-ci-scale-down --query 'VpcConfig.VpcId' --output text`;
127+
* Important to select the role/role-profile you created during creation;
128+
* Use AMZN Linux 2023, latest version;
129+
* Select your ssh keys;
130+
131+
* You should't be able to SSH to it directly, so it is recommended to use SSM to connect. In order of making things easier use the `aws-ssh-session` hacky script available on `terraform-aws-github-runner/tools/aws-ssh-session` of this repository. This script should create a ssh port-forwarding so you can both ssh to it AND scp:
132+
133+
```
134+
local$ aws-ssh-session <instance-id> ec2-user us-east-1
135+
```
136+
137+
* Install node:
138+
139+
```
140+
remote$ sudo yum install nodejs
141+
```
142+
143+
* scp your already built `index.js`:
144+
145+
```
146+
local$ scp -C -P 5113 dist/index.js [email protected]:/home/ec2-user/.
147+
```
148+
149+
* You can use the script `run-aws-lambda-helper` (terraform-aws-github-runner/tools/run-aws-lambda-helper) from your laptop to create a script export all relevant environment variables and call your lambda:
150+
151+
```
152+
local$ run-aws-lambda-helper gh-ci-scale-down us-east-1 >run-lambda.sh
153+
local$ scp -C -P 5113 run-lambda.sh [email protected]:/home/ec2-user/.
154+
remote$ bash run-lambda.sh
155+
```
156+
157+
* If you prefer to do things manually:
158+
* Just export all environment variables as the lambda function, adding `FUNCTION_NAME`, `AWS_REGION` and `AWS_DEFAULT_REGION`;
159+
* Run your lambda with: `node -e 'require("./index").scaleDown({}, {}, {});'`
160+
161+
**IMPORTANT WARNINGS**
162+
163+
* Those environment variables are SECRETS, be very careful not to expose them;
164+
* The environment variables **MUST BE UP TO DATE**, some variables changes during each deployment, and mistmatching them can potentially cause runner disruptions!
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
3+
INSTANCE=$1
4+
LOGIN_USR=$2
5+
REGION=$3
6+
7+
SSM_SESSION_PID=""
8+
TEMP_FILE=$(mktemp)
9+
10+
trap on_exit EXIT
11+
12+
function on_exit {
13+
if [ ! -z "$SSM_SESSION_PID" ] ; then
14+
echo "Terminating session PID $SSM_SESSION_PID"
15+
kill -s SIGINT $SSM_SESSION_PID
16+
sleep 3
17+
kill -s SIGKILL $SSM_SESSION_PID
18+
fi
19+
SESSION_ID=$(cat $TEMP_FILE | grep 'Starting session with SessionId: ' | cut -d ':' -f 2 | xargs)
20+
if [ ! -z "$SESSION_ID" ] ; then
21+
echo "Terminating session $SESSION_ID"
22+
aws ssm terminate-session --session-id $SESSION_ID
23+
fi
24+
rm -rf "$TEMP_FILE"
25+
}
26+
27+
if [ -z "$REGION" ] ; then
28+
REGION="$AWS_DEFAULT_REGION"
29+
fi
30+
31+
if [ -z "$REGION" ] ; then
32+
echo "AWS_DEFAULT_REGION is not defined, you need to provide region argument"
33+
exit 1
34+
fi
35+
36+
if [ -z "$LOGIN_USR" ] ; then
37+
LOGIN_USR=ec2-user
38+
fi
39+
40+
if [ -z "$INSTANCE" ] ; then
41+
echo "usage $0 <instance-id> [login] [region]"
42+
exit 1
43+
fi
44+
45+
SESSION_MANAGER_PLUGIN_PID=$(ps | grep -v grep | grep session-manager-plugin | xargs | cut -d ' ' -f 1)
46+
47+
if [ ! -z "$SESSION_MANAGER_PLUGIN_PID" ] ; then
48+
kill $SESSION_MANAGER_PLUGIN_PID
49+
fi
50+
51+
aws ssm start-session \
52+
--target $INSTANCE \
53+
--region $REGION \
54+
--document-name AWS-StartPortForwardingSession \
55+
--parameters '{"portNumber":["22"], "localPortNumber":["5113"]}' >$TEMP_FILE &
56+
57+
SSM_SESSION_PID=$!
58+
59+
if ps -p $SSM_SESSION_PID > /dev/null ; then
60+
while true ; do
61+
if grep -Fxq "Waiting for connections..." "$TEMP_FILE" >/dev/null ; then
62+
break
63+
fi
64+
sleep 1
65+
done
66+
sleep 1
67+
echo "scp -C -P 5113 ./local-file $LOGIN_USR@127.0.0.1:/remote/location"
68+
ssh -i ~/.ssh/pet-instances-skeleton-key-v2 -p 5113 $LOGIN_USR@127.0.0.1
69+
else
70+
echo "humm, seems that aws ssm start-session failed :("
71+
exit 1
72+
fi
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
FUNCTION_NAME="$1"
4+
AWS_REGION="$2"
5+
6+
if [ -z "$FUNCTION_NAME" ] || [ -z "$AWS_REGION" ]; then
7+
echo "Usage: $0 <LAMBDA_FUNCTION_NAME> <AWS_REGION>"
8+
exit 1
9+
fi
10+
11+
echo "#!/bin/bash"
12+
echo ""
13+
14+
# Fetch the environment variables for the given Lambda function.
15+
# Then use jq to transform them into export statements.
16+
aws lambda get-function-configuration \
17+
--region "$AWS_REGION" \
18+
--function-name "$FUNCTION_NAME" \
19+
--query 'Environment.Variables' \
20+
--output json \
21+
2>/dev/null \
22+
| jq -r 'to_entries | map("export \(.key)=\(.value|@sh)") | .[]'
23+
24+
echo "export FUNCTION_NAME=$FUNCTION_NAME"
25+
echo "export AWS_REGION=$AWS_REGION"
26+
echo "export AWS_DEFAULT_REGION=$AWS_REGION"
27+
28+
echo ""
29+
30+
echo "node -e 'require(\"./index\").scaleDown({}, {}, {});'"

0 commit comments

Comments
 (0)