Create app
All next steps can be simplified once we write plugin for NX, but it'll require time, so for now we should be fine executing next steps manually.
Steps to create serverless lambda function ZIP
1) run nx generate @nrwl/nest:app actions/sls/{name-of-app}
2) add .env files with next pattern:
- .env.offline - used for local running
- .env.dev - used for dev environment, these variables should be provided in
CI/CD - .env.prod - production environment, these variables should be provided in
CI/CD - .env.example - values that are expected to be added to get app work
3) Create serverless.yml file
4) Copy serverless template from new-action-helper/zip/serverless.example.yml and adjust it by your needs:
- replace first line
service: {your-app-name}with your app name, by this name you can find your app on AWS - Adjust memorySize and timeout required for your funciton
- Modify environments variables that you need
- If any additional AWS resources are needed for you function, you can specify them in this file. Follow serverless framework docs for more details
- !!Modify
lambdaPort: {last-port-3001}in source file to new port number3001 + 1in your copy use new value <--- how to handle it?
5) For local development, define new queue in infrastructure/local/sqs/custom.conf
- add next fragment:
offline-${your-app-name}-queue {
defaultVisibilityTimeout = 180 seconds
delay = 0 seconds
receiveMessageWait = 0 seconds
deadLettersQueue {
name = "offline-dlq-failed-actions"
maxReceiveCount = 1 // from 1 to 1000
}
fifo = false
contentBasedDeduplication = false
} - replace
${your-app-name}with your app name.
1) modify your project.json:
- copy project.json from
new-action-helper/zip/project.json - replace
${project-path}with your app path -actions/sls/${your-folder-name} - replace
${nx-project-app}with yoyr nx app name -actions-sls-${your-service-name} - replace
${service-name}with your service name - in VS Code use command/ctrl + R
- type ${project-path} in Find input
- type
actions/sls/${your-folder-name}in Replace input - click replace all
2) replace main.ts with main.ts from new-action-helper\zip
3) Delete all //@ts-ignore rows
4) Delete unused folders and files (enviroment, asset, app.service, app.controller)
5) Go to connect app with Lib section
Steps to create serverless lambda function Docker
1) run nx generate @nrwl/nest:app actions/sls/{name-of-app}
2) add .env files with next pattern:
- .env.offline - used for local running
- .env.dev - used for dev environment
- .env.prod - production environment
- .env.example - values that are expected to be added to get app work
3) Create serverless.yml file
4) Copy serverless template from new-action-helper/docker/serverless.yml and adjust it by your needs:
- replace
{project-name}andproject-image-namewith your app name (more than in 2 places use search for this purposes), by this name you can find your app on AWS andproject-image-nameis your repository that you need to create in next step - Adjust memorySize and timeout required for your funciton
- Modify environments that you needed
- If any additional AWS resources are needed for you
5) describe ECR in infrastructure/terrafrom/ecr with project-image-name
6) modify your project.json:
- copy project.json from
new-action-helper/docker/project.json - replace
${project-name}and${project-image-path}with your app name - Check
servetarget, it may require additional modification.
7) replace main.ts with main.ts from new-action-helper\zip
8) Add your Dockerfile in most cases it would be different file so, just inspect already existed Dockerfiles and modify them by your needs. AWS Docker Lambda
9) Copy custom-webpack.config.js and replace ${project-name} to your app name. This file allows you to include in dist folder additional files
10) if your image require entry_script for aws lambda (in case you don't use aws lambda prepared images) copy it as well and modify your Dockerfile and custom-webpack.config.js
11) Add service to the docker-compose in order to run it locally:
playwright-fargate:
# platform: linux/arm64
build:
context: ./dist/${path-to-your-built-app}
dockerfile: Dockerfile
container_name: ${app-name}
volumes:
- "./dist/${path-to-your-index}/index.js:/fargate/index.js"
- "./node_modules:/${path-to-your-app-in-docker}/node_modules"
env_file:
- ./apps/${path-to-your}/.env.local
ports:
- "${port}:8080"
depends_on:
- sqs
Steps to create fargate app
1) run nx generate @nrwl/nest:app actions/fargate/{name-of-app}
2) Write required infra for your app in infrastructure/terrafrom folder. Terraform For running ECS service: create VPC, IAM, ECS, SQS
for autoscaling: CloudWatch, Autoscaling
3) Write Dockerfile
4) Write your main.ts so that it listens to the SQS, parse data and pass it to SQSHandler service to process action
5) Change your project.json so that it can run watcher for your code changes
6) Add your service to docker-compose (See point 10 of the previous section)
Connect app with lib
In your app module
1) Import ExecutorModule from @actions-executor/action-base and register your created mapping from the create-lib tutorial
2) Import SqsQueueModule form @actions-executor/queues/sqs-queue
Example:
import { Module } from '@nestjs/common';
import { ExecutorModule } from '@actions-executor/action-base';
import { SqsQueueModule } from '@actions-executor/queues/sqs-queue';
import { LoggerModule } from '@actions-executor/utils/logger';
import { PlaywrightActionMapping } from '@actions-executor/actions/browser-automation/playwright';
@Module({
imports: [
ExecutorModule.register(PlaywrightActionMapping),
SqsQueueModule,
LoggerModule,
],
})
export class AppModule {}
Connect Action Manager (sh_project) and Action Executor (sh_actions)
After you created lib and connect lib to app, you need to register your app in sh_project. 1) Go to action service
2) Open src/modules/master/actions/groups/types/actions.ts
4) Add new ActionType similar to how you did when create lib.
1) Add new ActionType
```ts
YOUR_NEW_ACTION_TYPE = 'new_action_type'
```
5) Add new group to your ActionGroups
export enum ActionGroups {
COMMON = 'common',
...
YOUR_SERVICE_NAME = 'your_service_name'
}
6) Modify ACTION_GROUPS to map your action to created group ib both sh_project and sh_actions (libs/queues/sqs-queue/src/lib/constants.ts)
export const ACTION_GROUPS = {
...
[ActionType.YOUR_NEW_ACTION_TYPE]: ActionGroups.YOUR_SERVICE_NAME,
}
7) Update SQS_QUEUE_URL in sh_actions (libs/queues/sqs-queue/src/lib/constants.ts)
Update run-helper.sh
Include Description on how to run your app