Tag Archives: amazon

Simple AWS Lambda Java Project – NetBeans and Gradle


I have been working on distributed harvesting projects for the last 5 years. Generally the projects used a raft of AWS EC2 instances and Gearman to manage the workflow. Having recently heard of some similar solutions using AWS Lambda functions, I wanted to explore this as a possible new architecture for these projects. So.. I dug in.

Article Contents:

The learning curve for me has been a little steep. There is a limited number of languages supported (AWS doc linked here), but the only two that came close to meeting my needs were NodeJS and Java.

Most of my project code is written in CasperJS, a NodeJS extension, so I thought that perhaps that was the path of least resistance.. but it was not clear to me how to ‘install’ PhantomJS and CapserJS components into this Lambda Function, so I switched to working with Java.

Designing the Test

This is a VERY simple test blob, but it took me about a day to get to this point, since it was not totally clear to me how the AWS API Gateway might work and how the data is packaged/posted to the <Lambda Function. After a lot of poking around, uploading my first hunk of (totally broken) code; I was presented the a test CLI. The test CLI has only a few options or packaging up data payloads (at least for test), singular scalar string and a JSON string (which turns into an Object.. the source of the frustration I faced figuring this out).

Boiling it down, this is what the test harness input looks like, when you use the “Hello World” test case, after changing the properties to match my desired inputs:

This will work fine for my needs, I prefer JSON payloads over a fixed (brittle) set of parameters.. this lets me muck around with the internals and add more capacity without having to work about external inputs; I’m just that way.

Having decided that JSON was the payload to accept.. next I needed to write a chunk of code to accept it.

Writing the Test Code

Knowing that I would be using a Json string as my input, I’d first coded my function like this:

public String searchAuto(String json){
return String.format(“%s”,json);
}

What I discovered quickly is that the string of JSON in the test payload (which looks like this), is deserialized as a Json Object, and not a string. This required me to step up my simple test to actually deal with the data as JSON. To do this, I selected simple.JsonObject. This is where the “fun” began. Hopefully this helps you avoid spending the time I did to figure out how to get the code cleanly compile locally.. AND to run when uploaded to AWS.

NOTE: I’m not going to say this is the right way to do this.. it’s just the way it worked for me. It’s kludgy and I don’t really like having a two step process but it’s working to get my version of HELLO WORLD working.

Long story short, I needed to get copies of these three .jar files added to my project’s build path, to get things going. Where you place them in your project might vary, but in a basic NetBeans project, these are in the lib/ directory off the project root:

ls -l lib/*.jar

7,267 lib/aws-lambda-java-core-1.1.0.jar
75,355 lib/aws-lambda-java-events-2.0.jar
57,607 lib/json-simple-2.1.2.jar

Locating the jars was not the simplest task either, Amazon’s horrible documentation sent me fishing around on various Github repos, but in the end.. I found them with a good ol search. I’ve linked the libs in the above block to where I sourced mine.

AWS Lambda requires use of specific handler hooks, when initiating the function. I’d like to wax poetic about what it all means but at this time, I just need a proof of concept, so using a code example.. this is the code I compiled:

package lambdaTest;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import org.json.simple.JsonObject;

/**
*
* @author david
*/

public class LambdaTest {

public String myHandler(int myCount, Context context) {
LambdaLogger logger = context.getLogger();
logger.log(“received : ” + myCount);
return String.format(“Received value of %d”,myCount);
//String.valueOf(myCount);
}

/* search options */
public String searchAuto(JsonObject json){

return String.format(“Search Region [%s] for ‘%s'”,json.getString(“region”),json.getString(“query”));
}
}

Beyond the Build – Packaging your Java

Again, the think the AWS documentation is pretty crappy, and this extends to every AWS service I’ve had to use. Once you figure out what I really means, they work very well.. but decoding Seattle AWS-speak (for me) is often not easy. You might be smarter than me and able to see the path right off the bat.. well.. good for you. For me and the rest of the inelastic brain crowd.. I’ll try to give you the Cliff Notes version…

When I simply wanted to test my JAR, and I didn’t need the JSON libary, it was a simple matter of uploading the Jar file in the Lambda dialog; it looks like this:

However, uploading more than 1 jar, things start to get a little wonky. The instructions say this:

Example Gradle Config
The page also provides a rather extensive rundown on using Gradle to run your build and make the package. Since creating my own .ZIP with the files in the ‘root’ as designated didn’t work, I went Gradle. If you decide to go Gradle, here is the build file I ended up using. When I made the mistake of including the Mavin paths for AWS into the Gradle build, I ended up with ALL if the AWS .jar files being downloaded and zipped into the library, creating a 7.8 MB upload!! For a Hello World program.. INSANE! Anyhow.. this is my gradle config that changes the default Gradle configs (build.gradle text file) to work with NetBeans paths. You might need to tweak this further to your own needs:

apply plugin: ‘java’

//repositories {
// mavenCentral()
//}

sourceSets {
main {
java {
srcDir ‘src’
}
}
test {
java {
srcDir ‘test’
}
}
}

dependencies {
compile (
fileTree(dir: ‘lib’, include: ‘*.jar’)
)
}

task buildZip(type: Zip) {
from compileJava
from processResources
into(‘lib’) {
from configurations.runtime
}
}

build.dependsOn buildZip

Using Gradle to run the build looks like this, run the command gradle build and you are on your way… as long as your code has a clean compile:

gradle build

BUILD SUCCESSFUL in 1s
3 actionable tasks: 3 executed

This allowed me to build a ZIP file that uploaded and worked. What I found out, was that the structure of the file is NOT really the way it was described in the docs. The contents of the zip file look like this for my project; note that when uploading this way, you are *not* packaging the .jar file for your class, but the compiled class file. Also not that it is *not* in the root of the path. With this knowledge, I can probably dispense with dual-building with Gradle and just package up the contents in this way. Hack the process!

Lambda/lambdaTest:
1612 Sep 29 15:18 LambdaTest.class

Lambda/lib:
7267 Sep 26 10:40 aws-lambda-java-core-1.1.0.jar
75355 Sep 26 10:13 aws-lambda-java-events-2.0.jar
57607 Sep 26 18:30 json-simple-2.1.2.jar

Now, you should be able to upload your zip file. Not that a file of 10MB or more, they’d like you to spend extra money storing it on S3… (niiiiice).

Testing your Function

Once uploaded, you can configure your test…

… and run it!

Conclusion

After a day of putting around and getting frustrated with the AWS docs that always leave (for me) gaping knowlege gaps.. I have a proof of concept test coded, compiled, uploaded and run. Now.. what needs to be sorted out is how to setup the AWS API Gateway to trigger this function… and look at how much each of those iterations will cost. With a planned execution batch that will range from 40-60 events per query.. this could add up fast.

So….. I’m not yet convinced this will function economically for my latest project; but I’m going to be testing out the concept pretty thoroughly over the coming week.


AWS EC2 Sendmail Configuration (with .procmail)

Sendmail.. how you frustrate people; but the power makes it difficult to turn away. I’ll now detail the steps required for me to setup a simple e-mail auto-processing service on an AWS EC2 instance without a dedicated hostname.

Setting up the Procmail Recepie

.procmail — you can do a lot of things with .procmail. The online resources are many, but for this example I’m going to keep it VERY simple.

First, create a procmail file in your local account. My AWS instances are CentOS based, and use the ‘ec2-user’ as the default account. I’m going to keep it simple here and stick with that paradigm.

-bash-4.1$ vi .procmailrc

I’m going to setup my .procmailrc file to look like this:

SHELL=/usr/bin/php
MAILDIR=$HOME/mail
LOGFILE=$HOME/logs/procmail.log

:0 # catch errors
* ^Subject: Returned mail:.*
logs/procmail.error.log

# — auto-catches
:0
|”$HOME/prod/MailIntake/process.mail.php” $1

Now that I have a .procmail setup… time to get down to making sendmail work.

Sendmail — allowing server to accept messages

To configure the sendmail files, I assume super user powers. If you are unable to assume superuser powers or run sudo.. I doubt you’ll be able to complete these configurations. Hopefully your responsible IT person is going to handle all this for you instead.

Main Sendmail File — sendmail.mc

Setting up the main Sendmail file. This file is fairly large, so I’m only going to highlight the sections I felt needed to be updated.

vi sendmail.mc

To allow use of the AWS mail relay, defined in this section:

define(`SMART_HOST’, `email-smtp.us-east-1.amazonaws.com’)dnl

Setup local hostname / domain identity

dnl # Also accept email sent to “localhost.localdomain” as local email.
dnl #LOCAL_DOMAIN(`localhost.localdomain’)dnl
LOCAL_DOMAIN(`ec2-52-6-000-000.compute-1.amazonaws.com’)dnl

Setting up the masquerade

MASQUERADE_DOMAIN(`ec2-52-6-000-000.compute-1.amazonaws.com’)dnl
MASQUERADE_AS(`ec2-52-6-000-000.compute-1.amazonaws.com’)dnl

access db configuration

Editing the access file to setup the local host relay, so messages can be sent from the various network interfaces on the machine. Obviously. one of those IP addresses was obscured. Where you see #private ip# substitute your AWS private IP (such as 172.123.321.1)

## By default we allow relaying from localhost…
localhost RELAY
127.0.0.1 RELAY
#private ip# RELAY
email-smtp.us-east-1.amazonaws.com RELAY

## Allowed Connections
Connect:127.0.0.1 OK
Connect:#private ip# OK
Connect:email-smtp.us-east-1.amazonaws.com OK

Defining Local Hostnames — local-host-names

For my configuration, I wanted to make sure the system understood it’s local non-FQDN identitiy, so I edited the local-host-names file to include three different ways to reference the system. The AWS DSN, public IP and private IP:

vi local-host-names

Contents of my file looks like this (my file contains real IPs and hostname)

# local-host-names – include all aliases for your machine here.
ec2-52-6-000-000.compute-1.amazonaws.com
52.6.000.000
172.30.000.000

The Mailer Table — mailertable

At this point I didn’t see a need to implement functions of the mailertable

Setting up trusted user file — trusted-users

Modified the trusted users file to allow my primary user, root and one alias to send mail without warnings:

vi /etc/mail/trusted-users

File contents:

# trusted-users – users that can send mail as others without a warning
# apache, mailman, majordomo, uucp, are good candidates
ec2-user, apps, proxy, root

Aliases — virtusertable

Entering user aliases to capture mail sent to various users, and route them to the local ‘ec2-user’.

vi /etc/mail/virtusertable

Contents of my file with aliases:

# A domain-specific form of aliasing, allowing multiple virtual domains to be
# hosted on one machine.
#
ec2-user@ec2-52-6-000-000.compute-1.amazonaws.com ec2-user@localhost
stuff@ec2-52-6-000-000.compute-1.amazonaws.com ec2-user@localhost
things@ec2-52-6-000-000.compute-1.amazonaws.com ec2-user@localhost

Rebuild Settings

Run a make on the directory to rebuild the sendmail db files

make -C /etc/mail
make: Entering directory `/etc/mail’
make: Leaving directory `/etc/mail’

Restart Sendmail!

Restart sendmail… watch for majik!

/etc/init.d/sendmail restart
Shutting down sm-client: [ OK ]
Shutting down sendmail: [ OK ]
Starting sendmail: [ OK ]
Starting sm-client: [ OK ]

Amazon AWS – spinning up a micro for Gearman Client

Walkthough for spinning up a micro EC2 for use as a GearMan worker node.

This is a preliminary effort, and the steps may very well change over the next few weeks as these are tested out. The documentation I’m presenting here is based on my previous work [HERE] Install Gearman + Gearman-PHP on AWS ec2.

Getting Started

This assumes you already have an AWS account setup. If you don’t you need to go do that now. You can get started [HERE].

Once logged in, go to your dashboard. From here you’ll be selecting the EC2 area.
Screen Shot 2013-06-17 at 10.26.23 AM

Under resources you’ll find a button “Launch Instance”. This is the button you want to click. This is where the fun begins.
Screen Shot 2013-06-17 at 10.27.57 AM

In this case I’m going to use the ‘Classic Wizard’. It’s really not that magical but Wizard is such a super-awesome-cool-name (ala Windoze ’95) you’ll see it used here. Anyhow, I’m going classic:
Screen Shot 2013-06-17 at 10.30.04 AM

I want to keep things EVERY simple here so I’m going use the default Amazon AWS distribution/AMI.
Screen Shot 2013-06-17 at 10.31.52 AM

For this exercise, I’m using an ‘On Demand’ Micro instance:
Screen Shot 2013-06-17 at 10.34.01 AM

No Advanced features should be required, so I’ve left everything on this page set to it’s default settings:
Screen Shot 2013-06-17 at 10.36.20 AM

Next the storage requirements are defined. Since this is a worker that should be able to be spun up on need, and shouldn’t require much in the way of local storage, I’m going to opt out of defining and EBS volume and rely upon Ephemeral storage.
Screen Shot 2013-06-17 at 10.40.43 AM

At this point, I don’t see the need to define any keys for EC2 management, so I’m leaving this area blank:
Screen Shot 2013-06-17 at 10.41.30 AM

Next, select the .ssh key file (it’s stored in a .pem file) that you want to use to access this system. If you don’t have a set of these keys setup already, you’ll want to define them. I’m using one specific to this node class already defined.. you’ll need to handle this step as your policy/needs dictate. It’s not that complex, but word to you, DOWNLOAD THAT KEY, once you create it, there is no known way (according to all places I’ve checked) to download it again. BE WARNED.
Screen Shot 2013-06-17 at 10.47.16 AM

Next step will be do select a security profile for your node. I’ve found that the default one is sufficient for these purposes. You may want to further restrict the number of ports open, as the default opens a few extra things you might not want. This is another area where you’re own needs an policy will need to be carefully considered. Otherwise, start with default and iterate to the optimal configuration.
Screen Shot 2013-06-17 at 10.52.02 AM

Check your settings on this review page, and if everything is to your preference, then you can spin it up!!
Screen Shot 2013-06-17 at 10.55.38 AM

One you click launch, it will take a little while for the instance to go live.
Screen Shot 2013-06-17 at 10.57.20 AM

Once launched, you’ll be able to see your instance in the dashboard! Sorry bout all the greyd out information, but the instance I’m talking about is slightly highlighted in blue.
Screen Shot 2013-06-17 at 11.00.22 AM

NOW, YOU CAN START TO INSTALL YOUR GEARMAN COMPONENTS