Coder Social home page Coder Social logo

mrdjen / aws-lambda-cdk-plain Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adambien/aws-lambda-cdk-plain

0.0 0.0 0.0 57 KB

Simplest Possible AWS Lambda Function with Cloud Development Kit (CDK) Boilerplate

License: MIT License

Shell 5.82% Java 94.18%

aws-lambda-cdk-plain's Introduction

Simplest Possible AWS Lambda Function with Cloud Development Kit (CDK) Boilerplate

A lean starting point for building, testing and deploying AWS Lambdas with Java.

TL;DR

A simple Java AWS Lambda without any AWS dependencies:

public class Greetings{

    public String onEvent(Map<String, String> input) {
        System.out.println("received: " + input);
        return input
        .entrySet()
        .stream()
        .map(e -> e.getKey() + "->" + e.getValue())
        .collect(Collectors.joining(","));
    }
    
}

...deployed with AWS Cloud Development Kit:

import software.amazon.awscdk.services.lambda.Code;
import software.amazon.awscdk.services.lambda.Function;
import software.amazon.awscdk.services.lambda.Runtime;

//...

Function createUserListenerFunction(String functionName,String functionHandler, int memory, int timeout) {
    return Function.Builder.create(this, id(functionName))
            .runtime(Runtime.JAVA_11) //https://aws.amazon.com/corretto
            .code(Code.fromAsset("../target/function.jar"))
            .handler(functionHandler)
            .memorySize(memory)
            .functionName(functionName)
            .timeout(Duration.seconds(timeout))
            .build();
}

...provisioned with maven and cdk:

mvn clean package
cd cdk && mvn clean package && cdk deploy

...and (blackbox) tested with AWS SDK for Java 2.x:

@BeforeEach
public void initClient() {
    var credentials = DefaultCredentialsProvider
    .builder()
    .profileName("airhacks.live")
    .build();

    this.client = LambdaClient.builder()
                   .credentialsProvider(credentials)
                   .build();
}

@Test
public void invokeLambdaAsynchronously() {
        String json = "{\"user \":\"duke\"}";
        SdkBytes payload = SdkBytes.fromUtf8String(json);

        InvokeRequest request = InvokeRequest.builder()
                .functionName("airhacks_lambda_greetings_boundary_Greetings")
                .payload(payload)
                .invocationType(InvocationType.REQUEST_RESPONSE)
                .build();

        var response = this.client.invoke(request);
        var error = response.functionError();
        assertNull(error);
        var value = response.payload().asUtf8String();
        System.out.println("Function executed. Response: " + value);
}    

In Action

Plain Java POJOs as AWS Lambdas

Java "vs." JavaScript

Cold and "warm" starts of JavaScript and Java Lambdas:

Java vs. JavaScript comparison

AWS Lambda on Java: How Good / Bad Is The Cold Start?

Coldstart with Java

Lambda Configuration

AWS Lambda Configuration with Java CDK

References

The deployment is borrowed from: "Slightly Streamlined AWS Cloud Development Kit (CDK) Boilerplate"

aws-lambda-cdk-plain's People

Contributors

adambien avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.