CS4521:   Mobile and Topics in Web Programming

 

STEP 4: Use Mobile Hub Project to generate some sample code showing you how to use your created Backend Services.

 

NOTE: You use the AWS Mobile SDK that lets you create a mobile App (there is SDK for both Android and iOS) that uses a
api given to you to make calls to your AWS backend services like Lambda Functions.

>>> The Mobile Hub Console helps you in this process --note there is a BUILD panel on a Mobile Hub Project that will generate sample code using the Mobile SDK either in Android or iOS that will give you code you can use or copy or modify to create your own App that will connect to your AWS project's backend services you set up.

>>> What I am showing you is how Mobile Hub does this and I explain (briefly) the code for invoking and getting responses from AWS Lambda function code.

>>> You can learn on your own how to invoke other AWS services like Identity and directly DynamoDB etc. There is sample code produced by mobile Hub

>>> If you want to investigate the AWS mobile SDK on your own ---go https://aws.amazon.com/mobile/sdk/

1) Go to Build panel in Mobile Hub Console of AWS for your project

2) Here I selected Android, it will generate a download package for me and give instructions (some of this like setting up for Android studio is done). You will download a sample Application that shows how to invoke AWS Lambda backend. Follow the instructions on how to import this application.


3) After importing the project- it contains in the demo folder a Fragment called CloudLogicDemoFragment

4) Run the Sample App --you will see this interface and when you hit the Invoke button it will call the Lambda function listed and return the results.

  • Launch MySampleApp in the Android emulator, click on the "Cloud Logic" section of the app, and then click "Demo Cloud Logic" in the next page. You will see the name of an AWS Lambda function, its request parameter in JSON format, and a blank text box for invocation result. 

HERE I am invoking the Lambda function userReadWrite and passing teh userid and password. NOTE there is nothing returned so null is correct.

HERE you can see the above data entry was written into the database of biker table as this Lambda function should do

+++++

5) Lets examine the code a bit

When button hit it invokes themethod invokeFunction

 

The invokeFunction reads in the data to be passed as input in the interface (NOTE it requires the
data to be stored in JSON format) to the backend functionName and
stores as the variable payload ByteBuffer. This payload is set as attribute in the InvokeRequest (this is
a class that comes from AWS mobile SDK code --see below) object created that points to the AWS Lambda
backend function of name funcitonName (that is typed into the interface).

The InvokeRequest instance requires a response which in this same code as shown below. It recieves the results (which are formatted as JSON) and simply dumps out to a textField in the Applications interface.

 

The other classes in the Sample Project are important supporting classes for securely connecting to the AWS Lambda function(s) for the project and are located in the
package amazonaws.mobile For example our code above imports the following classes (Note the InvokeRequest and InvokeResults classes)

import com.amazonaws.mobile.AWSMobileClient;
import com.amazonaws.services.lambda.model.InvocationType;
import com.amazonaws.services.lambda.model.InvokeRequest;
import com.amazonaws.services.lambda.model.InvokeResult;

 

 

 

© Lynne Grewe