An open API service indexing awesome lists of open source software.

https://github.com/importre/hello-sam

Kotlin 기반으로 AWS Lambda, API Gateway를 테스트 및 배포하는 샘플
https://github.com/importre/hello-sam

api-gateway aws gradle kotlin kotlin-dsl lambda sam sample serverless

Last synced: 12 months ago
JSON representation

Kotlin 기반으로 AWS Lambda, API Gateway를 테스트 및 배포하는 샘플

Awesome Lists containing this project

README

          

////
Marked Style: asciidoctor-default
Custom Processor: true
Custom Preprocessor: false
////
:author: Jaewe Heo
:email: jaeweheo@gmail.com
:doctype: article
:toc:

= hello-sam

https://github.com/importre?utf8=%E2%9C%93&tab=repositories&q=sample[image:https://img.shields.io/badge/importre-sample-yellow.svg[alt=importre.sample]]

> https://kotlinlang.org/[Kotlin] 기반으로 AWS https://aws.amazon.com/lambda/[Lambda], https://aws.amazon.com/api-gateway/[API Gateway]를 테스트 및 배포하는 샘플

== Prerequisites

* https://docs.aws.amazon.com/cli/latest/userguide/installing.html[awscli]
* https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html[aws-sam-cli]

== Initialization

.build.gradle.kts
[source, kotlin, linenums]
----
sam {
s3Bucket = "riiid-dev-hello"
stackName = "hello-sam"
"HelloSamFunction"(SamResource.Type.FUNCTION) { // <1>
properties {
handler = "com.importre.example.Hello::handleRequest"
codeUri = "$buildDir/libs/${project.name}-$version-all.jar" // <2>
runtime = SamResource.Properties.Runtime.JAVA8 // <3>
events {
"HelloSam"(SamEvent.Type.API) { // <4>
properties {
path = "/hello"
method = SamEvent.Properties.Method.GET
}
}
}
}
}
}
----
<1> 리소스가 함수면 타입 생략 가능 (``"HelloSamFunction" { ... }``)
<2> 생략 가능 (기본값: ``build/libs/${project.name}-$version-all.jar``)
<3> 생략 가능 (기본값: java8)
<4> 이벤트 타입이 API면 생략 가능 (``"HelloSam { ... }``)

== How to use

=== Test

.shell
[source, zsh, linenums]
----
% ./gradlew test
----

=== SAM local

.shell
[source, zsh, linenums]
----
% ./gradlew runLocalSamApp
----

http://127.0.0.1:3000/hello or http://127.0.0.1:3000/hello?name=world 확인

=== Deployment

.shell
[source, zsh, linenums]
----
% ./gradlew deploySamApp
----

> 내부적으로는 다음과 같이 실행됨

. ``clean``(``build`` 폴더 청소)
. ``shadowJar``(``jar`` 파일 생성)
. ``generateTemplateYaml``(``sam`` 기반으로 ``template.yaml``을 ``build/generated``에 생성)
. ``packageSamApp``(``sam package`` 명령 실행)
. ``deploySamApp``(``sam deploy`` 명령 실행)

== References

* https://github.com/gradle/kotlin-dsl[gradle/kotlin-dsl]
* https://docs.aws.amazon.com/lambda/latest/dg/java-programming-model.html[Building Lambda Functions with Java]
** https://github.com/aws/aws-sdk-java[aws/aws-sdk-java]
* https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md[AWS Serverless Application Model (SAM)]
* https://aws.amazon.com/ko/blogs/korea/new-aws-sam-local-beta-build-and-test-serverless-applications-locally/[AWS SAM Local]

== Future works

* ``sam``에 모자른 https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md[SAM 스펙] 구현하기 → Plugin으로 배포
** 예외처리