Giter Site home page Giter Site logo

springboot-serverles-with-aws-lambda's Introduction

Library SpringBoot - AWS Lambdas Functions

This repository uses Java, SpringBoot with AWS Lambdas Functions

  • An AWS Lambda Function is required to run the test/app

  • To use a Lambda Function is necessary to update the AWS properties in the files src/main/resources/application.properties and src/main/resources/application-dev.properties

      #AWS Keys
      amazon.aws.accesskey=<KEY>
      amazon.aws.secretkey=<SECRET_KEY>
      amazon.aws.region=<REGION>
      amazon.aws.amazonAWSFunctionARN=<FUNCTION_RESOURCE_NAME>
    
  • To test execute ./mvnw clean test

  • To run the app execute ./mvnw spring-boot:run

  • Rest endpoints

The code

In this repository to use AWS Lambdas in Java, it is necessary to add some dependencies, create a Lambda Client class

Dependencies (pom.xml file)

com.amazonaws::aws-java-sdk-s3
software.amazon.awssdk:lambda

The AWS Lambda Client class (org.hillmerch.library.service.NotificationClient)

A LambdaClient is created using AWS Account keys

	LambdaClient awsLambda = LambdaClient.builder()
    				.credentialsProvider( StaticCredentialsProvider.create( AwsBasicCredentials.create( amazonAWSAccessKey, amazonAWSSecretKey ) ) )
    				.region( region ).build();

Then, the payload with the parameters is created

SdkBytes payload = SdkBytes.fromUtf8String(
			"{ \"message\": " +
					"{\n" +
					" \"name\": \"" + name + "\",\n" +
					" \"phone\": \"" + phone + "\",\n" +
					" \"message\": \"" + message + "\"\n" +
					"}"
					+ "}" );

Then, an InvokeRequest using the function name, the parameters are send with the method payload

	InvokeRequest request = InvokeRequest.builder()
			.functionName( amazonAWSFuntionARN )
			.payload( payload )
			.build();

Finally, the Lambda function is invoked

	InvokeResponse res = awsLambda.invoke( request );

The Lambda Function

This example uses a Function created with Node, in this case the function return a string

exports.handler = async (event) => {
    const response = {
        statusCode: 200,
        body: JSON.stringify('Message sent: ' + event.message.name + '  ' + event.message.phone + ' ' + event.message.message),
    };
    return response;
};

springboot-serverles-with-aws-lambda's People

Contributors

hillmerch avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  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.