Skip to content

Commit cce7c3e

Browse files
authored
Merge pull request #1 from failsafe-engineering/feature/implement-dynamic-trigger
Major: Implementing basic version
2 parents 83730f7 + 9eb6259 commit cce7c3e

File tree

9 files changed

+20490
-1
lines changed

9 files changed

+20490
-1
lines changed

.github/workflows/cd.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: CD
2+
on:
3+
release:
4+
types: [published]
5+
workflow_dispatch:
6+
inputs:
7+
debug_enabled:
8+
description: 'Type true if you want to debug the jobs.'
9+
required: false
10+
default: false
11+
jobs:
12+
build_and_test:
13+
name: Build and test
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
node-version: [14.x, 16.x]
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v3
21+
- name: Setup node
22+
uses: actions/setup-node@v3
23+
with:
24+
node-version: ${{ matrix.node-version }}
25+
registry-url: "https://registry.npmjs.org"
26+
- name: Setup tmate session
27+
uses: mxschmitt/action-tmate@v3
28+
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled }}
29+
- name: Install dependencies
30+
run: npm ci
31+
- name: Lint code
32+
run: npm run lint
33+
- name: Run tests
34+
run: npm test
35+
publish_to_npm:
36+
name: Publish to npm
37+
needs: build_and_test
38+
runs-on: ubuntu-latest
39+
steps:
40+
- name: Checkout
41+
uses: actions/checkout@v3
42+
- name: Setup node
43+
uses: actions/setup-node@v3
44+
with:
45+
node-version: 16
46+
registry-url: https://registry.npmjs.org/
47+
- name: Setup tmate session
48+
uses: mxschmitt/action-tmate@v3
49+
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled }}
50+
- run: npm ci
51+
- run: npm publish --access public
52+
env:
53+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

.github/workflows/ci.yml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: CI
2+
on:
3+
pull_request:
4+
types: [opened, synchronize, closed]
5+
workflow_dispatch:
6+
inputs:
7+
debug_enabled:
8+
description: 'Type true if you want to debug the jobs.'
9+
required: false
10+
default: false
11+
jobs:
12+
build_and_test:
13+
name: Build and test
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
node-version: [14.x, 16.x]
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v3
21+
- name: Setup node
22+
uses: actions/setup-node@v3
23+
with:
24+
node-version: ${{ matrix.node-version }}
25+
registry-url: "https://registry.npmjs.org"
26+
- name: Setup tmate session
27+
uses: mxschmitt/action-tmate@v3
28+
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled }}
29+
- name: Install dependencies
30+
run: npm ci
31+
- name: Lint code
32+
run: npm run lint
33+
- name: Run tests
34+
run: npm test
35+
bump_version:
36+
name: Bump version
37+
if: ${{ (github.event.pull_request.state == 'closed' && github.event.pull_request.merged == true) || (github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled) }}
38+
runs-on: ubuntu-latest
39+
needs: build_and_test
40+
env:
41+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42+
steps:
43+
- name: Checkout
44+
uses: actions/checkout@v3
45+
- name: Setup node
46+
uses: actions/setup-node@v3
47+
with:
48+
node-version: "16.x"
49+
registry-url: "https://registry.npmjs.org"
50+
- name: Setup tmate session
51+
uses: mxschmitt/action-tmate@v3
52+
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled }}
53+
- name: Configure Git
54+
run: |
55+
git config --local user.email "[email protected]"
56+
git config --local user.name "GitHub Action"
57+
- name: Set increment patch
58+
if: contains(github.event.pull_request.title, 'patch')
59+
run: npm version patch
60+
- name: Set increment minor
61+
if: contains(github.event.pull_request.title, 'minor')
62+
run: npm version minor
63+
- name: Set increment major
64+
if: contains(github.event.pull_request.title, 'major')
65+
run: npm version major
66+
- name: Commit
67+
if: contains(github.event.pull_request.title, 'patch') || contains(github.event.pull_request.title, 'minor') || contains(github.event.pull_request.title, 'major')
68+
env:
69+
RELEASE_MESSAGE: ${{ github.event.pull_request.title }}
70+
run: |
71+
git push "https://$GITHUB_ACTOR:[email protected]/$GITHUB_REPOSITORY.git" HEAD:$GITHUB_REF --follow-tags;
72+
LATEST_TAG=$(git tag | grep -E '^v[0-9]' | sort -V | tail -1);
73+
curl --request POST \
74+
--url https://api.github.com/repos/$GITHUB_REPOSITORY/releases \
75+
--header "Authorization: token $GITHUB_TOKEN" \
76+
--header "content-type: application/json" \
77+
--data '{
78+
"tag_name": "'"$LATEST_TAG"'",
79+
"name": "'"$LATEST_TAG"'",
80+
"body": "'"$RELEASE_MESSAGE"'",
81+
"draft": true
82+
}'

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
lib
3+
coverage
4+
.DS_Store

README.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,106 @@
1-
# serverless-aws-lambda-dynamic-trigger
1+
## Modules
2+
3+
<dl>
4+
<dt><a href="#index.module_js">js</a></dt>
5+
<dd><p>The plugin can register triggers (events) for a lambda function dynamically. At deployment time</p>
6+
<ol>
7+
<li>It fetches the value of a parameter in the Parameters. The value must be a list ARNs sepearted by comma.</li>
8+
<li>Parses the individual ARNs.</li>
9+
<li>Register the ARNs as triggers with the configured lambda function or functions.</li>
10+
</ol>
11+
<p>Please note that currently you can only use the plugin with sns, sqs or kinesis triggers (events).</p>
12+
<p>The original idea is to make the same lambda function triggered by different events on different environments (stages).
13+
Like on <em>dev</em> foo lambda function is triggered by</p>
14+
<ul>
15+
<li>arn:aws:sns:eu-west-2:123456654321:topic1</li>
16+
<li>arn:aws:sns:eu-west-2:123456654321:topic2</li>
17+
<li>arn:aws:sns:eu-west-2:123456654321:topic3
18+
while on <em>prod</em> foo lambda function is triggered by</li>
19+
<li>arn:aws:sns:eu-west-2:123456654321:topic1</li>
20+
<li>arn:aws:sns:eu-west-2:123456654321:topic2
21+
This way we can switch features on and off on different stages.</li>
22+
</ul>
23+
<p>The dynamic trigger sets needs to ne stored in the Parameter Store of the Systems Manager (SSM) and it should look somewhat like this:
24+
Name: /dev/dynamic-trigger
25+
Value: arn:aws:sns:eu-west-2:123456654321:topic1,arn:aws:sns:eu-west-2:123456654321:topic2,arn:aws:sns:eu-west-2:123456654321:topic3
26+
or
27+
Name: /prod/dynamic-trigger
28+
Value: arn:aws:sns:eu-west-2:123456654321:topic1,arn:aws:sns:eu-west-2:123456654321:topic2</p>
29+
<p>The config parameters:</p>
30+
<ul>
31+
<li>region: the region of the Systems Manager -&gt; Parameter Store</li>
32+
<li>functions:<ul>
33+
<li>name: The name of the function</li>
34+
</ul>
35+
</li>
36+
</ul>
37+
<p>plugins:</p>
38+
<ul>
39+
<li>@kakkuk/serverless-aws-lambda-dynamic-trigger
40+
custom:
41+
dynamicTrigger:
42+
region: &quot;eu-west-2&quot; // !!! Optional !!! It&#39;ll fall back to AWS_DEFAULT_REGION if it&#39;s not set
43+
functions:<ul>
44+
<li>name: &quot;handler&quot;
45+
ssmPath: &quot;{/path/to/triggers}&quot;</li>
46+
</ul>
47+
</li>
48+
</ul>
49+
</dd>
50+
</dl>
51+
52+
## Members
53+
54+
<dl>
55+
<dt><a href="#Package @kakkuk/serverless-aws-lambda-dynamic-trigger">Package @kakkuk/serverless-aws-lambda-dynamic-trigger</a></dt>
56+
<dd><p>Serverless plugin registers a set of events stored in the AWS Parameter Store.</p>
57+
</dd>
58+
</dl>
59+
60+
<a name="index.module_js"></a>
61+
62+
## js
63+
The plugin can register triggers (events) for a lambda function dynamically. At deployment time
64+
1. It fetches the value of a parameter in the Parameters. The value must be a list ARNs sepearted by comma.
65+
2. Parses the individual ARNs.
66+
3. Register the ARNs as triggers with the configured lambda function or functions.
67+
68+
Please note that currently you can only use the plugin with sns, sqs or kinesis triggers (events).
69+
70+
The original idea is to make the same lambda function triggered by different events on different environments (stages).
71+
Like on *dev* foo lambda function is triggered by
72+
- arn:aws:sns:eu-west-2:123456654321:topic1
73+
- arn:aws:sns:eu-west-2:123456654321:topic2
74+
- arn:aws:sns:eu-west-2:123456654321:topic3
75+
while on *prod* foo lambda function is triggered by
76+
- arn:aws:sns:eu-west-2:123456654321:topic1
77+
- arn:aws:sns:eu-west-2:123456654321:topic2
78+
This way we can switch features on and off on different stages.
79+
80+
The dynamic trigger sets needs to ne stored in the Parameter Store of the Systems Manager (SSM) and it should look somewhat like this:
81+
Name: /dev/dynamic-trigger
82+
Value: arn:aws:sns:eu-west-2:123456654321:topic1,arn:aws:sns:eu-west-2:123456654321:topic2,arn:aws:sns:eu-west-2:123456654321:topic3
83+
or
84+
Name: /prod/dynamic-trigger
85+
Value: arn:aws:sns:eu-west-2:123456654321:topic1,arn:aws:sns:eu-west-2:123456654321:topic2
86+
87+
The config parameters:
88+
- region: the region of the Systems Manager -> Parameter Store
89+
- functions:
90+
- name: The name of the function
91+
92+
plugins:
93+
- @kakkuk/serverless-aws-lambda-dynamic-trigger
94+
custom:
95+
dynamicTrigger:
96+
region: "eu-west-2" // !!! Optional !!! It'll fall back to AWS_DEFAULT_REGION if it's not set
97+
functions:
98+
- name: "handler"
99+
ssmPath: "{/path/to/triggers}"
100+
101+
<a name="Package @kakkuk/serverless-aws-lambda-dynamic-trigger"></a>
102+
103+
## Package @kakkuk/serverless-aws-lambda-dynamic-trigger
104+
Serverless plugin registers a set of events stored in the AWS Parameter Store.
105+
106+
**Kind**: global variable

0 commit comments

Comments
 (0)