|
| 1 | +# Challenge Orchestrator |
| 2 | + |
| 3 | +This is the base code for challenge orchestrator which setup two lumbda functions and a state machine on AWS platform using serverless. |
| 4 | + |
| 5 | +## Dependency |
| 6 | + |
| 7 | +- Nodejs v12 |
| 8 | +- serverless |
| 9 | + |
| 10 | +## Environment Variables |
| 11 | + |
| 12 | +| Name | Description | |
| 13 | +| ------------ | ---------------------------------------- | |
| 14 | +| REGION | AWS region | |
| 15 | +| API_ENDPOINT | The endpoint for lambda function to call | |
| 16 | + |
| 17 | +The environment variables are passed using `dotEnv`, so there should be a `.env` file containing the variables in the root folder before deployment. You can find a example of `.env` in the root folder called `example.env`. |
| 18 | + |
| 19 | +## How to deploy |
| 20 | + |
| 21 | +1. Install dependencies |
| 22 | + |
| 23 | + ```bash |
| 24 | + npm install |
| 25 | + ``` |
| 26 | + |
| 27 | +2. Run lint |
| 28 | + |
| 29 | + ```bash |
| 30 | + npm run lint |
| 31 | + npm run lint:fix # with fix |
| 32 | + ``` |
| 33 | + |
| 34 | +3. Configure AWS account with serverless. Refer https://www.serverless.com/framework/docs/providers/aws/guide/credentials/ for details. |
| 35 | + |
| 36 | +4. Deploy |
| 37 | + |
| 38 | + ```bash |
| 39 | + npm run deploy |
| 40 | + ``` |
| 41 | + |
| 42 | +5. Remove resources after varification |
| 43 | + |
| 44 | + ```bash |
| 45 | + npm run destroy |
| 46 | + ``` |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +## Explainations |
| 51 | + |
| 52 | +Currently, two lambda functions and one step function (state machine) is deployed for demostration. |
| 53 | + |
| 54 | +| Name | Type | Handler | |
| 55 | +| ------------- | --------------- | --------------------- | |
| 56 | +| submit | Lambda function | src/handler.submit | |
| 57 | +| startExec | Lambda function | src/handler.startExec | |
| 58 | +| submitMachine | state machine | N/A | |
| 59 | + |
| 60 | +The workflow is: |
| 61 | + |
| 62 | +- user send http request to trigger `startExec` |
| 63 | +- `startExec` starts `submitMachine` |
| 64 | +- `stateMachine` start from state `CallSubmit`, and execute `submit` as a task |
| 65 | +- `stateMachine` stop as `CallSubmit` is the only state it has for now |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +## Verification |
| 70 | + |
| 71 | +1. Deploy the lambda functions and state machine as instructed above. Copying `example.env` to `.env` and keeping the environment variables is recommended. |
| 72 | + |
| 73 | +2. Note that there is an endpoints in the console output for startExec. |
| 74 | + |
| 75 | +3. Make a POST request to the endpoint'. The body of the POST request must have field "input" and "name". "name" should be unique for each execution. For example |
| 76 | + |
| 77 | + ```bash |
| 78 | + curl -X POST 'https://e0gbflxn58.execute-api.us-east-1.amazonaws.com/dev/start' \ |
| 79 | + --header 'Content-Type: application/json' \ |
| 80 | + --data-raw '{ |
| 81 | + "input": { |
| 82 | + "message": "hello", |
| 83 | + "value": 123 |
| 84 | + }, |
| 85 | + "name": "name1" |
| 86 | + }' |
| 87 | + ``` |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + - Check status code 200 and message 'ok' from response |
| 92 | + |
| 93 | + - Go to https://console.aws.amazon.com/cloudwatch/home, click log groups from left menu, check logs from both lambda functions |
| 94 | + |
| 95 | + - Go to https://console.aws.amazon.com/states/home, click the stateMachine and check the executions and logs of it. |
| 96 | + |
| 97 | + - Go to https://webhook.site/#!/de92f4c4-dc9d-4d90-9b00-8bcb350d19e9/9ed801a9-e0e3-4fcf-b868-ef94e089cae0/1, check the request details from `submit` function. The body should be the same as your request. |
| 98 | + |
| 99 | + Note: this url is related to `API_ENDPOINT` variable. Any request sent to this endpoint will be shown on the page. If you change `API_ENDPOINT`, you have to check the request on your own. |
| 100 | + |
| 101 | +4. Don't forget to remove resources after verification : ) |
| 102 | + |
| 103 | +## Local Deploy |
| 104 | + |
| 105 | +### Lambda Functions |
| 106 | + |
| 107 | +Lambda functions can be deployed locally with the help of plugin `serverless-offline`. Just run `sls offline` and you can invoke your lambdas. |
| 108 | + |
| 109 | +You can also configure it to run in docker. Add `--useDocker` after the command. See https://www.npmjs.com/package/serverless-offline for details. |
| 110 | + |
| 111 | +### Step functions |
| 112 | + |
| 113 | +There is a dockerized step function image provided by Amazon, but step functions have to be deployed to it using aws command line tool with template.yaml file and the file can't be generated from serverless.yml file. So another file has to be maintained which is not a good idea. |
| 114 | + |
| 115 | +What's more, lambda functions and step functions use ARNs to interact with each other. I'm not sure if it will work with local-deployed lambda function. |
| 116 | + |
| 117 | +With all the above being said, I will leave this part as TBD. https://docs.aws.amazon.com/step-functions/latest/dg/sfn-local-lambda.html might be a possible solution for your reference. |
0 commit comments