Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Leveraging Micronaut on AWS Lambda

Slides from my AWS Community Day Bangalore 2019 talk - "Leveraging Micronaut on AWS Lambda".

  • Login to see the comments

  • Be the first to like this

Leveraging Micronaut on AWS Lambda

  1. 1. Leveraging Micronaut on AWS Lambda Naresha K Continuous Delivery Architect | Technical Excellence Coach naresha.k@gmail.com @naresha_k
  2. 2. IaaS PaaS SaaS
  3. 3. IaaS PaaS SaaS FaaS CaaS
  4. 4. IaaS PaaS SaaS FaaS CaaS
  5. 5. ➜ ~ sdk install micronaut https://sdkman.io/
  6. 6. ➜ ~ mn
  7. 7. ➜ ~ mn | Starting interactive mode... | Enter a command name to run. Use TAB for completion: mn>
  8. 8. ➜ ~ mn | Starting interactive mode... | Enter a command name to run. Use TAB for completion: mn> create-function temperature-converter
  9. 9. ➜ ~ mn | Starting interactive mode... | Enter a command name to run. Use TAB for completion: mn>create-function temperature-converter | Generating Java project... | Function created at /Users/naresha/practice/micronaut/ awsdaysblr/temperature-converter | Initializing application. Please wait... mn>
  10. 10. @Getter @Setter @NoArgsConstructor @Introspected public class Temperature { private double inCelcius; private double inFahrenheit; public static Temperature ofCelcius(double celcius) { Temperature temperature = new Temperature(); temperature.setInCelcius(celcius); return temperature; } public static Temperature ofFahrenheit(double fahrenheit) { Temperature temperature = new Temperature(); temperature.setInFahrenheit(fahrenheit); return temperature; } }
  11. 11. @FunctionBean("temperature-converter") public class TemperatureConverterFunction }
  12. 12. @FunctionBean("temperature-converter") public class TemperatureConverterFunction implements Function<Temperature, Temperature> { }
  13. 13. @FunctionBean("temperature-converter") public class TemperatureConverterFunction implements Function<Temperature, Temperature> { @Override public Temperature apply(Temperature input) { return Temperature.ofFahrenheit(input.getInCelcius() * 9 / 5 + 32); } }
  14. 14. Deploying to AWS Lambda
  15. 15. ➜ temperature-converter ./gradlew assemble
  16. 16. ➜ temperature-converter ./gradlew assemble > Task :compileJava Note: Creating bean classes for 1 type elements BUILD SUCCESSFUL in 5s 10 actionable tasks: 8 executed, 2 up-to-date ➜ temperature-converter
  17. 17. ➜ temperature-converter ./gradlew assemble > Task :compileJava Note: Creating bean classes for 1 type elements BUILD SUCCESSFUL in 5s 10 actionable tasks: 8 executed, 2 up-to-date ➜ temperature-converter echo '{"inCelcius": 37}' | java -jar ./build/libs/ temperature-converter-0.1-all.jar
  18. 18. ➜ temperature-converter ./gradlew assemble > Task :compileJava Note: Creating bean classes for 1 type elements BUILD SUCCESSFUL in 5s 10 actionable tasks: 8 executed, 2 up-to-date ➜ temperature-converter echo '{"inCelcius": 37}' | java -jar ./build/libs/ temperature-converter-0.1-all.jar {"inCelcius":0.0,"inFahrenheit":98.6}
  19. 19. ➜ temperature-converter ./gradlew deploy > Task :deploy Function not found: arn:aws:lambda:us-east-1:341512844459:function:temperature- converter-mn-java-2 (Service: AWSLambda; Status Code: 404; Error Code: ResourceNotFoundException; Request ID: 0c789b90-e936-4663-ad68-2037c1a3b6ef) Creating function... temperature-converter-mn-java-2 BUILD SUCCESSFUL in 57s 4 actionable tasks: 1 executed, 3 up-to-date ➜ temperature-converter
  20. 20. @FunctionBean("temperature-converter") public class TemperatureConverterFunction extends FunctionInitializer implements Function<Temperature, Temperature> { @Override public Temperature apply(Temperature input) { return Temperature.ofFahrenheit(input.getInCelcius() * 9 / 5 + 32); } public static void main(String... args) throws IOException { TemperatureConverterFunction function = new TemperatureConverterFunction(); function.run(args, (context) -> function.apply(context.get(Temperature.class))); } }
  21. 21. package com.nareshak.demo Temperature temperatureConverter(Temperature input) { new Temperature(inFahrenheit: input.getInCelcius() * 9 / 5 + 32) }
  22. 22. package com.nareshak.demo import groovy.transform.Field import javax.inject.Inject @Field @Inject TemperatureConveterService temperatureConveterService Temperature temperatureConverter(Temperature input) { new Temperature(inFahrenheit: temperatureConveterService .celciusToFahrenheit(input.inCelcius)) }
  23. 23. package com.nareshak.demo import javax.inject.Singleton @Singleton class TemperatureConveterService { double celciusToFahrenheit(double celcius) { celcius * 9 / 5 + 32 } }
  24. 24. Testing Functions
  25. 25. package temperature.converter; import io.micronaut.test.annotation.MicronautTest; import org.junit.jupiter.api.Test; import javax.inject.Inject; import static org.junit.jupiter.api.Assertions.assertEquals; @MicronautTest public class TemperatureConverterFunctionTest { @Inject TemperatureConverterClient client; @Test public void testFunction() throws Exception { Temperature body = Temperature.ofCelcius(37.0); assertEquals(98.6, client.apply(body).blockingGet().getInFahrenheit()); } }
  26. 26. package com.nareshak.demo import io.micronaut.test.annotation.MicronautTest import spock.lang.Specification import javax.inject.Inject @MicronautTest class TemperatureConverterFunctionSpec extends Specification { @Inject TemperatureConverterClient client void "test temperature-converter-groovy function"() { given: Temperature temperature = new Temperature(inCelcius: 37) expect: client.convertTemperature(temperature).blockingGet().inFahrenheit == 98.6 } }
  27. 27. Function Client
  28. 28. @FunctionClient interface TemperatureConverterClient { @Named("temperature-converter") Single<Temperature> convertTemperature(Temperature input) }
  29. 29. micronaut: application: name: clientapp --- aws: lambda: functions: convertTemperature: functionName: temperature-converter region: us-east-1
  30. 30. @Controller class MainController { @Inject TemperatureConverter temperatureConverter @Get("/") String index() { def temperature = new Temperature(inCelcius: 37) Temperature result = temperatureConverter .convertTemperature(temperature) .blockingGet() result.toString() } }
  31. 31. Compile-time dependency injection => less runtime overhead
  32. 32. https://github.com/naresha/awscommunitydayblr_micronaut_faas
  33. 33. Thank You

×