1 | const fs = require( "fs" );
|
2 | const AWS = require( "aws-sdk" );
|
3 |
|
4 | const utils = require( "./utils" );
|
5 |
|
6 | function getLambdaEventFile () {
|
7 |
|
8 | const path = `${process.env.COLLY__PROJECT_DIR}/${process.env.COLLY__LAMBDA_EVENT_FILE}`;
|
9 |
|
10 | if ( fs.existsSync( path ) ) {
|
11 | return JSON.parse( fs.readFileSync( path, "utf8" ) );
|
12 | } else {
|
13 | return {};
|
14 | }
|
15 |
|
16 | }
|
17 |
|
18 | function runningLocally () {
|
19 | return JSON.parse( process.env.COLLY__RUN_LAMBDA_LOCAL );
|
20 | }
|
21 |
|
22 | function runLocally () {
|
23 |
|
24 |
|
25 | return new Promise( ( resolve, reject ) => {
|
26 |
|
27 | const projectConfig = utils.getProjectConfig();
|
28 |
|
29 | utils.addLambdaEnvironmentVariablesToProcess( projectConfig.environmentVariables );
|
30 |
|
31 | const config = utils.getLambdaConfigFile();
|
32 |
|
33 | const lambdaHandler = getLambdaHandler();
|
34 |
|
35 | const eventFile = getLambdaEventFile();
|
36 |
|
37 | lambdaHandler(
|
38 | eventFile,
|
39 | {
|
40 | "done": () => {}
|
41 | },
|
42 | function callback( err, data ) {
|
43 | if ( err ) {
|
44 | reject( err );
|
45 | }
|
46 | resolve( data );
|
47 | }
|
48 | );
|
49 |
|
50 | });
|
51 |
|
52 | }
|
53 |
|
54 | function runDeployedLamda () {
|
55 |
|
56 | utils.setAwsRegion();
|
57 | const config = utils.getLambdaConfigFile();
|
58 | const lambda = new AWS.Lambda();
|
59 | const params = {
|
60 | "FunctionName": utils.getLambdaName(),
|
61 | "InvocationType": "RequestResponse",
|
62 | "Payload": JSON.stringify( getLambdaEventFile() ),
|
63 | "LogType": "Tail"
|
64 | };
|
65 |
|
66 | lambda.invoke(params, function(err, data) {
|
67 | if ( err ) {
|
68 | console.log( err );
|
69 | } else {
|
70 | if ( data.LogResult ) {
|
71 | console.log( "Status code: " + data.StatusCode );
|
72 | console.log( "Payload: " + data.Payload );
|
73 | console.log( Buffer.from( data.LogResult, "base64" ).toString() );
|
74 | }
|
75 |
|
76 | }
|
77 | });
|
78 |
|
79 | }
|
80 |
|
81 | function getLambdaHandler() {
|
82 | try {
|
83 | return require( utils.getLambdaFilePath() )[ utils.getLambdaHandlerName() ];
|
84 | }
|
85 | catch( err ) {
|
86 | console.log( "Error: Cannot find the lambda you are trying to run. Check the `--name` or the handler property in the lambda's function file.");
|
87 | throw err;
|
88 | }
|
89 | }
|
90 |
|
91 | function init () {
|
92 |
|
93 | return new Promise( ( resolve, reject ) => {
|
94 |
|
95 | utils.authenticate().then( () => {
|
96 |
|
97 | if ( runningLocally() ) {
|
98 | runLocally()
|
99 | .then( ( data ) => {
|
100 | console.log( data );
|
101 | resolve( data );
|
102 | })
|
103 | .catch( reject );
|
104 | } else {
|
105 | runDeployedLamda();
|
106 | }
|
107 | resolve();
|
108 |
|
109 | }).catch( console.log );
|
110 |
|
111 | });
|
112 |
|
113 | }
|
114 |
|
115 | module.exports = {
|
116 | "getLambdaEventFile": getLambdaEventFile,
|
117 | "init": init,
|
118 | "runLocally": runLocally,
|
119 | "runningLocally": runningLocally
|
120 | } |
\ | No newline at end of file |