API Gateway Example


Create function for Customer Record

customerWrite


const  AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});

exports.handler = function(event, context, callback) {
    var params = {
        Item: {
            name: "Jonathan Iacovacci",
            address: "55 Daffodil Road",
            phone: "917-701-6177"
        },
        TableName: 'Customer'
        };
        docClient.put(params, function(err, data){
        if (err){
            callback(err, null);
        }else{
            callback(null, data);


Creates a new record on existing customer table

column are added as they are provide e.g. phone

customerQuery


console.log('Starting Function');

const  AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});

exports.handler = function(event, context, callback) {
   
        var params = {
        TableName: 'Customer',
        Key: {
            "name": "Jonathan Iacovacci"
        }
        };
        docClient.get(params, function(err, data){
        if (err){
            callback(err, null);
        }else{
            callback(null, data);
        }
        });
        }


customerInfo

const AWS = require('aws-sdk');
const querystring = require('querystring');

// Set this to the region you upload the Lambda function to.
AWS.config.region = 'us-east-1';

exports.handler = function(evt, context, callback) {
    // Our raw request body will be in evt.body.
    const params = querystring.parse(evt.body);

    // Our field from the request.
    const my_name = params['my-name'];
    const my_address = params['my-address'];
    const my_phone = params['my-phone'];

 
    // Generate HTML.
    const html = `<!DOCTYPE html><p>Customer Name: ` + my_name + ` <br> Customer Address:
               ` + my_address + `<br> Customer Phone `+  my_phone + `</p>`;

   // const html = `<!DOCTYPE html><p>Customer Info ` + my_name + my_address + my_phone`</p>` ;

    // Return HTML as the result.
    callback(null, html);
};
Create the API


Create a method to match your function


Select the POST method to match the HTTP request from your HTML form 


Attach newly created Lambda function to the method


Now we configure our requests to receive data from the HTML form 

to the Lambda function then send back data from LAMBDA function

Then send output data from Lambda function to the client web browser


We start with Integration Request to map the data coming from the web page into the lambda function in the Mapping Templates section


Request comes in with a URL encoded body

 (Content-Type: application/x-www-form-urlencoded).


Add mapping template (click the +) 

put application/x-www-form-urlencoded in Content-Type box Click check 

Then map it to JSON form so the function can understand it


{ 

 "body": "$input.body"

}

template takes the raw request body, $input.body, and places it inside a JSON object on the body property. 





Change the Method Response where the function is sending data back to the browser




Add Content-type for Response Headers to state that the response will include this header.


 Remove application/json in Response Body because we are not sending JSON back





next we need to adjust the Integration Response




Click on Arrows to expand the 200 method response status and Header Mappings

Under Header Mappings 

Click the edit button beside Content-Type.
Enter 'text/html' (the single quotes must be there) and click the checkmark:

Under Mapping Templates, remove application/json.

 Add mapping template and enter text/html

This tells API Gateway to send a particular template when sending text/html.

Enter this into the template editor:

$input.path('$')

Then click Save:

response from our function is a JSON string. $input.path()
 takes as its parameter a JSONPath expression.
Using $ as the parameter means to access the root object.
In our case, the root object is a string.
This means the value of the template becomes the value of the string, which is HTML.








Now deploy the API from Actions drop down

use prod for the stage name


Now put the API Gateway URL in your HTML file that will 
send the data via HTTP POST


<html>
<h1>
</h1>
<h1>Enter Customer Information</h1>
<br>
<br>
<form method="POST" action="https://pesebiwhz1.execute-api.us-east-1.amazonaws.com/prod">
    Name: <input type="text" name="my-name"><br>
Address: <input type="text" name="my-address"><br>
Phone: <input type="text" name="my-phone">

    <input type="submit" value="Submit form">
</form>
</html>
Delete index.html
upload new html files

Welcome to UCONN Bagel World

The largest assortment of Bagels on the UCONN Stamford Campus
MessageTest2 function
const AWS = require('aws-sdk');
const querystring = require('querystring');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
myCustInfo

const AWS = require('aws-sdk');
const querystring = require('querystring');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});

// Set this to the region you upload the Lambda function to.
AWS.config.region = 'us-east-1';

exports.handler = function(evt, context, callback) {
    // Our raw request body will be in evt.body.
    const params = querystring.parse(evt.body);

    // Our field from the request.
    const my_name = params['my-name'];
    const my_address = params['my-address'];
    const my_phone = params['my-phone'];

    
    var newrec = {
        Item: {
            name: my_name,
            address: my_address,
            phone: my_phone
        },
        TableName: 'Customer'
        };
    
        docClient.put(newrec, function(err, data){
        if (err){
            callback(err, null);
        }else{
            callback(null, data);
        }
        });
        }

    // Generate HTML.
//    const html = `<!DOCTYPE html><p>Customer Name: ` + my_name + ` <br> Customer Address: 
  //             ` + my_address + `<br> Customer Phone `+  my_phone + `</p>`;

   // const html = `<!DOCTYPE html><p>Customer Info ` + my_name + my_address + my_phone`</p>` ;

    // Return HTML as the result.


No comments:

Post a Comment

To All, If you are working in starter accounts please move the work into the classroom account so I can review. For the previous ...