Android: Execute Code On Demand with AWS Lambda

The following reference content only applies to existing apps that were built using
the AWS Mobile SDKs for iOS and Android. If you’re building a new mobile or web app,
or you're adding cloud capabilities to an existing app, visit the Amplify Framework website instead. Documentation for the AWS Mobile SDKs for iOS and Android is now
part of the Amplify Framework.

Overview

AWS Lambda is a compute service that runs your code in response to events and automatically
manages
the compute resources for you, making it easy to build applications that respond quickly
to new
information. The AWS Mobile SDK for Android enables you to call Lambda functions from
your Android
mobile apps.

The tutorial below explains how to integrate AWS Lambda with your app.

Setup

Prerequisites

Add the AWS Mobile SDK for Android to your app before beginning
this tutorial.

Create a Lambda Function in the AWS Console

For this tutorial, let's use a simple "echo" function that returns the input. Follow
the steps described at AWS Lambda Getting Started, replacing the function code with the code below:

Set IAM Permissions

The default IAM role policy grants your users access to Amazon Mobile Analytics and
Amazon Cognito
Sync. To use AWS Lambda in your application, you must configure the IAM role policy
so that it
allows your application and your users access to AWS Lambda. The IAM policy in the
following steps allows the
user to perform the actions shown in this tutorial on a given AWS Lambda function
identified by its
Amazon Resource Name (ARN). To find the ARN go to the Lambda Console and click the
Function name.

Type your identity pool name into the search box. Two roles will be listed: one for
unauthenticated users and one for authenticated users.

Click the role for unauthenticated users (it will have unauth appended to your Identity
Pool name).

Click the Create Role Policy button, select Custom Policy, and then
click the Select button.

Enter a name for your policy and paste in the following policy document, replacing
the function’s
Resource value with the ARN for your function (click your function’s Function name
in the AWS Lambda console to view its ARN).

Create a Lambda proxy

Declare an interface containing one method for each Lambda function call. Each method
in the interface must be decorated with the "@LambdaFunction" annotation. The LambdaFunction
attribute can take 3 optional parameters:

functionName allows you to specify the name of the Lambda function to call when the method is
executed, by default the name of the method is used.

logType is valid only when invocationType is set to "Event". If set, AWS Lambda will return
the last 4KB of log data produced by your Lambda Function in the x-amz-log-results
header.

invocationType specifies how the Lambda function will be invoked. Can be one of the following values:

Event: calls the Lambda Function asynchronously

RequestResponse: calls the Lambda Function synchronously

DryRun: allows you to validate access to a Lambda Function without executing it

Invoke the Lambda Function

Note

Do not invoke the Lambda function from the main thread as it results in a network
call.

The following code shows how to initialize the Cognito Caching Credentials Provider
and invoke a Lambda function. The value for IDENTITY_POOL_ID will be specific to your account. Ensure the region is the same as the Lambda function
you are trying to invoke.