What do i put in the "handler" prop of aws-cdk's gateway.LambdaRestApi? - node.js

I'm building out one of my first gateway api's and have been reading the code and documentation here.
For an apigateway which use made using the LambdaRestApi function, my understanding was that i define the endpoints and the lambda attached to the endpoints.
If that's the case, what do i put as the functions handler function? I don't have any plans for there to be a base route for it so do i have to just have a blank lambda here? Or am i going in the wrong direction with my thinking?

LambdaRestApi is just a utility construct based on RestApi with defaultIntegration to a Lambda function.
You can use a plain RestApi instead if you don't need the default proxy integration to a Lambda handler configured.

Related

Best practice to call lambda to lambda with multi tenancy?

In my Serverless web-app (nodeJS) which supports multi tenancy, I have the following architecture:
Layer of controller - each controller is a lambda function (separated repository)
Layer of service - each service is a lambda function (another separated repository) which also calling to Dynamo DB.
Currently the controller is calling the service lambda using http (development purposes only) and we want to make it better using aws-sdk with lambda.invoke() or Step functions.
In case that we will use lambda.invoke(), there is a need to have a stable ARN per each lambda function and use it over other lambda's.
My question is, how can I have an ARN per each tenant+lambda and how can I maintain it?
In other case which we will use step functions, I wanted to know if its suitable for this kind of architecture ?
You don't need to maintain the lambda ARN if you know where to look for the current one. You can use an export from a CloudFormation deck or ssm parameter or DynamoDB or anything really. Even for the step function you can redeploy with using the CloudFormation output exports and it will point to the correct ARNs.

How to make a function as default in azure function app?

I have an azure function app (abc.azurewebsites.net) and have a function named Function1 defined in it. (abc.azurewebsites.net/api/Function1?code=def==)
Is there a way in which, I can define this Function1 as default and all the incoming requests to abc.azurewebsites.net gets routed to my Function1?
Thanks.
You could use proxy to get this.
Follow this tutorial, and the Route template should be /, and then you should change your function authLevel to anonymous like this.
1.Set the application setting add HELLO_HOST(any parameter same as the backend url) with value <YourBackendApp>.azurewebsites.net.
2.Create the proxy.
3.Set your function to anonymous then your function don't need API keys code to call it.
Then below my test result, it's a HTTP trigger function with query string &name=<yourname>.

aws-xray captureAWS annotations

I've begun using AWSXRay in order to get more insight into why performance is not ideal in my lambda function. This lambda function runs a gql service meaning it has lots of outbound requests to other lambda functions as well as dynamodb for caching.
I've added tracing to all aws-sdk client calls by utilizing the following in my handler. It mutates the imported AWS module so that all subsequent usage of AWS clients successful includes aws-xray tracing, regardless of what module imports it. Awesome!
import AWS from 'aws-sdk';
import AWSXRay from 'aws-xray-sdk';
AWSXRay.captureAWS(AWS);
Heres an example of the output:
The Problem
The problem is that none of the "Traces" have any annotation regarding the parameters of the requests. Both the annotation and metadata of each trace is empty:
The Hope
The hope is that there is a way to configure the AWSXRay CaptureAWS modifications so that they include the arguments of each aws-client request in the annotations or metadata.
The Question
Is it possible to request that AWSXRay.captureAWS(AWS); includes the parameters passed to the aws sdk client invocations in either the annotations or the metadata of the traces it produces?
The resources section contains high level arguments for some clients e.g. DynamoDB table name. Not all arguments are captured by default. This is because they may contain information that the users do not wish to track in their trace and may also be verbose.
For now opt-in is not available in X-Ray SDK for arbitrary API parameters. As a workaround for now, I would suggest that you wrap your sdk calls in a local subsegment and record the parameters you want to capture as annotations or Metadata for that subsegment. Let me know if you need any help in locating docs that allow you to create your own subsegments.

Independent NPM library that validates request based on swagger file

We are building APIs using Swagger, AWS API gateway and Lambda functions with NodeJS. The API gateway will do the request validation, however as per the design, the lambda functions need to re-validate the request object as an API Gateway Proxy Request Event. This makes sense as in theory we can reuse the lambda functions by invoking them via other event source (e.g. SNS).
Therefore we need an NodeJS tool which can validate the request (not only body but also params, etc) based on the swagger spec - exactly what the swagger-tools and a few other tools (e.g. swagger-request-validator) are doing, but not as a middleware.
I did some search but could not find one, also looked into swagger-tools source code, reckon its validation component was written in the way that cannot be easily used separately.
Any suggestion is welcome. Thanks in advance.
You can use swagger-model-validator.
var Validator = require('swagger-model-validator');
var swaggerFile = require("./swagger.json");
const validator = new Validator(swaggerFile);
console.log(validator.validate({
name: 'meg'
}, swaggerFile.definitions.Pet, swaggerFile.definitions, true).GetErrorMessages())
This outputs:
[ 'photoUrls is a required field' ]
validator.validate returns an object, so you can also check if the returned object contains anything under the errors attribute. It should be as simple as
if (validator.validate({
name: 'meg'
}, swaggerFile.definitions.Pet, swaggerFile.definitions, true).errors) {
// do something with error
}
I have used Swagger's sample JSON for this answer.

How to use lambda and api getaway to trigger a custom event?

I'm trying to understand how The AWS api gateway works with lambda. What i am wanting to do is quite simple :
When I submit a basic form in a localhosted web page, this simple action should invoke a lambda function.
I know i need to use aws api gateway to complete this action and i read some tutorials online but i can't figure out how to start a lambda function after a custom event.
Thanks yor any help.
It is easier to understand if you work backwards. First, make your custom event handler. Amazon provides a good overview of what you need to do here:
http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html
If you need more of a kick in the right direction, LithosTech has a well-written guide to handling FORM POST events in Lambda here:
http://lithostech.com/2015/10/aws-lambda-example-contact-form-handler/
At its simplest level, you're going to have a function that takes an event parameter and does something with its values:
var AWS = require('aws-sdk');
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
// TODO: Do something with event.name, event.email, event.*, ...
}
After you make this function in a .JS file, upload it using the Lambda Web console - you can do it entirely from the command line, but it's easier to use the Web interface when you're first starting out. The biggest benefit to doing it this way is that during the creation process, you'll be asked if you want to make an API gateway endpoint for the function - say yes! This will automatically create a suitable entry for you and give you the details. Drop those in your form and you're off to the races!

Resources