Skip to content

Commit

Permalink
add worker lambda template that's deployed by AWS CDK
Browse files Browse the repository at this point in the history
  • Loading branch information
damienung1 committed Apr 1, 2021
1 parent 612c04c commit dcb1be1
Show file tree
Hide file tree
Showing 19 changed files with 1,248 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ This initialises a new directory and Git repository.
a message queue is employed between the source topic and the Lambda function,
and unprocessed events are sent to a dead-letter queue for manual triage.

- `lambda-sqs-worker-cdk`

An asynchronous [worker] built on [AWS Lambda] and deployed with [AWS CDK].

SNS -> SQS (with a dead-letter queue) -> Lambda

Comes with configuration validation and infrastructure snapshot testing.

- `oss-npm-package`

A public npm package published via [semantic-release] pipeline.
Expand Down Expand Up @@ -198,6 +206,8 @@ This initialises a new directory and Git repository.
[worker]: https://tech-strategy.ssod.skinfra.xyz/docs/v1/components.html#worker
[express]: https://expressjs.com/

[AWS CDK]: [Serverless](https://tech-strategy.ssod.skinfra.xyz/docs/v1/technology.html#cdk)

This script is interactive by default.
For unattended execution, pipe in JSON:

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"runtypes": "^5.0.1",
"semantic-release": "^17.3.8",
"serialize-error": "^8.0.1",
"source-map-support": "^0.5.19",
"ts-jest": "^26.4.4",
"ts-node": "^9.1.1",
"ts-node-dev": "1.1.6",
Expand Down
3 changes: 2 additions & 1 deletion template/base/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"paths": {
"src": ["src"]
},
"target": "es2020"
"target": "es2020",
"resolveJsonModule": true
},
"exclude": ["lib*/**/*"],
"extends": "skuba/config/tsconfig.json"
Expand Down
61 changes: 61 additions & 0 deletions template/lambda-sqs-worker-cdk/.buildkite/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
dev-agent: &dev-agent
agents:
queue: <%- devBuildkiteQueueName %>

prod-agent: &prod-agent
agents:
queue: <%- prodBuildkiteQueueName %>

plugins: &plugins #alias for shared plugins
seek-oss/aws-sm#v2.3.1:
env:
NPM_TOKEN: 'arn:aws:secretsmanager:ap-southeast-2:987872074697:secret:npm/npm-read-token'
docker#v3.8.0:
volumes:
- /app/node_modules
workdir: '/app'
environment:
- NPM_TOKEN
seek-oss/docker-ecr-cache#v1.9.0:
dockerfile: builder.dockerfile
build-args:
- NPM_TOKEN
cache-on:
- yarn.lock

steps:
- label: ':yarn: :eslint: Lint and :jest: unit test'
<<: *dev-agent
plugins:
<<: *plugins
key: test
command:
- echo "--- Running yarn lint"
- yarn lint
- echo "--- Running yarn test :jest:"
- yarn test

- label: 'CDK Deploy Staging :shipit:'
<<: *dev-agent
plugins:
<<: *plugins
depends_on:
- test
command:
- echo "--- Running CDK deploy to staging"
- yarn deploy:dev
concurrency: 1
concurrency_group: '<%- repoName %>/deploy/dev'

- label: 'CDK Deploy Production :shipit:'
branches: 'master'
<<: *prod-agent
plugins:
<<: *plugins
depends_on:
- test
command:
- echo "--- Running CDK deploy to production"
- yarn deploy:prod
concurrency: 1
concurrency_group: '<%- repoName %>/deploy/prod'
29 changes: 29 additions & 0 deletions template/lambda-sqs-worker-cdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# managed by skuba
.idea/
.serverless/
.vscode/
node_modules*/

/coverage*/
/dist*/
/lib*/
/tmp*/

.DS_Store
.npmrc
*.tgz
*.tsbuildinfo
npm-debug.log
package-lock.json
yarn-error.log
# end managed by skuba

*/*.js
!jest.config.js
*.d.ts

# CDK asset staging directory
.cdk.staging
cdk.out

# esbuild
22 changes: 22 additions & 0 deletions template/lambda-sqs-worker-cdk/.me
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
# https://codex.ssod.skinfra.xyz/docs

components:
'<%- repoName %>':
# TODO: supply system catalog information
# dependencies:
# - type: api
# key: SEEK-Jobs/???
# - type: datastore
# arn: arn:aws:dynamodb:us-east-1:123456789012:table/???
# - type: datastore
# key: infrastructure/???
deploy_target: Worker
# is_production_system: true
primary_technologies:
- AWS Lambda
- Buildkite
- AWS CDK
- skuba
- TypeScript
# scope: APAC
1 change: 1 addition & 0 deletions template/lambda-sqs-worker-cdk/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14.16.0
12 changes: 12 additions & 0 deletions template/lambda-sqs-worker-cdk/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require('esbuild')
.build({
entryPoints: ['./src/app.ts'],
bundle: true,
minify: true,
platform: 'node',
target: ['node14.16.0'],
outdir: './dist',
})
.catch((e) => {
throw new Error(e);
});
13 changes: 13 additions & 0 deletions template/lambda-sqs-worker-cdk/builder.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:14.16.0-alpine3.11 as builder
ARG NPM_TOKEN
WORKDIR /app
COPY ./ /app
RUN echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > .npmrc
RUN yarn install --frozen-lockfile --non-interactive
RUN rm -f .npmrc

FROM node:14.16.0-alpine3.11
RUN apk update && apk upgrade && \
apk add --no-cache bash git
WORKDIR /app
COPY --from=builder /app .
53 changes: 53 additions & 0 deletions template/lambda-sqs-worker-cdk/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"app": "npx ts-node infra/index.ts",
"context": {
"@aws-cdk/core:enableStackNameDuplicates": "true",
"global": {
"appName": "<%- serviceName %>"
},
"dev": {
"workerLambda": {
"name": "<%- serviceName %>",
"environment": {
"SOMETHING": "dev"
}
},
"queue": {
"name": "<%- serviceName %>",
"deadLetterQueue": {
"name": "<%- serviceName %>-dlq",
"maxReceiveCount": 3
}
},
"topic": {
"name": "<%- serviceName %>"
},
"kmsKey": {
"description": "<%- serviceName %>",
"alias": "seek/self/<%- serviceName %>"
}
},
"prod": {
"workerLambda": {
"name": "<%- serviceName %>",
"environment": {
"SOMETHING": "prod"
}
},
"queue": {
"name": "<%- serviceName %>",
"deadLetterQueue": {
"name": "<%- serviceName %>-dlq",
"maxReceiveCount": 3
}
},
"topic": {
"name": "<%- serviceName %>"
},
"kmsKey": {
"description": "<%- serviceName %>",
"alias": "seek/self/<%- serviceName %>"
}
}
}
}
Loading

0 comments on commit dcb1be1

Please sign in to comment.