22
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

KotlinでAWS Lambdaを書く!

Last updated at Posted at 2018-11-20

1 初めに

毎度お馴染み参考元
AWS LambdaでKotlinのコードを動かす

経緯としては別の記事でKotlinの勉強を始めたのですが、ちょうどAWSも使っていたこともあり
なんならAWSもKotlinで!と思った次第です。

開発環境
・eclipse
・Gradle IDE Pack
・Kotlin Plugin for Eclipse

2 やったこと

・まず普通にGradleの新規プロジェクトを作る
・build.gradleをごにょごにょ
 →Gradle使ったことなかったので正直これが一番難しかった

簡単ですが、以下の通り
細かいところはまだ理解が追い付いていない。。。

build.gradle
/*
 * This build file was generated by the Gradle "init" task.
 *
 * This generated file contains a sample Java Library project to get you started.
 * For more details take a look at the Java Libraries chapter in the Gradle
 * user guide available at https://docs.gradle.org/4.3/userguide/java_library_plugin.html
 */

// Apply the java-library plugin to add support for Java Library

//プロジェクト自体のバージョン
version = "1.0"

//buildscript:ビルドスクリプト自体が依存しているライブラリなどを指定するブロック
buildscript {
    ext.kotlin_version = "1.3.10"
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

//ビルド設定で利用するプラグインの宣言です。apply plugin: java とすると java のプラグインが適用
apply plugin: "java-library"
apply plugin: "java"
apply plugin: "kotlin"


//プロジェクトのソースセットには、sourceSetsプロパティを使ってアクセスすることができます。
sourceSets {
    main.kotlin.srcDirs += "src/main/kotlin"
    main.java.srcDirs += "src/main/java"
}


//プロジェクトとしての依存ライブラリを指定してます。
//compile やら testCompile やらは Maven での <scope> にあたるものになります。
repositories {
    mavenCentral()
}

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api "org.apache.commons:commons-math3:3.6.1"

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation "com.google.guava:guava:23.0"
    compile fileTree(dir: "lib", include: "*.jar")

    // Use JUnit test framework
    testImplementation "junit:junit:4.12"
        compile (
            "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version",
            "com.amazonaws:aws-java-sdk-lambda:1.11.455",
            "com.amazonaws:aws-lambda-java-core:1.2.0",
            "com.googlecode.json-simple:json-simple:1.1.1",
    )
    testCompile group: "junit", name: "junit", version: "4.11"
}


//ビルドの設定を行うスクリプト
//from compileKotlinでkotlinのコンパイル完了物をさらうイメージ??
//lib配下にライブラリ等を入れるとlambda実行時に参照できるらしい
task buildZip(type: Zip) {
    from compileKotlin
    from processResources
    into("lib") {
         from configurations.runtime
    }
}

//dependsOn メソッドを使用することで、タスク間に依存関係を持たせることができます。
//下記だとbuildコマンドを実行する前にbuildZipをやるという意味らしい
build.dependsOn buildZip

・1つ簡単なソースを作成
数値の引数を受け取って、3倍にして返す

App.kt
import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.LambdaLogger

public class App {
	public fun handler(count: Int, context: Context): String {
		val lambdaLogger = context.getLogger()
		lambdaLogger.log("Kotlinモジュールですよ!!!\n")
		lambdaLogger.log("count = " + count +"\n")
		lambdaLogger.log("インプットの3倍の値が出てきますよ!、テストパラメタは全て第一引数に入ってくるようです。\n")
		return "${count * 3}"
	}
}

・Gradleタスクからclean→build
→build\distributions配下にZipで固められるのでそれをLambdaに手動でアップロード

・いざテスト実行、パラメタはもう1でいいや
→ランタイムはJava8です。
68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f3330393436362f31363630336637302d663631372d313862302d623239652d6230613065626563313133662e706e67.png
68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f3330393436362f36376532333462352d636436642d373132372d353132332d6636383037663761386364612e706e67.png
・実行後
68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f3330393436362f35623061306333352d633433362d613766352d616333642d3537346337313161373839342e706e67.png

お、いいんじゃないんでしょうか。
意図したとおり動いてくれてますね。

3 最後に

今回は超簡単なことしか動作確認してませんが、こちらも後日色々試してみたいと思います。
それまでにもっと勉強します。

※2019/1/1 更新
某サイトでこのKotlinでLambdaを作る雛形を作成しました。
落ち着いたらどこかのタイミングでここでも公開しようと思います。

22
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?