AWS Lambda Function with Kotlin

Download AWS Lambda Function with Kotlin

Post on 23-Jan-2018

263 views

Category:

Software

4 download

Embed Size (px)

TRANSCRIPT

<ul><li><p>Silicon Valley Code Camp </p><p>8 October 2017</p><p>AWS Lambda Function with Kotlin</p></li><li><p>Troy Miles Troy Miles aka the RocknCoder </p><p> Over 38 years of programming experience </p><p> Speaker and author </p><p> bit.ly/rc-jquerybook </p><p> rockncoder@gmail.com </p><p> @therockncoder </p><p> lynda.com Author!</p><p>http://lynda.com</p></li><li><p>Kotlin for Java Developers</p><p>Join me at KotlinConf 2+3 Nov 2017 in San Francisco</p></li><li><p>Agenda Kotlin </p><p> Microservice </p><p> RESTful API </p><p> HTTP Methods </p><p> Amazon Lambda </p><p> Amazon API Gateway </p><p> Spring MVC </p><p> Debugging </p><p> Next Steps</p></li><li><p>A Microservice Is a service with one narrowly defined focused </p><p> Is individually deployable </p><p> Consists of one or more process </p><p> Owns its data store </p><p> Is replaceable</p></li><li><p>Amazon Web Services</p><p> Weve decided to move to AWS </p><p> Two phases: </p><p> Now - Lift and Shift - get it running on EC2 </p><p> Later - Microservices &amp; Client-side - bust it apart </p></li><li><p>Kotlin</p></li><li><p>History</p><p> Announced in 2011 by JetBrains </p><p> Open sourced in 2012 under Apache 2 </p><p> Version 1.0 released February 2016 </p><p> May 2017 Google Kotlin an Android language</p></li><li><p>Features Makes apps for Android, JVM, Browser, and Native </p><p> 100% interoperable with Java </p><p> Clean, succinct syntax </p><p> Null-pointer exception safety </p><p> Lambda functions and other function features</p></li><li><p>AWS Lambda</p></li><li><p>Treat AWS Like Money</p><p> If your account is compromised, it can be very bad </p><p> Criminals can easily charge thousands of $$$ of AWS services to you in minutes </p><p> Never use your root account </p><p> Always use 2FA</p></li><li><p>AWS Lambda Runs code without provisioning servers </p><p> Pay only for compute time used </p><p> Scales automatically </p><p> Uses CloudWatch for monitoring </p><p> Execution environment is Amazon Linux</p></li><li><p>Lambda Pricing: Request</p><p> You are charged for the number of requests + </p><p> You are charged for the duration of the request </p><p> The first 1 million requests are free </p><p> $0.20 per 1 million requests after the first million</p></li><li><p>Lambda Pricing: Duration</p><p> Duration is per second round up to the nearest 100ms </p><p> You are charged $0.00001667 for every GB-second used </p><p> The first 400,000 GB/seconds is free </p><p> Faster and smaller lambdas are cheaper</p></li><li><p>Lambda Languages</p><p>C# No</p><p>Java* 8</p><p>Node.js 4.3</p><p>Node.js 6.10</p><p>Python 2.7</p><p>Python 3.6</p></li><li><p>Kotlin/Lambda Demo</p></li><li><p>AWS API Gateway</p></li><li><p>AWS API Gateway</p><p> Fully managed service for programmatically creating APIs </p><p> Tightly integrates with Lambda </p><p> Combines with CloudFront for edge caching </p><p> Think of it as a flexible front door for your API</p></li><li><p>Kotlin/API Gateway Demo</p></li><li><p>RESTful API Makes use of HTTP methodologies defined by RFC </p><p>2616 protocol </p><p> Preferred over SOAP since it uses less bandwidth </p><p> Breaks down a transaction into a series of HTTP methods </p><p> Stateless by design</p></li><li><p>GET MethodGET /resource</p><p>Request has body No</p><p>Successful response has body Yes</p><p>Safe Yes</p><p>Idempotent Yes</p><p>Cacheable Yes</p></li><li><p>HEAD MethodHEAD /resource (HEAD * )</p><p>Request has body No</p><p>Successful response has body No</p><p>Safe Yes</p><p>Idempotent Yes</p><p>Cacheable Yes</p></li><li><p>POST MethodPOST /resource</p><p>Request has body Yes</p><p>Successful response has body Yes</p><p>Safe No</p><p>Idempotent No</p><p>Cacheable No*</p></li><li><p>PUT MethodPUT /new-resource</p><p>Request has body Yes</p><p>Successful response has body No</p><p>Safe No</p><p>Idempotent Yes</p><p>Cacheable No</p></li><li><p>PATCH MethodPATCH /resource</p><p>Request has body Yes</p><p>Successful response has body No</p><p>Safe No</p><p>Idempotent No</p><p>Cacheable No</p></li><li><p>DELETE MethodDELETE /resource</p><p>Request has body No</p><p>Successful response has body No</p><p>Safe No</p><p>Idempotent Yes</p><p>Cacheable No</p></li><li><p>OPTIONS MethodOPTIONS /resource</p><p>Request has body No</p><p>Successful response has body Yes</p><p>Safe Yes</p><p>Idempotent No</p><p>Cacheable No</p></li><li><p>Spring</p></li><li><p>Spring Framework</p><p> An application framework and IOC container for Java* </p><p> Created by Rod Johnson in 2002 </p><p> Released under Apache 2.0 in June 2003</p></li><li><p>Spring MVC</p><p> Model-View-Controller Web App Framework </p><p> Implements the strategy pattern </p><p> Easier testing than most web frameworks</p></li><li><p>HTTP Method Shortcuts</p><p>Get @GetMapping</p><p>Post @PostMapping</p><p>Put @PutMapping</p><p>Delete @DeleteMapping</p><p>Patch @PatchMapping</p></li><li><p>Head and Options</p><p> @GetMappingand @RequestMapping(method=HttpMethod.GET) support Head transparently </p><p> Options is handled automatically</p></li><li><p>Debugging</p><p> System.out() or System.err() goes to log </p><p> View the logs via CloudWatch </p><p> This is no way to debug code</p></li><li><p>Local Debugging</p><p> Luckily we still run our app locally </p><p> Using a modern IDE, we can set breakpoints</p></li><li><p>It Aint Paradise</p><p> Cold start time can be long </p><p> Not a lot of documentation </p><p> Tooling is rough</p></li><li><p>Next Steps</p><p> Goal is to get compiled size down </p><p> And the execution speed up </p><p> Planning to try RatPack </p><p> Also KotlinNative - </p></li><li><p>Links https://github.com/awslabs/lambda-kotlin-groovy-</p><p>example </p><p> https://www.infoq.com/articles/Ratpack-and-Spring-Boot </p><p> https://aws.amazon.com/blogs/compute/kotlin-and-groovy-jvm-languages-with-aws-lambda/ </p><p> https://github.com/awslabs/aws-serverless-java-container </p><p> https://kotlinlang.org/docs/tutorials/spring-boot-restful.html</p></li></ul>