Transcript

Silicon Valley Code Camp

8 October 2017

AWS Lambda Function with Kotlin

Troy Miles• Troy Miles aka the RocknCoder

• Over 38 years of programming experience

• Speaker and author

• bit.ly/rc-jquerybook

[email protected]

• @therockncoder

• lynda.com Author!

Kotlin for Java Developers

Join me at KotlinConf 2+3 Nov 2017 in San Francisco

Agenda• Kotlin

• Microservice

• RESTful API

• HTTP Methods

• Amazon Lambda

• Amazon API Gateway

• Spring MVC

• Debugging

• Next Steps

A Microservice…• Is a service with one narrowly defined focused

• Is individually deployable

• Consists of one or more process

• Owns its data store

• Is replaceable

Amazon Web Services

• We’ve decided to move to AWS

• Two phases:

• Now - Lift and Shift - get it running on EC2

• Later - Microservices & Client-side - bust it apart

Kotlin

History

• Announced in 2011 by JetBrains

• Open sourced in 2012 under Apache 2

• Version 1.0 released February 2016

• May 2017 Google Kotlin an Android language

Features• Makes apps for Android, JVM, Browser, and Native

• 100% interoperable with Java

• Clean, succinct syntax

• Null-pointer exception safety

• Lambda functions and other function features

AWS Lambda

Treat AWS Like Money

• If your account is compromised, it can be very bad

• Criminals can easily charge thousands of $$$ of AWS services to you in minutes

• Never use your root account

• Always use 2FA

AWS Lambda• Runs code without provisioning servers

• Pay only for compute time used

• Scales automatically

• Uses CloudWatch for monitoring

• Execution environment is Amazon Linux

Lambda Pricing: Request

• You are charged for the number of requests +

• You are charged for the duration of the request

• The first 1 million requests are free

• $0.20 per 1 million requests after the first million

Lambda Pricing: Duration

• Duration is per second round up to the nearest 100ms

• You are charged $0.00001667 for every GB-second used

• The first 400,000 GB/seconds is free

• Faster and smaller lambdas are cheaper

Lambda Languages

C# No

Java* 8

Node.js 4.3

Node.js 6.10

Python 2.7

Python 3.6

Kotlin/Lambda Demo

AWS API Gateway

AWS API Gateway

• Fully managed service for programmatically creating APIs

• Tightly integrates with Lambda

• Combines with CloudFront for edge caching

• Think of it as a flexible front door for your API

Kotlin/API Gateway Demo

RESTful API• Makes use of HTTP methodologies defined by RFC

2616 protocol

• Preferred over SOAP since it uses less bandwidth

• Breaks down a transaction into a series of HTTP methods

• Stateless by design

GET MethodGET /resource

Request has body No

Successful response has body Yes

Safe Yes

Idempotent Yes

Cacheable Yes

HEAD MethodHEAD /resource (HEAD * )

Request has body No

Successful response has body No

Safe Yes

Idempotent Yes

Cacheable Yes

POST MethodPOST /resource

Request has body Yes

Successful response has body Yes

Safe No

Idempotent No

Cacheable No*

PUT MethodPUT /new-resource

Request has body Yes

Successful response has body No

Safe No

Idempotent Yes

Cacheable No

PATCH MethodPATCH /resource

Request has body Yes

Successful response has body No

Safe No

Idempotent No

Cacheable No

DELETE MethodDELETE /resource

Request has body No

Successful response has body No

Safe No

Idempotent Yes

Cacheable No

OPTIONS MethodOPTIONS /resource

Request has body No

Successful response has body Yes

Safe Yes

Idempotent No

Cacheable No

Spring

Spring Framework

• An application framework and IOC container for Java*

• Created by Rod Johnson in 2002

• Released under Apache 2.0 in June 2003

Spring MVC

• Model-View-Controller Web App Framework

• Implements the strategy pattern

• Easier testing than most web frameworks

HTTP Method Shortcuts

Get @GetMapping

Post @PostMapping

Put @PutMapping

Delete @DeleteMapping

Patch @PatchMapping

Head and Options

• @GetMapping and @RequestMapping(method=HttpMethod.GET) support Head transparently

• Options is handled automatically

Debugging

• System.out() or System.err() goes to log

• View the logs via CloudWatch

• This is no way to debug code

Local Debugging

• Luckily we still run our app locally

• Using a modern IDE, we can set breakpoints

It Ain’t Paradise

• Cold start time can be long

• Not a lot of documentation

• Tooling is rough

Next Steps

• Goal is to get compiled size down

• And the execution speed up

• Planning to try RatPack

• Also KotlinNative -

Links• https://github.com/awslabs/lambda-kotlin-groovy-

example

• https://www.infoq.com/articles/Ratpack-and-Spring-Boot

• https://aws.amazon.com/blogs/compute/kotlin-and-groovy-jvm-languages-with-aws-lambda/

• https://github.com/awslabs/aws-serverless-java-container

• https://kotlinlang.org/docs/tutorials/spring-boot-restful.html


Top Related