AWS Lambda part 4 – running from Android

The power coming from AWS Lambdas is clear when you think what and who can invoke it.  You can run it obviously from AWS Console.  But the real value of this solution is clear when you can use your exisiting microservice from the device or hitting some HTTP endpoint.  The last time me using Android is when there was no Intellij and emulators were heavier than my PC could endure.

Let’s start Android

Android Studio made an easy start to my first Android activity.  When I typed : Android project, and chose the first Android SDK that came to my mind – the window and project with many files predefined was created for me.  The blank activity was ready to be run and tried on my device!  Gradle-backed app and device deploy scritps works so fast comparing to the previous versions.

I played a little with basic components and prepared easy input/output operations like reading inputs and displaying outputs on Snacks.

Works! So why not to connect to the Amazon now.

Android SDK

To work with Amazon from Android you are to use SDK.  Amazon gives you nice tutorial that can be found there.

Using whole variety of factories is available just after declaring dependencies on your project. Please, have a look at Gradle snippet from properties of my project

Basics of Lambda Android

To run the app you have to care about all permission on Android and AWS backend (mentioned later on).  Then, you have to decide about what function will be invoked using @LambdaFunction annotation.  Types of the event are to be sent using simple POJO object with getters and setters.

Then this function can be utilized with Invoker like here

That’s all.  Now I will focus on some permissions.

Permissions

Android permissions

Project can be run when all the policies will be satisfied and you will be safely guarded by all of the Android and AWS Security officials.  To develop the sample Android invoker application the biggest issue was to satisfy all of the safety issues.

In Android app when using API 23 or Android version 6+ there is not enough to manifest all of the permissions as it was done previously (manifest file) but permission can / should be requested on runtime. Special permissions are needed, because Amazon SDK uses Internet domain.

AWS permissions

To use the Lambda from the outside I have play with Cognito roles.  I was forced to create the federated policy with no authorization from the outside.  Then this cognity pool id is passed to the invoker factory.

I was so happy when finally my Lambda was executed from within the Android application.  The Android context tests were good help for me, f.e. when invoking test like this below it is executed on emulator or your device and you are sure that this part of the flow will work, integration is tested here.

TL;DR

I was happy to create sample 1-activity Android application and invoke the AWS Lambda function with parameters.  I can easily record all of my spendings using my cell phone. All of the progress is available on my GITHUB project HERE.