Calling AWS services from parse cloud code? - node.js

Which is the best way of making call to AWS services like S3,Dynamodb,cloudwatch etc from parse cloud code (offcourse while taking care of cloud function timeouts)?
Importing AWS sdk in parse cloud code is showing lot of wierd errors relating to internal file dependencies which is very time consuming to resolve.
Is calling them via aws lambda function REST call advisable? or Please quote me an example if some better technique is available.
Thanks

Related

How to create mock for AWS resources like dynamoDB for local development in nodejs lambda?

Does anyone know how can I mock AWS resources and their operations for local development? Currently I have a lambda that inserts values into dynamoDB. I'm not looking for test case purpose (will need these later on), but if possible I can do these operations without calling actual services?
Currently I have created a mockdb.js file with similar values of dynamoDB but to run the code everytime on my machine, I have to comment out the actual aws code and mention-
import {data} from '../mockdb.js';
const list = insertTable(data) // everytime I have to add this instead of aws dynamoDB sdk
Is their a easier way? Eg. For react applications we have mockservers to mimic route responses?
There is no easy way to test and develop all AWS services locally but there are some solutions which cover some services and use cases (the list is not exhaustive):
Localstack: https://github.com/localstack/localstack
SAM framework allows local testing and developing for some services (Lambdas and API Gateway): https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-test-and-debug.html
There is a downloadable local version for DynamoDB: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html
For missing services you have to develop your own mock services/functions and use them for developing testing.

Nestjs run on lambda function without creating an actual server to combine with AWS API Gateway?

I have 5 HTTP microservices written with NestJS. I have to convert them into lambda function where each service will have its own lambda function. The purpose of this is to completely turn my service to serverless.
I am using API Gateway to map requests to the right lambda by the given request path.
Now creating an MVC pattern from scratch that receives an URL path and resolves and controller & function needed (including url params, and such) is something that has already been done by both express and nestjs.
Is there a way to implement nesstjs's abstraction functionality without the actual server listening? So I can simply pass nestjs the URI and request data and it will work upon it?
Any other solutions for running an MVC serverless process on lambda?
After researching the network and looking at these great answers, I thought of collecting that information all together and bring something more detailed, things that I was misunderstanding from these articles described and the ones I found.
How to create a serverless NestJS application using Lambda function AWS provider
Let's take a look at this repository article: https://github.com/rdlabo/serverless-nestjs
Thanks to him, he basically packed a ready-to-go NestJS project configured with the Serverless framework.
I am not experienced enough to say a lot about the serverless framework, but I can explain how it works in our case.
First of all, there is a serverless.yml as explained the the github repository article, basically here you describe the name of your lambda function, what to exclude in the package and the trigger http events.
functions:
index:
handler: dist/index.handler
events:
- http:
cors: true
path: '/'
method: any
- http:
cors: true
path: '{proxy+}'
method: any
If we take a look at this yml file, you can see that we have 2 HTTP events, one for root path and one for proxy path.
A question that I asked myself when reading through it, so what does this event part do?
This part of the yml basically creates your endpoints in the API Gateway service of AWS (if you are using AWS as a provider).
API Gateway is a service made by AWS that lets you map requests into other services of AWS, such as a lambda function.
When you run the command sls deploy
after entering your credentials using sls config credentials the serverless framework will create OR modify if exists a new lambda function based on the config you set up, and set up API Gateway endpoints linked to that lambda.
After deploying you will receive a link that activates that lambda.
The example I use basically uses an express serverless solution that someone created, basically a proxy code that knows how to receive API Gateway request object and transform it to express and activate it without having a server running.
Note: Serverless is using CloudFormation to create a stack in order to upload and deploy the lambda function, I think with this way you can upload more than 250mb unzipped, because my project currently is 450mb unzipped, I am not sure about this but when I tried uploading a bigger zip, my lambda started overflowing, as in saying that it is missing some modules, I guess because of the size.
Or maybe serverless really optimizes the modules so the uploaded package is much smaller than what you expect. If someone knows about this, +1!
You must provide more info about your architecture.
Have you used Blueprint with microservice?
Then choose microservice-http-endpoint.
Take a look at [microservice-http-endpoint] example.
1
You can convert you whole express app in a lambda function and process request as they come in.
This blog shows how to do it for express: https://serverless.com/blog/serverless-express-rest-api/
Similar thing can be tried for NestJS as well. So lambda function is basically a piece of code that gets executed as the request come in. There are limitation on the size of the code and time to execute a job which is platform specific.

How to create tests for serverless testing using Jest for these scenarios?

I am new to serverless and NodeJS.Could you please guide me how can I create a automated test cases for
lambda to lambda invoke
API Gateway to Lambda Invoke
DynamoDB insertion test
Please help. Thanks in advance.
If you want full end-to-end test of a lambda function, you will have to handle that outside the function itself.
If you use unit testing tools you will be able to run them locally or even inside the function, but you won't have the ability to actually query the function and go through the whole process.
I'd create a second lambda function with any unit test library, like mocha, and write functional tests that invoke the first lambda function, through API gateway, with a simple http-request package (like request).
EDIT:
Here's more clarification on each one of your points:
1) Lambda to lambda invoke
If by lambda-to-lambda you mean you want to call another function WITHOUT using API GW, then I guess you're planning to use the AWS SDK to trigger a function.
If that is the case, it's like any other test. You will create a test function which will get the SDK to trigger the second lambda, and then check the result of the SDK function. It will probably indicate if it's a success or not, or even give you the result.
2) API gateway to lambda invoke
If you are looking to test if the connection between API GW and lambda works, I'd say, why bother? It's a setup-once-and-use kind of deal.. But if you still want to test this, it will be similar to item 1), with the exception that instead of using the SDK, you'd use an API gateway URL.
So you can use an npm package such as axios or request to make a request to such URL and see if the content is the expected.
I'd even say you can run the test in the lambda function and call the very same lambda function, no need to create separate lambdas.
3) Dynamo insertion
This one is the easiest, just create a unit test that writes something into dynamo. Then, in order to know if the test passes or not, just read the DB trying to find what you wrote.
If you're in the fence between testing libraries, I'd suggest going for mocha and chai.
If I can help you answering something more specific, let me know.

Calling secured API from NodeJS Lambda

I have an API that is secured using OAuth (IdentityServer 4). I need to call this from an AWS Lambda function. I cant figure out how to do this in Node (noob to node). I can see an example of using oidc-client package but it seems to be designed for browser based clients. I just need the access token to call the api. Ive done this from a .Net console app, but Im lost in Node. Does anyone know of an example of doing this?
Thanks
#Jonesie have you tried example from AWS repository? Request you to take a look https://github.com/awslabs/serverless-application-model/tree/master/examples/apps/api-gateway-authorizer-nodejs

Webhook deployment on Lambda AWS

I am trying to deploy the Webhook Example for Google Actions found here onto Lambda AWS.
I was successful deploying and making the POST calls using ngrok. So, no problems there.
But the issue i found is it uses Express node module for POST request calls. Lambda AWS fails when the request is made to Express module. So is there a way to make the POST call successful.
I tried using Lambda-Express node module to deploy it, but it seems to have some issue as well.
Lambda AWS does not directly support an HTTP interface.
One solution would be for you to use API Gateway which would allow you to translate the HTTPS POST that AoG sends, into a call to AWS Lambda.
In your lambda you will handle the request which comes in via the standard Lambda handler:
function( event, context, callback );
instead of via Express. You would probably also want to remove Express from your code, which might sound like a lot of work, but I took a brief look at it when it was released and my impression was that the dependence on Express was minor and quite unnecessary.
The alternative would be to switch from Lambda to something HTTP based like Google App Engine which is also serverless (to a degree). I guess that might be easier but I don't know what your other factors might be.
I have come across this tutorial that explains the step by step process of connecting API.ai intent using the Lambda function.
And if you follow the Google web hook example to deploy it to Lambda, then it's a wrong direction. Completely eliminate the express usage and also the Assistant class is not necessary when Lambda deployment is necessary.

Resources