Java with AWS Lambda Functions

Mon, Aug 14, 2017 3-minute read

In this post we’ll build an AWS Lambda function and then open it up to the outside world through the AWS API Gateway service.

The starting point is a Maven Java project.

With the project in place, we add the Maven dependency it needs.

<dependencies>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-core</artifactId>
      <version>1.1.0</version>
    </dependency>
  </dependencies>

Next comes a class, which I named Ornek.java.

package ornekgroup.lambdaornek1;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;

public class Ornek {
        public String myHandler(String postVeri, Context context) {
        LambdaLogger logger = context.getLogger();
        logger.log("gelenVeri : " + postVeri+"\n");
        return "Merhaba " + postVeri;
    }
}

Once the code is ready, we build the project.

With the artifact in hand, we move over to the AWS side.

The first thing we do there is create a Lambda function from scratch, without any trigger attached.

Then we point it at our function.

And with that, the function is created.

The next thing to do is test it. For a quick sample run, I send the data “Fahri”, and the test comes back successful.

With the function working, we move on to the API Gateway part.

Here we create a new resource and, inside it, select “Create New Method”.

For the method I went with POST, though AWS API Gateway will let you use GET, PUT, OPTIONS, and others just as well.

I test the API Gateway side too: I wrote “webischia” in the POST body, and this test is successful as well.

Finally we deploy this API Gateway, and then I give the method a try from Postman.

– Turkish Version –

AWS Lambda Functions ile Java

Bu yazımızda bir AWS Lambda fonksiyonu oluşturup, bunu AWS API Gateway servisi üzerinden dışarı açacağız.

Başlangıç noktamız bir Maven Java projesi.

Proje hazır olunca ihtiyaç duyduğu Maven dependency’sini ekliyoruz.

<dependencies>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-core</artifactId>
      <version>1.1.0</version>
    </dependency>
  </dependencies>

Ardından bir class oluşturuyoruz; ben isim olarak Ornek.java verdim.

package ornekgroup.lambdaornek1;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;

public class Ornek {
        public String myHandler(String postVeri, Context context) {
        LambdaLogger logger = context.getLogger();
        logger.log("gelenVeri : " + postVeri+"\n");
        return "Merhaba " + postVeri;
    }
}

Kod hazır olduğunda projemizi build ediyoruz.

Çıktı elimizde olunca AWS kısmına geçiyoruz.

Orada ilk yaptığımız şey, hiçbir trigger eklemeden sıfırdan bir lambda functions oluşturmak.

Daha sonra fonksiyonumuzu belirtiyoruz.

Ve böylece fonksiyon yaratılmış oluyor.

Sırada test etmek var. Hızlı bir örnek olarak “Fahri” verisini gönderiyorum, ve test başarılı geliyor.

Fonksiyon çalıştığına göre API Gateway kısmına geçiyoruz.

Burada yeni bir resource oluşturup içinde “Create New Method” seçiyoruz.

Method olarak ben POST seçtim; AWS API Gateway GET, PUT, OPTIONS vb. methodlara da izin veriyor.

API Gateway kısmını da test ediyorum: POST body kısmına webischia yazdım, ve bu test de başarılı.

Son olarak bu API Gateway’i deploy ediyoruz, ve ardından methodu Postman aracılığıyla deniyorum.