@allthings/cloud-toolkit 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,3 +12,8 @@ Standardizes the setup of aws, datadog and other things
12
12
  | function | description |
13
13
  |----------|------------------------------------------------|
14
14
  | sendMail | Sends an email via SES or provided SMTP config |
15
+
16
+ ### lambda
17
+ | function | description |
18
+ |--------------|------------------------------------------------------|
19
+ | invokeLambda | Invokes set lambda with invocations type and payload |
package/dist/lib.cjs.js CHANGED
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ var clientLambda = require('@aws-sdk/client-lambda');
3
4
  var clientSsm = require('@aws-sdk/client-ssm');
4
5
  var aws = require('@aws-sdk/client-ses');
5
6
  var nodemailer = require('nodemailer');
@@ -27,6 +28,37 @@ const DEFAULT_AWS_CONFIGURATION = {
27
28
  region: 'eu-central-2',
28
29
  };
29
30
 
31
+ const invokeLambda = async (lambdaClient, config, invocationType, payload) => {
32
+ try {
33
+ const invokeCommand = new clientLambda.InvokeCommand({
34
+ FunctionName: config.functionName,
35
+ InvocationType: invocationType,
36
+ Payload: Buffer.from(JSON.stringify(payload)),
37
+ });
38
+ const result = await lambdaClient.send(invokeCommand);
39
+ if (config.logger) {
40
+ config.logger('info', JSON.stringify(result));
41
+ }
42
+ return true;
43
+ }
44
+ catch (error) {
45
+ if (config.logger) {
46
+ config.logger('error', error);
47
+ }
48
+ return false;
49
+ }
50
+ };
51
+ const lambda = (userConfig) => {
52
+ const { clientConfig, ...config } = {
53
+ ...DEFAULT_AWS_CONFIGURATION,
54
+ ...userConfig,
55
+ };
56
+ const lambdaClient = new clientLambda.LambdaClient(clientConfig);
57
+ return {
58
+ invokeLambda: (invocationType, payload) => invokeLambda(lambdaClient, config, invocationType, payload),
59
+ };
60
+ };
61
+
30
62
  const DEFAULT_CLIENT_CONFIG$1 = {
31
63
  clientConfig: DEFAULT_AWS_CONFIGURATION,
32
64
  onlyDefault: false,
@@ -102,7 +134,7 @@ const sendMail = async (config, transport, options, from) => {
102
134
  ...options,
103
135
  });
104
136
  if (config.logger) {
105
- config.logger('info', `Transporter response: ${transportResponse}`);
137
+ config.logger('info', `Transporter response: ${JSON.stringify(transportResponse)}`);
106
138
  }
107
139
  if (!transportResponse) {
108
140
  if (config.logger) {
@@ -128,5 +160,6 @@ const ses = (fromEmailAddress, userConfig) => {
128
160
 
129
161
  exports.DEFAULT_PARAMETER_STORE_CLIENT_CONFIG = DEFAULT_CLIENT_CONFIG$1;
130
162
  exports.DEFAULT_SES_CLIENT_CONFIG = DEFAULT_CLIENT_CONFIG;
163
+ exports.lambda = lambda;
131
164
  exports.parameterStore = parameterStore;
132
165
  exports.ses = ses;
package/dist/lib.esm.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { LambdaClient, InvokeCommand } from '@aws-sdk/client-lambda';
1
2
  import { SSMClient, GetParametersCommand, GetParameterCommand } from '@aws-sdk/client-ssm';
2
3
  import * as aws from '@aws-sdk/client-ses';
3
4
  import { createTransport } from 'nodemailer';
@@ -6,6 +7,37 @@ const DEFAULT_AWS_CONFIGURATION = {
6
7
  region: 'eu-central-2',
7
8
  };
8
9
 
10
+ const invokeLambda = async (lambdaClient, config, invocationType, payload) => {
11
+ try {
12
+ const invokeCommand = new InvokeCommand({
13
+ FunctionName: config.functionName,
14
+ InvocationType: invocationType,
15
+ Payload: Buffer.from(JSON.stringify(payload)),
16
+ });
17
+ const result = await lambdaClient.send(invokeCommand);
18
+ if (config.logger) {
19
+ config.logger('info', JSON.stringify(result));
20
+ }
21
+ return true;
22
+ }
23
+ catch (error) {
24
+ if (config.logger) {
25
+ config.logger('error', error);
26
+ }
27
+ return false;
28
+ }
29
+ };
30
+ const lambda = (userConfig) => {
31
+ const { clientConfig, ...config } = {
32
+ ...DEFAULT_AWS_CONFIGURATION,
33
+ ...userConfig,
34
+ };
35
+ const lambdaClient = new LambdaClient(clientConfig);
36
+ return {
37
+ invokeLambda: (invocationType, payload) => invokeLambda(lambdaClient, config, invocationType, payload),
38
+ };
39
+ };
40
+
9
41
  const DEFAULT_CLIENT_CONFIG$1 = {
10
42
  clientConfig: DEFAULT_AWS_CONFIGURATION,
11
43
  onlyDefault: false,
@@ -81,7 +113,7 @@ const sendMail = async (config, transport, options, from) => {
81
113
  ...options,
82
114
  });
83
115
  if (config.logger) {
84
- config.logger('info', `Transporter response: ${transportResponse}`);
116
+ config.logger('info', `Transporter response: ${JSON.stringify(transportResponse)}`);
85
117
  }
86
118
  if (!transportResponse) {
87
119
  if (config.logger) {
@@ -105,4 +137,4 @@ const ses = (fromEmailAddress, userConfig) => {
105
137
  };
106
138
  };
107
139
 
108
- export { DEFAULT_CLIENT_CONFIG$1 as DEFAULT_PARAMETER_STORE_CLIENT_CONFIG, DEFAULT_CLIENT_CONFIG as DEFAULT_SES_CLIENT_CONFIG, parameterStore, ses };
140
+ export { DEFAULT_CLIENT_CONFIG$1 as DEFAULT_PARAMETER_STORE_CLIENT_CONFIG, DEFAULT_CLIENT_CONFIG as DEFAULT_SES_CLIENT_CONFIG, lambda, parameterStore, ses };