Skip to content

Commit

Permalink
Fix invocation of lambda functions that are just strings (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterwerlla authored Mar 13, 2020
1 parent 97b81a7 commit 081a102
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 85 deletions.
17 changes: 16 additions & 1 deletion Tasks/LambdaInvokeFunction/TaskOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export class TaskOperations {
FunctionName: this.taskParameters.functionName
}
if (this.taskParameters.payload) {
params.Payload = Buffer.from(this.taskParameters.payload)
if (this.isJson(this.taskParameters.payload)) {
params.Payload = Buffer.from(this.taskParameters.payload)
} else {
params.Payload = Buffer.from(JSON.stringify(this.taskParameters.payload))
}
}
if (this.taskParameters.invocationType) {
params.InvocationType = this.taskParameters.invocationType
Expand Down Expand Up @@ -49,6 +53,17 @@ export class TaskOperations {
}
}

// A quick heuristic for if it is json. In the past, it was possible to put in just a string
// which is not valid JSON. This heurstic checks for if it is a JSON string, object, or array
private isJson(input: string): boolean {
const i = input.trim()

return (
(i.startsWith('"') || i.startsWith('{') || i.startsWith('[')) &&
(i.endsWith('"') || i.endsWith(']') || i.endsWith('}'))
)
}

private async verifyResourcesExist(functionName: string): Promise<void> {
try {
await this.lambdaClient.getFunctionConfiguration({ FunctionName: functionName }).promise()
Expand Down
2 changes: 1 addition & 1 deletion Tasks/LambdaInvokeFunction/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"type": "multiLine",
"label": "Payload",
"defaultValue": "",
"helpMarkDown": "The payload to pass to the function."
"helpMarkDown": "The payload to pass to the function. The payload must be JSON."
},
{
"name": "invocationType",
Expand Down
Loading

0 comments on commit 081a102

Please sign in to comment.