Last active
October 18, 2019 22:33
-
-
Save arianacosta/bc7e0f465b7a772420e6cfac3884dd19 to your computer and use it in GitHub Desktop.
Uses TS interfaces to create a request and get a response from Lambdas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Lambda } from 'aws-sdk' | |
import { GetGreetingRequest, GetGreetingResponse } from './GetGreetingLambda.ts'; | |
(async () => { | |
const lambda = new Lambda({ | |
apiVersion: '2015-03-31', | |
region: 'us-east-1', | |
}); | |
const request: GetGreetingRequest = { // TS checks the Request properties | |
firstName: 'Arian', | |
lastName: 'Acosta', | |
isFormal: false, | |
}; | |
const params = { | |
FunctionName: "LAMBDA_ARN", | |
Payload: JSON.stringify(request), | |
}; | |
const { Payload } = await lambda.invoke(params).promise(); | |
// Asserting the parsed object as GetGreetingResponse | |
const response = JSON.parse(Payload as string) as GetGreetingResponse; | |
console.log(response.greeting); // TS autocompletes .greeting ! | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment