Skip to content

Commit

Permalink
[v0.9] Fix failed lambda execution crashing coordinator (#1580)
Browse files Browse the repository at this point in the history
* Fix failed lambda execution crashing coordinator

* Add changeset

* Check for data payload explicitly
  • Loading branch information
Siegrift authored Dec 5, 2022
1 parent 3571c24 commit cf382ad
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-weeks-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@api3/airnode-node': patch
---

Fix failed lambda execution crashing coordinator
35 changes: 29 additions & 6 deletions packages/airnode-node/src/workers/cloud-platforms/aws.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { logger } from '@api3/airnode-utilities';
import { goSync } from '@api3/promise-utils';
import AWS from 'aws-sdk';
import { WorkerParameters, WorkerResponse } from '../../types';

export function spawn(params: WorkerParameters): Promise<WorkerResponse> {
// lambda.invoke is synchronous so we need to wrap this in a promise
return new Promise((resolve, reject) => {
return new Promise((spawnResolve, spawnReject) => {
// Uses the current region by default
const lambda = new AWS.Lambda();

Expand All @@ -15,12 +17,33 @@ export function spawn(params: WorkerParameters): Promise<WorkerResponse> {
Payload: JSON.stringify(params.payload),
};

const resolve: typeof spawnResolve = (value) => {
logger.debug(`Successful Lambda response: ${value}`);
spawnResolve(value);
};

const reject: typeof spawnReject = (error) => {
logger.debug(`Lambda invocation or execution failed. Response: ${error}`);
spawnReject(error);
};

// The Lambda invocation callback populates the error (first argument) only when the invocation fails (e.g. status
// code is 400) or the request is not parsed properly by the SDK and can't be invoked. For errors and timeouts that
// happen inside the invoked lambda have "data.FunctionError" and "data.Payload.errorMessage" populated instead.
// See: https://stackoverflow.com/q/42672023 and https://stackoverflow.com/q/48644093
lambda.invoke(options, (err, data) => {
if (err) {
reject(err);
return;
}
resolve(JSON.parse(JSON.parse(data.Payload as string).body) as WorkerResponse);
if (err) return reject(err);

if (data.FunctionError) return reject(data.Payload);

if (!data.Payload) return reject(new Error('Missing payload in lambda response'));
const goPayload = goSync(() => JSON.parse(data.Payload!.toString()));
if (!goPayload.success) return reject(goPayload.error);

const goBody = goSync(() => JSON.parse(goPayload.data.body));
if (!goBody.success) return reject(goBody.error);

resolve(goBody.data as WorkerResponse);
});
});
}

0 comments on commit cf382ad

Please sign in to comment.