{"id":522,"date":"2019-02-09T05:45:00","date_gmt":"2019-02-08T20:45:00","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2019\/02\/09\/appsync-lambda-golang"},"modified":"2019-02-09T05:45:00","modified_gmt":"2019-02-08T20:45:00","slug":"appsync-lambda-golang","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2019\/02\/09\/appsync-lambda-golang\/","title":{"rendered":"AppSync\u306e\u30c7\u30fc\u30bf\u30bd\u30fc\u30b9\u3068\u3057\u3066golang\u306eLambda\u3092\u5229\u7528\u3059\u308b"},"content":{"rendered":"

AWS AppSync\u306e\u30c7\u30fc\u30bf\u30bd\u30fc\u30b9\u3068\u3057\u3066\u3001
GO\u8a00\u8a9e\u306eLambda\u30cf\u30f3\u30c9\u30e9\u30fc\u3092\u6307\u5b9a\u3057\u305f\u74b0\u5883\u3092\u69cb\u7bc9\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

\u6982\u8981\u56f3<\/h2>\n

\u4eca\u56de\u306f\u3001\u4ee5\u4e0b\u306e\u56f3\u306e\u901a\u308a\u306b\u69cb\u7bc9\u3057\u307e\u3059\u3002
AppSync\u306fMutation\u304a\u3088\u3073\u3001Query\u30e1\u30bd\u30c3\u30c9\u3092\u5229\u7528\u3057\u3066\u3001
Lambda Function\u7d4c\u7531\u3067DynamoDB\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u7d50\u679c\u3092\u8fd4\u5374\u3057\u307e\u3059\u3002
\"\u6982\u8981\u56f3\"<\/p>\n

AppSync API<\/h2>\n

\u6700\u521d\u306bAppSync\u306eAPI\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002
\u4eca\u56de\u306f\u30ab\u30b9\u30bf\u30e0API\u306e\u305f\u3081\u3001\u300cBuild from scratch\u300d\u3092\u9078\u629e\u3057\u307e\u3059\u3002
\"AppSync<\/p>\n

DynamoDB<\/h2>\n

DynamoDB\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002
\u4eca\u56de\u306fString\u578b\u306epk, sk\u3092\u5b9a\u7fa9\u3057\u305f\u30b7\u30f3\u30d7\u30eb\u306a\u30c6\u30fc\u30d6\u30eb\u3068\u3057\u307e\u3059\u3002 <\/p>\n

\"\"<\/p>\n

AppSync Schema<\/h2>\n

AppSync API\u306eSchema\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002
\u4eca\u56de\u306fPost\u306e\u578b\u306bfield\u3068\u3044\u3046\u9805\u76ee\u3092\u8a2d\u3051\u3001
Lambda\u306e\u51e6\u7406\u3092\u5206\u5c90\u3055\u305b\u308b\u3088\u3046\u306b\u5b9f\u88c5\u3057\u307e\u3057\u305f\u3002<\/p>\n

type Mutation {\n    putPost(\n        field: String!,\n        pk: String!,\n        sk: String!,\n        title: String!\n    ): Post\n}\n\ntype Post {\n    field: String!\n    pk: String!\n    sk: String!\n    title: String\n}\n\ntype Query {\n    singlePost(field: String!, pk: String!, sk: String!): Post\n}\n\nschema {\n    query: Query\n    mutation: Mutation\n}<\/code><\/pre>\n

Lambda Handler<\/h2>\n

Lambda\u30cf\u30f3\u30c9\u30e9\u30fc\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002
\u4eca\u56de\u306f\u3001Serverless Application Model(SAM)\u3092\u7528\u3044\u307e\u3057\u305f\u3002
golang\u306eswitch\u69cb\u6587\u306b\u3088\u308afield\u306e\u5024\u3067\u51e6\u7406\u304c\u5206\u5c90\u3057\u3001
DynamoDB\u3078\u306eput\u307e\u305f\u306f\u3001get\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n

package main\n\nimport (\n    "fmt"\n    "github.com\/aws\/aws-lambda-go\/lambda"\n    "github.com\/aws\/aws-sdk-go\/aws"\n    "github.com\/aws\/aws-sdk-go\/aws\/session"\n    "github.com\/aws\/aws-sdk-go\/service\/dynamodb"\n    "github.com\/aws\/aws-sdk-go\/service\/dynamodb\/dynamodbattribute"\n)\n\ntype Post struct {\n    Field string `json:"field"`\n    Pk    string `json:"pk"`\n    Sk    string `json:"sk"`\n    Title string `json:"title"`\n}\n\ntype Response struct {\n    Pk    string `json:"pk"`\n    Sk    string `json:"sk"`\n    Title string `json:"title"`\n}\n\nfunc hander(arg Post) (Response, error) {\n\n    response := Response{}\n\n    fmt.Println("[DEBUG]Start create session.")\n    session, err := session.NewSession(\n        &aws.Config{Region: aws.String("ap-northeast-1")},\n    )\n    if err != nil {\n        return Response{}, err\n    }\n    svc := dynamodb.New(session)\n\n    switch arg.Field {\n    case "putPost":\n        input := &dynamodb.PutItemInput{\n            Item: map[string]*dynamodb.AttributeValue{\n                "pk": {\n                    S: aws.String(arg.Pk),\n                },\n                "sk": {\n                    S: aws.String(arg.Sk),\n                },\n                "title": {\n                    S: aws.String(arg.Title),\n                },\n            },\n            TableName: aws.String("appsync-lambda-go"),\n        }\n\n        _, err = svc.PutItem(input)\n        if err != nil {\n            return Response{}, err\n        }\n\n        response.Pk = arg.Pk\n        response.Sk = arg.Sk\n        response.Title = arg.Title\n\n    case "singlePost":\n        input := &dynamodb.GetItemInput{\n            Key: map[string]*dynamodb.AttributeValue{\n                "pk": {\n                    S: aws.String(arg.Pk),\n                },\n                "sk": {\n                    S: aws.String(arg.Sk),\n                },\n            },\n            TableName: aws.String("appsync-lambda-go"),\n        }\n\n        result, err := svc.GetItem(input)\n        if err != nil {\n            return Response{}, err\n        }\n\n        resultData := &Response{}\n        if err := dynamodbattribute.UnmarshalMap(result.Item, resultData); err != nil {\n            return Response{}, err\n        }\n        response.Pk = resultData.Pk\n        response.Sk = resultData.Sk\n        response.Title = resultData.Title\n    }\n\n    return response, nil\n}\n\nfunc main() {\n    lambda.Start(hander)\n}\n<\/code><\/pre>\n

AppSync DataSource<\/h2>\n

\u5b9f\u88c5\u6e08\u307f\u306eLambda Function\u3092AppSync\u306e\u30c7\u30fc\u30bf\u30bd\u30fc\u30b9\u306b\u6307\u5b9a\u3057\u307e\u3059\u3002 <\/p>\n

\"AppSync<\/p>\n

AppSync Schema Resolver<\/h2>\n

Mutation\u304a\u3088\u3073\u3001Query\u306eResolver\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002
\u4eca\u56de\u306f\u4e0b\u8a18\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u30fb\u30ec\u30b9\u30dd\u30f3\u30b9\u30de\u30c3\u30d4\u30f3\u30b0\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u8a2d\u5b9a\u3057\u307e\u3057\u305f\u3002<\/p>\n

request mapping template.<\/h3>\n
{\n    "version" : "2017-02-28",\n    "operation": "Invoke",\n    "payload": $util.toJson($context.arguments)\n}<\/code><\/pre>\n

response mapping template.<\/h3>\n
$util.toJson($context.result)<\/code><\/pre>\n

\u52d5\u4f5c\u78ba\u8a8d<\/h2>\n

\u52d5\u4f5c\u78ba\u8a8d\u3092\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n

Mutation<\/h3>\n

AppSync\u306eQueries\u753b\u9762\u3067\u4e0b\u8a18\u30af\u30a8\u30ea\u3092\u8a18\u8ff0\u3057\u3001\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n

mutation PutPost {\n  putPost(field: "putPost", pk: "hoge", sk: "huga", title: "Hello! AppSync!!") {\n    pk\n    sk\n    title\n  }\n}<\/code><\/pre>\n

\u5b9f\u884c\u7d50\u679c<\/h4>\n

\"DynamoDB\u306e\u30c7\u30fc\u30bf\u5185\u5bb9\"
\n\"Management
\nDynamoDB\u306b\u30c7\u30fc\u30bf\u304c\u4fdd\u5b58\u3055\u308c\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u3082\u671f\u5f85\u3057\u305f\u5024\u304c\u8fd4\u5374\u3055\u308c\u307e\u3057\u305f\uff01<\/p>\n

Query<\/h3>\n

AppSync\u306eQueriess\u753b\u9762\u306b\u4e0b\u8a18\u30af\u30a8\u30ea\u3092\u8a18\u8ff0\u3057\u3001\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n

query GetPost {\n  singlePost(field: "singlePost", pk: "hoge", sk: "huga") {\n    pk\n    sk\n    title\n  }\n}<\/code><\/pre>\n

\u5b9f\u884c\u7d50\u679c<\/h4>\n

\"Management
\nMutation\u3067\u767b\u9332\u3057\u305f\u30c7\u30fc\u30bf\u304c\u3001\u53d6\u5f97\u3067\u304d\u3066\u3044\u307e\u3059\uff01<\/p>\n

\u30c7\u30fc\u30bf\u30bd\u30fc\u30b9\u306bLambda\u3092\u6307\u5b9a\u3057\u305f\u5834\u5408\u306e\u5b9f\u88c5\u306f\u7121\u9650\u5927<\/h2>\n

Lambda\u3067\u51e6\u7406\u3092\u8a18\u8ff0\u3059\u308b\u3053\u3068\u3067\u3001\u305d\u306e\u5b9f\u88c5\u306f\u7121\u9650\u5927\u3067\u3059\u3002
\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u56f3\u306e\u3088\u3046\u306bS3\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4fdd\u5b58\u3057\u3001
\u5206\u6790\u7528\u9014\u3068\u3057\u3066\u3082\u5229\u7528\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u3002
\"S3\u306b\u4fdd\u5b58\u3059\u308b\u4f8b\"<\/p>\n

\u6539\u5584\u70b9<\/h2>\n

\u4eca\u56de\u306f\u30ea\u30af\u30a8\u30b9\u30c8\u30d1\u30e9\u30e1\u30fc\u30bf\u306bfield\u3068\u3044\u3046\u9805\u76ee\u3092\u7528\u610f\u3057\u3001
Lambda\u306e\u51e6\u7406\u3092\u5206\u5c90\u3057\u307e\u3057\u305f\u3002 <\/p>\n

\u3057\u304b\u3057\u306a\u304c\u3089\u3001AppSync\u306e\u305f\u3081\u306eAppSyncResolverTemplate\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u304c
\u6700\u9069\u89e3\u3067\u306f\u306a\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002
AppSyncResolverTemplate\u3092\u5229\u7528\u3059\u308b\u3088\u3046\u3001\u6539\u5584\u3057\u3066\u3044\u3051\u308c\u3070\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

AppSyncResolverTemplate\u306b\u3088\u308b\u5b9f\u88c5\u4f8b<\/h3>\n
package main\n\nimport (\n    "context"\n    "fmt"\n\n    "github.com\/aws\/aws-lambda-go\/events"\n    "github.com\/aws\/aws-lambda-go\/lambda"\n)\n\nfunc handler(ctx context.Context, event events.AppSyncResolverTemplate) error {\n\n  fmt.Printf("Version: %s\n", event.Version)\n  fmt.Printf("Operation: %s\n", event.Operation)\n  fmt.Printf("Payload: %s\n", string(event.Payload))\n\n    return nil\n}<\/code><\/pre>\n

MMM\u306f\u3001\u4f1a\u793e\u3068\u3057\u3066\u3082AWS Lambda\u306b\u529b\u3092\u5165\u308c\u3066\u3044\u307e\u3059\u3002\u305c\u3072\u4ee5\u4e0b\u306e\u30da\u30fc\u30b8\u3082\u3042\u308f\u305b\u3066\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n

\u30fb\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\uff08AWS Lambda\uff09<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

AWS AppSync\u306e\u30c7\u30fc\u30bf\u30bd\u30fc\u30b9\u3068\u3057\u3066\u3001GO\u8a00\u8a9e\u306eLambda\u30cf\u30f3\u30c9\u30e9\u30fc\u3092\u6307\u5b9a\u3057\u305f\u74b0\u5883\u3092\u69cb\u7bc9\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u6982\u8981\u56f3 \u4eca\u56de\u306f\u3001\u4ee5\u4e0b\u306e\u56f3\u306e\u901a\u308a\u306b\u69cb\u7bc9\u3057\u307e\u3059\u3002AppSync\u306fMutation\u304a\u3088\u3073\u3001Query\u30e1\u30bd\u30c3\u30c9\u3092\u5229\u7528\u3057\u3066\u3001Lambda Function\u7d4c\u7531\u3067DynamoDB\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u7d50\u679c\u3092\u8fd4\u5374\u3057\u307e\u3059\u3002 AppSync API \u6700\u521d\u306bAppSync\u306eAPI\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\u4eca\u56de\u306f\u30ab\u30b9\u30bf\u30e0AP […]<\/p>\n","protected":false},"author":8,"featured_media":826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,21],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/522"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=522"}],"version-history":[{"count":0,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/522\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/826"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}