|
| 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! |
0 commit comments