Using Lambda to return data

Getting started with Lambda.

CreateLambdaFunction-CreateLambdaFunction-lambda-en

Create author from scratch using Node.js version 6.

CreateLambdaFunction-authorFromScratch-en

Daterange picker passes start and end parameters. The following code returns data based on the parameters.

CreateLambdaFunction-testLambda-en

exports.handler = (event, context, callback) => {
  const timeunit = (Number(event.end) - Number(event.start))/100
  const category = ['A', 'B', 'C', 'D']
  const result = (function () {
    let arr = []
    for (var i = 100; i >= 1; i--) {
      arr.push({
        timestamp: Number(event.start) + Math.floor(timeunit*i),
        value:{
            category:category[Math.floor(Math.random()*4)],
            value: Math.floor(Math.random()*10),
            created:Number(event.start) + Math.floor(timeunit*i)
        }
      })
    }
    return arr
  })()
  callback(null, result);
};

Creating an API Gateway that connects to the Lambda function.

CreateLambdaFunction-amazonApiGgateway-en

Creating a new API.

CreateLambdaFunction-newApi-en

Creating a new resource.

CreateLambdaFunction-newResource-en

Enter the path name and check "Enable API Gateway CORS".

CreateLambdaFunction-newResourceChild-en

With resource completed create a GET method.

CreateLambdaFunction-createGetMethod-en

Setting a method. Select the Lamnda function created (It will not appear unless you select the region where you created Lamdba).

CreateLambdaFunction-setupMethod-en

Once a method is created edit the method response.

CreateLambdaFunction-methodResponse-en

Create the 200 hedder as "Access-Control-Allow-Origin".

CreateLambdaFunction-200Header-en

Create an intergration response.

CreateLambdaFunction-intergrationResponse-en

Set「Access-Control-Allow-Origin」」 as'*'.

CreateLambdaFunction-accessControl-en

Lastly, change "Intergration Request".

CreateLambdaFunction-intergrationRequest-en

Create the following mapping of "body mapping template" with "application / json" as shown below

{
  "start": "$input.params('start')",
  "end": "$input.params('end')"
}

CreateLambdaFunction-bodyMapping-en Next create a "deploy api".

CreateLambdaFunction-deployApi-en

Input a deploy name.

CreateLambdaFunction-deployName-en

The resource named / test becomes the endpoint to be registered with DataSource.

*example endpoint.
https://*********.execute-api.ap-northeast-1.amazonaws.com/teststage/test

CreateLambdaFunction-exampleEndpoint-en

results matching ""

    No results matching ""