<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Rui Teixeira</title>
    <description>The latest articles on Forem by Rui Teixeira (@rmpt).</description>
    <link>https://forem.com/rmpt</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1014512%2F383b44c1-f9dd-417a-99b2-22e235fab846.png</url>
      <title>Forem: Rui Teixeira</title>
      <link>https://forem.com/rmpt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rmpt"/>
    <language>en</language>
    <item>
      <title>AWS Lambda + Kotlin</title>
      <dc:creator>Rui Teixeira</dc:creator>
      <pubDate>Thu, 26 Jan 2023 11:54:15 +0000</pubDate>
      <link>https://forem.com/rmpt/aws-lambda-kotlin-4ich</link>
      <guid>https://forem.com/rmpt/aws-lambda-kotlin-4ich</guid>
      <description>&lt;h2&gt;
  
  
  Run AWS Lambda with Kotlin
&lt;/h2&gt;

&lt;p&gt;Lambda is a buzz word nowadays, for good reasons: simple, fast and pay as you go.&lt;br&gt;
Majority of lambda functions examples are written in TypeScript and, for the JVM side, Java only. That's why I'm creating this example, to anyone interested in running a Kotlin function on AWS.&lt;br&gt;
I'll be using the AWS CLI only, not the AWS online console.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create the kotlin project – gradle
&lt;/h2&gt;

&lt;p&gt;First of all we need to create our kotlin project. We'll use Gradle to do it, but translate it to Maven should be trivial.&lt;br&gt;
Create a folder wherever you want, navigate to it in the command shell and execute the following:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gradle init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After that, select the following options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select type of project to generate: &lt;code&gt;library&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;    Select implementation language: &lt;code&gt;Kotlin&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;    Select build script DSL: &lt;code&gt;Groovy&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;    Generate build using new APIs and behavior 
(some features may change in the next minor release): &lt;code&gt;no&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Dependencies
&lt;/h2&gt;

&lt;p&gt;To run our function on AWS we need to add some dependencies to our project.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;implementation 'com.amazonaws:aws-lambda-java-core:1.2.2'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Apart from AWS specific, we'll need a fat jar, so we need to add a &lt;strong&gt;plugin&lt;/strong&gt; to generate it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;id "com.github.johnrengelman.shadow" version "7.1.2"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's all we need to have our proof of concept.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create the actual Kotlin function
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;I'm using IntelliJ IDE&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Our Lambda function will be very simple:&lt;br&gt;
Implement an echo function, i.e., it will return the received input.&lt;/p&gt;

&lt;p&gt;Let's create our main Class. I'll create the package &lt;code&gt;com.ruimptech.echo&lt;/code&gt; inside &lt;code&gt;src/main/kotlin&lt;/code&gt;. Inside that package I'll create the main kotlin class, named &lt;code&gt;App&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6rflo93lw0gpc6ddrpuz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6rflo93lw0gpc6ddrpuz.png" alt="Project structure" width="268" height="106"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to run in AWS, our main class needs to implement the &lt;code&gt;RequestHandler&lt;/code&gt; interface.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpottxa4c5lz1nhfmok97.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpottxa4c5lz1nhfmok97.png" alt="Implementing RequestHandler interface" width="500" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the image, there's an error, that's because the RequestHandler interface requires to specify the input and output data type. To fix that, we'll create a class for input and another for output and use them to fix the interface error.&lt;/p&gt;
&lt;h3&gt;
  
  
  Input class
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.ruimptech.echo

class EchoIn {
    var content: String = ""
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Receives a simple content String.&lt;br&gt;
Example: &lt;code&gt;{ "content": "hello" }&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Output class
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.ruimptech.echo

data class EchoOut(val content: String)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Very similar to the input class, contains the output content String, that in our proof of concept will be the received input.&lt;/p&gt;

&lt;p&gt;Note that our &lt;code&gt;EchoIn&lt;/code&gt; class is &lt;strong&gt;not&lt;/strong&gt; a data class, that's because a data class will not work on AWS. If we use a data class as the input, lambda execution will terminate immediately due to a initialization error.&lt;/p&gt;

&lt;p&gt;Now that we have input and output classes, we can fix our lambda function (App class).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.ruimptech.echo

import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.LambdaLogger
import com.amazonaws.services.lambda.runtime.RequestHandler

class App : RequestHandler&amp;lt;EchoIn, EchoOut&amp;gt; {
    override fun handleRequest(input: EchoIn, context: Context): EchoOut {
        val logger: LambdaLogger = context.logger
        logger.log("ECHO INPUT RECEIVED: " + input.content)
        return EchoOut("Echo: ${input.content}")
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, the function is ready! Let's go over what is going on:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;RequestHandler&amp;lt;EchoIn, EchoOut&amp;gt;&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;We say to our lambda function that EchoIn and EchoOut are the input and output data types.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;override fun handleRequest&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;The actual code to be run on each incoming request&lt;/li&gt;
&lt;li&gt;As mentioned before, the function is very simple. We print a log with the received input and return it inside a EchoOut instance.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Create a fat jar
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;com.github.johnrengelman.shadow&lt;/em&gt; plugin allow us to create a fat jar with a simple command:&lt;br&gt;
&lt;code&gt;./gradlew shadowJar&lt;/code&gt;&lt;br&gt;
As a result, we'll find a file named &lt;strong&gt;lib-all.jar&lt;/strong&gt; inside the &lt;code&gt;build/libs&lt;/code&gt; directory, that's our fat jar.&lt;/p&gt;
&lt;h2&gt;
  
  
  Deploy the lambda function
&lt;/h2&gt;

&lt;p&gt;We have everything in place in our computer, now let's send it to AWS.&lt;br&gt;
As any AWS deploy, we need a template to specify what we want to do. We'll use an existing AWS template (you can find the original version &lt;a href="https://github.com/awsdocs/aws-lambda-developer-guide/blob/main/sample-apps/java-basic/template.yml" rel="noopener noreferrer"&gt;here&lt;/a&gt;) and tweak some properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: Kotlin Lambda Function
Resources:
  function:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: build/libs/lib-all.jar
      Handler: com.ruimptech.echo.App::handleRequest
      Description: Kotlin function
      Runtime: java11
      MemorySize: 512
      Timeout: 10
      # Function's execution role
      Policies:
        - AWSLambdaBasicExecutionRole
        - AWSLambda_ReadOnlyAccess
        - AWSXrayWriteOnlyAccess
        - AWSLambdaVPCAccessExecutionRole
      Tracing: Active
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main changed parameters are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CodeUri

&lt;ul&gt;
&lt;li&gt;Where is the fat jar to send to AWS&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Handler

&lt;ul&gt;
&lt;li&gt;What is the function to run&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Description&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Save this template in a file named &lt;code&gt;template.yml&lt;/code&gt;, place it in the lib folder of the project:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fscfc221zir7l77lggzev.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fscfc221zir7l77lggzev.png" alt="Lambda template file location" width="231" height="86"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;From that point on, I assume you have an AWS account and the CLI is correctly configured in your computer. If not, please check &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html" rel="noopener noreferrer"&gt;this&lt;/a&gt; out&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, to deploy the function we need 3 steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a S3 bucket where the final template will be stored (the template.yml file we just created is a template to generate the final template, don't worry too much about it)&lt;/li&gt;
&lt;li&gt;Generate the final template and save it on the bucket created before&lt;/li&gt;
&lt;li&gt;Deploy the final template&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These 3 steps are directly converted to 3 CLI commands:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;aws s3 mb s3://lambda-kotlin&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;aws cloudformation package –template-file template.yml –s3-bucket lambda-kotlin –output-template-file template-out.yml&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Based on our template.yml, the CLI will generate the final template - template-out.yml - that we can use in the final step&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;aws cloudformation deploy –template-file template-out.yml –stack-name lambda-kotlin –capabilities CAPABILITY_NAMED_IAM&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Use the final template to deploy the lambda function&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That' it! Our Kotlin Lambda function is on AWS!&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing
&lt;/h2&gt;

&lt;p&gt;Now that we have our lambda online, we can test it. To do it we need to know the name of our function. The simplest way to find it is going into AWS console and copy the name:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr867kfyc3ug7gquedco3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr867kfyc3ug7gquedco3.png" alt="Finding lambda function name" width="787" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have our function name, let's call it from the CLI.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In order to run our test, we need to enable the literal input, that means we'll pass the payload from the command line. To enable it, open the CLI config file:&lt;br&gt;
MacOS and Linux: &lt;code&gt;~/.aws/config&lt;/code&gt;&lt;br&gt;
Windows: &lt;code&gt;%USERPROFILE%\.aws\config&lt;/code&gt;&lt;br&gt;
Add the line:&lt;br&gt;
&lt;code&gt;cli_binary_format = raw-in-base64-out&lt;/code&gt;&lt;br&gt;
If you want more information, check it &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-config-cli_binary_format" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The payload needs to match our EchoIn class, so to test the lambda we need to run something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws lambda invoke \
  --function-name lambda-kotlin-function-R4M2Bf9fxxBf \
  --payload '{ "content": "Hey!" }' \
  out.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The content of the response will be stored in &lt;code&gt;out.json&lt;/code&gt; file and the content will be:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"content":"Echo: Hey!"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's it, you just deployed and run a Kotlin Lambda function on AWS!&lt;/p&gt;

&lt;p&gt;You can find the code on &lt;a href="https://github.com/rmpt/aws-lambda-kotlin" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>career</category>
    </item>
  </channel>
</rss>
