aws lambda in golang

Download AWS Lambda in Golang

Post on 21-Apr-2017

1.393 views

Category:

Internet

1 download

Embed Size (px)

TRANSCRIPT

  • @k_nishijima

    AWS Lambda in Golang

    JAWS-UG 201608 @k_nishijima

    The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/) The design is licensed under the Creative Commons 3.0 Attributions license.

    http://reneefrench.blogspot.com/

  • @k_nishijima

    Go

    2

  • @k_nishijima

    3

  • @k_nishijima

    GoLambda

    4

  • @k_nishijima

    5

    / https://www.r3it.com100%

    okinawa.go JAWSUG

    Facebook/Twitter

    @k_nishijima

    nishijima.koichiro

    https://www.r3it.com

  • @k_nishijima

    6

    AWSGolang Lambda

  • @k_nishijima

    AWSGo7

  • @k_nishijima

    AWSGo

    8

    AWS SDK for Go https://aws.amazon.com/jp/sdk-for-go/

    https://github.com/aws/aws-sdk-go

    Lambda Node.js (JavaScript)Python Java (Java 8 )https://aws.amazon.com/jp/lambda/faqs/

    https://aws.amazon.com/jp/sdk-for-go/https://github.com/aws/aws-sdk-gohttps://aws.amazon.com/jp/lambda/faqs/

  • @k_nishijima

    LambdaGo

    9

  • @k_nishijima

    10

  • @k_nishijima

    11

    APEX : Lambdahttp://apex.run/https://github.com/apex/apex

    Simple API Gateway : API Gateway https://github.com/horike37/simple-api-gateway

    Terraform : https://www.terraform.io/

    http://apex.run/https://github.com/apex/apexhttps://github.com/horike37/simple-api-gatewayhttps://www.terraform.io/

  • @k_nishijima 12

    Lambda

    Terraform

    Node.jsshimGoGoLambda

    APEX

  • @k_nishijima 13

    Go

    (^_^;)

    APEX

  • @k_nishijima

    Simple API Gateway

    14

    API GatewayCLI

    API

  • @k_nishijima

    Simple API Gateway

    15

  • @k_nishijima 16

    HashiCorp

    JSON/

    IAMLambda

    Terraform

  • @k_nishijima

    LambdaWeb API

    17

  • @k_nishijima

    DynamoDB

    API

    18

  • @k_nishijima

    Slack

    19

  • @k_nishijima 20

    https://github.com/k-nishijima/lambda-handson-jawsug-okinawa-201608

    infraTerraformtf lambdaLambda

    https://github.com/k-nishijima/lambda-handson-jawsug-okinawa-201608

  • @k_nishijima 21

    TerraformAWSIAMDynamoDB API GatewayWeb API GoLambdaAPEX GoAWS

    GoLambdaSimple API Gateway

    HTTP LambdaAPI GatewayHTTP

  • @k_nishijima

    22

    1. Terraform DBIAM 2. Go 3. APEXLambda 4. Simple API GatewayAPI1 5. APIOK? 235

  • @k_nishijima 23

    https://www.terraform.io/intro/getting-started/install.html

    infraREADME.mdterraform apply"

    DynamoDBIAM Role

    Terraform

    https://www.terraform.io/intro/getting-started/install.htmlhttps://github.com/k-nishijima/lambda-handson-jawsug-okinawa-201608/tree/master/infra

  • @k_nishijima

    Go

    24

    GOPATHLambda

  • @k_nishijima

    25

    ~/gopath/src/github.com/k-nishijima/lambda-handson

    APEXLambda

    ~/lambda-handson/functions/funcname/

    import

    APEXimport

  • @k_nishijima

    26

    GoLambda

    AWS DynamoDB localGoAwsSNS/SQS

    https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/DynamoDBLocal.htmlhttps://github.com/p4tin/GoAws

  • @k_nishijima

    27

    https://github.com/k-nishijima/lambda-handson-golang-201608/blob/master/dao.go

    GoAWSLambda

    HTTP

    https://github.com/k-nishijima/lambda-handson-golang-201608/blob/master/dao.gohttps://github.com/k-nishijima/lambda-handson-golang-201608/blob/master/dao.go#L17

  • @k_nishijima

    28

    JSONvalidate

    AWSProfile

    svc

    https://github.com/k-nishijima/lambda-handson-golang-201608/blob/master/dao.go#L17https://github.com/k-nishijima/lambda-handson-golang-201608/blob/master/dao.go#L44

  • @k_nishijima

    29

    go install APEXLambda

    go install

  • @k_nishijima 30

    (?)

    GoLambda

  • @k_nishijima

    APEX

    31

    $ apex -p lambda-handson-201608 init -pAWS profile

    Lambda Terraform

  • @k_nishijima

    APEX

    32

    functions hello index.js project.json

    suffix

    Node.jsindex.jshello

  • @k_nishijima

    project.json

    33

    project.json

    name: prefix

    Lambdarole function.json

    https://github.com/k-nishijima/lambda-handson-jawsug-okinawa-201608/blob/master/lambda/project.json

  • @k_nishijima

    GoLambda

    34

    APEXSTDINSTDOUTNodeGoSTDERR os.Stderr.WriteString(hoge)

  • @k_nishijima

    35

    func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { // var request lambdaHandson.AddValueRequest if err := json.Unmarshal(event, &request); err != nil { return nil, err }

    https://github.com/k-nishijima/lambda-handson-jawsug-okinawa-201608/blob/master/lambda/functions/contact/main.go#L11

  • @k_nishijima

    json.UnmarshalJSON

    36

    type AddValueRequest struct { Stage string `json:"stage" valid:"required"` Email string `json:"email" valid:"email,length(1|512),required"` Message string `json:"message" valid:"length(1|1024),required"` }

    { "stage": "dev", "email": "foo@bar.com", "message": "hello golang"}

    unmarshal

    JSON

    JSON

  • @k_nishijima

    validategovalidator

    37

    type AddValueRequest struct { Stage string `json:"stage" valid:"required"` Email string `json:"email" valid:"email,length(1|512),required"` Message string `json:"message" valid:"length(1|1024),required"` }

    https://github.com/asaskevich/govalidator

    https://github.com/asaskevich/govalidator

  • @k_nishijima

    38

    err = dao.Put(request) if err != nil { return nil, err }

    Lambda

    Lambda(^_^;)

  • @k_nishijima

    LambdaTIPS

    39

    LambdaIAM RoleAccessKey = Profile

    Lambda ProfileOK

    https://github.com/k-nishijima/lambda-handson-golang-201608/blob/master/dao.go#L46

  • @k_nishijima

    (^^;

    40

  • @k_nishijima 41

    $ apex -p lambda-handson-201608 deploy contact

    contact

    GUI(^_^;)

    APEX

  • @k_nishijima

    APEX

    42

    project.jsonname + function

  • @k_nishijima

    APEXLambda

    43

    $ apex -p lambda-handson-201608 invoke contact < request_contact.json

    JSONcontactinvoke

  • @k_nishijima

    DynamoDB

    44

    DynamoDB(^_^;)

    GetItemsAPI

  • @k_nishijima

    APEX

    45

    $ apex -p lambda-handson-201608 logs contact

  • @k_nishijima 46

  • @k_nishijima

    47

    GoGoLambda

    S3

  • @k_nishijima

    Web API><

    LambdaHTTPS

    48

  • @k_nishijima

    API Gateway

    49

  • @k_nishijima

    Web API

    HTML

    50

  • @k_nishijima

    Simple API GatewayAPI

    51

    npm install -g simple-api-gateway

    ES6Node4AWS CLIhttps://github.com/horike37/simple-api-gateway

    https://github.com/horike37/simple-api-gateway

  • @k_nishijima

    Simple API GatewayAPI

    52

  • @k_nishijima

    53

    POST /contact Lambda API

  • @k_nishijima

    AWS

    54

    https://github.com/horike37/simple-api-gateway/issues/5

    (^_^;)

    https://github.com/horike37/simple-api-gateway/issues/5

  • @k_nishijima

    55

    west-mbp:lambda-handson-jawsug-okinawa-201608 nishijima$ apigw create ? Please select Region for API Gatway ap-northeast-1 ? Please input API Name Handson API API Create Success!! Please action `apigw edit` and set up API

  • @k_nishijima

    56

    west-mbp:lambda-handson-jawsug-okinawa-201608 nishijima$ apigw edit ? Please select Region for API Gatway ap-northeast-1 ? Please select API Handson API

    ? Do you want to use an existing Resources on Handson APIor create a new one? Create A New Resouce

    ? Please select parent Resource / ? Please input Resouce Path contact

    ? Please select method POST ? Please select backend lambda function lambda-handson-jawsug-okinawa-201608_contact

    ? May I set enable CORS? Yes ? Set up mapping template? (y/N) y ? Please edit mapping template Received

    ? May I create the API? Yes Create Success!! Resource and Method on Handson API API.

    https://github.com/k-nishijima/lambda-handson-jawsug-okinawa-201608/blob/master/apigw/mapping-tamplate.md

  • @k_nishijima

    57

    Integration RequestURL//HTTPLambda

    URLAPIGWHTTPLambda

    http://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/api-gateway-mapping-template-reference.htmlhttps://github.com/k-nishijima/lambda-handson-jawsug-okinawa-201608/blob/master/apigw/mapping-tamplate.md

  • @k_nishijima

    API

    58

    west-mbp:lambda-handson-jawsug-okinawa-201608 nishijima$ apigw deploy ? Please select Region for API Gatway ap-northeast-1 ? Please select API Handson API ? Please input Stage Name dev Deploy success! Endpoint:https://.execute-api.ap-northeast-1.amazonaws.com/dev

    devAPI = OK URL Lambda

    http://execute-api.ap-northeast-1.amazonaws.com/dev

  • @k_nishijima

    59

    $ curl -H "Content-Type: application/json" -X POST -d "email=curl@foo.com&message=hello world via curl" https://URL.execute-api.ap-northeast-1.amazonaws.com/dev/contact "ok"

  • @k_nishijima 60

    !?

  • @k_nishijima

    61

    Q: A: 1

  • @k_nishijima

    62

    Q: A:

    DynamoDB