{"id":380,"date":"2017-06-29T09:00:00","date_gmt":"2017-06-29T00:00:00","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2017\/06\/29\/aws_autocomplete_api"},"modified":"2017-06-29T09:00:00","modified_gmt":"2017-06-29T00:00:00","slug":"aws_autocomplete_api","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2017\/06\/29\/aws_autocomplete_api\/","title":{"rendered":"API Gateway + Lambda + Amazon Elasticserach Service\u306b\u3088\u308b\u30aa\u30fc\u30c8\u30b3\u30f3\u30d7\u30ea\u30fc\u30c8API\u69cb\u7bc9"},"content":{"rendered":"
\u4e0b\u689d\u3067\u3059\u3002
\n\u3053\u306e\u5ea6\u3001Elasticsearch\u3092\u5229\u7528\u3057\u305f\u30aa\u30fc\u30c8\u30b3\u30f3\u30d7\u30ea\u30fc\u30c8\u7684\u306aAPI\u306e\u958b\u767a\u3092\u3059\u308b\u6a5f\u4f1a\u304c\u3042\u308a\u307e\u3057\u305f\u3002Elasticsearch\u3068\u3057\u3066\u306fAWS\u30de\u30cd\u30fc\u30b8\u30c9\u306eAmazon Elasticsearch Service\u3092\u57fa\u672c\u3068\u3057\u3001API Gateway\u304a\u3088\u3073Lambda\u7d4c\u7531\u3067Amazon Elasticsearch Service\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u69cb\u6210\u3068\u3057\u307e\u3057\u305f\u3002\u4eca\u56de\u306fAWS\u306e\u30a4\u30f3\u30d5\u30e9\u7684\u306a\u90e8\u5206\u306b\u7126\u70b9\u3092\u5f53\u3066\u3066\u69cb\u7bc9\u6642\u306e\u30dd\u30a4\u30f3\u30c8\u306b\u3064\u3044\u3066\u3054\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u5b9f\u969b\u306e\u30aa\u30fc\u30c8\u30b3\u30f3\u30d7\u30ea\u30fc\u30c8\u3092\u5b9f\u73fe\u3059\u308b\u306b\u3042\u305f\u3063\u3066\u306fElasticsearch\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u5b9a\u7fa9\u3092\u3044\u308d\u3044\u308d\u3068\u691c\u8a0e\u3057\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u304c\u3001\u4eca\u56de\u306fElasticsearch\u81ea\u4f53\u306e\u8a71\u306f\u3044\u305f\u3057\u307e\u305b\u3093\u3002<\/p>\n
Amazon Elasticsearch Service\u3092\u76f4\u63a5\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u306b\u516c\u958b\u3059\u308b\u3068Elasticsearch\u306e\u4e2d\u8eab\u304c\u5916\u90e8\u304b\u3089\u3044\u3058\u3089\u308c\u305f\u3044\u653e\u984c\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u305f\u3081\u3001\u4e00\u822c\u7684\u306b\u4f55\u3089\u304b\u306e\u30a2\u30af\u30bb\u30b9\u5236\u9650\u3092\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\n\u5b9f\u969b\u3001Amazon Elasticsearch Service\u306e\u8a2d\u5b9a\u3067\u30b0\u30ed\u30fc\u30d0\u30eb\u306b\u958b\u653e\u3059\u308b\u30dd\u30ea\u30b7\u30fc\u306b\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u8b66\u544a\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n
<\/p>\n
\u305d\u308c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u3001\u4eca\u56de\u306fElasticsearch\u306e\u524d\u6bb5\u306bAPI Gateway\u304a\u3088\u3073Lambda\u3092\u7d4c\u7531\u3055\u305b\u3001\u5fc5\u8981\u306a\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u306e\u307f\u3078\u306e\u30a2\u30af\u30bb\u30b9\u3092\u5b9f\u73fe\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002\u307e\u305f\u3001Elasticsearch\u81ea\u4f53\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u30fb\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u5f62\u5f0f\u306f\u5916\u304b\u3089\u898b\u3066\u4fdd\u6301\u3055\u308c\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002
\n\u4ee5\u4e0b\u306b\u5404\u30b5\u30fc\u30d3\u30b9\u306e\u8a2d\u5b9a\u30dd\u30a4\u30f3\u30c8\u306b\u3064\u3044\u3066\u8a18\u8f09\u3057\u307e\u3059\u3002<\/p>\n
Amazon Elasticsearch Service\u306f\u975e\u5e38\u306b\u7c21\u5358\u306bElasticsearch\u30af\u30e9\u30b9\u30bf\u3092\u69cb\u7bc9\u3067\u304d\u3066\u3057\u307e\u3046\u306e\u3067\u7279\u7b46\u3059\u3079\u304d\u30dd\u30a4\u30f3\u30c8\u306f\u3042\u307e\u308a\u3042\u308a\u307e\u305b\u3093\u3002
\n\u4eca\u56de\u306fLambda\u306bIAM\u30ed\u30fc\u30eb\u3092\u5272\u308a\u5f53\u3066\u3066\u3001Lambda\u304b\u3089\u306e\u30a2\u30af\u30bb\u30b9\u306e\u307f\u8a31\u53ef\u3059\u308b\u305f\u3081\u3001AmazonESFullAccess\u30dd\u30ea\u30b7\u30fc\u3092\u30a2\u30bf\u30c3\u30c1\u3057\u305fIAM\u30ed\u30fc\u30eb\u4f5c\u6210\u3057\u305f\u4e0a\u3067\u3001\u30a2\u30af\u30bb\u30b9\u5236\u9650\u3067\u306f\u305d\u306eIAM Role\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n
\u30dd\u30a4\u30f3\u30c8\u306f\u5927\u304d\u304f2\u3064\u3042\u308a\u307e\u3059\u3002<\/p>\n
\u524d\u8ff0\u3057\u305f\u3088\u3046\u306bLambda\u306b\u306fIAM\u30ed\u30fc\u30eb\u3092\u4ed8\u4e0e\u3057\u307e\u3059\u304c\u3001Amazon Elasticsearch Service\u3078\u306e\u30a2\u30af\u30bb\u30b9\u306b\u306f\u660e\u793a\u7684\u306b\u7f72\u540d\u3092\u4ed8\u3051\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<\/li>\n
API Gateway\u3067Lambda\u3092\u30d7\u30ed\u30ad\u30b7\u7d71\u5408\u3068\u3057\u3066\u5229\u7528\u3059\u308b\u5834\u5408\u3001Lambda\u304b\u3089\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u5f62\u5f0f\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<\/li>\n<\/ul>\n
{\n "statusCode": httpStatusCode,\n "headers": { "headerName": "headerValue", ... },\n "body": "..."\n}<\/code><\/pre>\n\u3053\u308c\u306f\u60c5\u5831\u304c\u3044\u308d\u3044\u308d\u3042\u3063\u3066\u3001
\nhttp:\/\/docs.aws.amazon.com\/ja_jp\/apigateway\/latest\/developerguide\/api-gateway-set-up-simple-proxy.html<\/a>
\nhttp:\/\/www.awslessons.com\/2017\/lambda-api-gateway-internal-server-error\/<\/a>
\n\u306a\u3069\u3092\u3054\u53c2\u7167\u304f\u3060\u3055\u3044\u3002<\/p>\n\u4e0a\u8a18\u3092\u8e0f\u307e\u3048\u3001Lambda\u306e\u30b3\u30fc\u30c9\u306fPython\u306e\u5834\u5408\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n
import json\nfrom botocore.awsrequest import AWSRequest\nfrom botocore.auth import SigV4Auth\nfrom botocore.endpoint import BotocoreHTTPSession\nfrom botocore.credentials import Credentials\n\nES_ENDPOINT = "https:\/\/xxxxxxxxxxxxxxxxxxxxx.ap-northeast-1.es.amazonaws.com\/some_index\/some_mapping\/_search?pretty"\n\ndef lambda_handler(event, context):\n credentials = Credentials(\n os.environ["AWS_ACCESS_KEY_ID"],\n os.environ["AWS_SECRET_ACCESS_KEY"],\n os.environ["AWS_SESSION_TOKEN"])\n\n response = request(ES_ENDPOINT, "POST", credentials, 'es', event['body'].encode("utf-8"))\n\n res= {\n 'statusCode': 200,\n 'headers': { 'Content-Type': 'application\/json' },\n 'body': response.text\n }\n return res\n\n\ndef request(url, method, credentials, service_name, data, headers=None):\n region = os.environ["AWS_REGION"]\n aws_request = AWSRequest(url=url, method=method, headers=headers, data=data)\n SigV4Auth(credentials, service_name, region).add_auth(aws_request)\n return BotocoreHTTPSession().send(aws_request.prepare())<\/code><\/pre>\n\u307e\u3068\u3081<\/h1>\n
\u4ee5\u4e0a\u3001\u7c21\u5358\u306bAPI Gateway + Lambda + AWS Elasticserach Service\u306e\u8a2d\u5b9a\u3092\u307e\u3068\u3081\u307e\u3057\u305f\u3002\u3053\u3046\u66f8\u3044\u3066\u307f\u308b\u3068\u305d\u3053\u307e\u3067\u30cf\u30de\u308a\u3069\u3053\u308d\u306f\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u5f53\u521dLambda\u304b\u3089\u7f72\u540d\u306e\u9001\u4fe1\u3092\u5fd8\u308c\u3066\u3044\u305f\u308a\u3001body\u3092\u30a8\u30f3\u30b3\u30fc\u30c9\u3059\u308b\u306e\u3092\u5fd8\u308c\u305f\u6545\u306bAmazon Elasticsearch Service\u304b\u3089\u30a2\u30af\u30bb\u30b9\u62d2\u5426\u3092\u3055\u308c\u305f\u308a\u3001\u5b9f\u306f\u3044\u308d\u3044\u308d\u3068\u30cf\u30de\u308a\u307e\u3057\u305f\u3002
\n\u4eca\u56de\u306fElasticsearch\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u5b9a\u7fa9\u306a\u3069\u306e\u8a71\u306f\u66f8\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u304c\u3001\u307e\u305f\u306e\u6a5f\u4f1a\u306b\u66f8\u3051\u308c\u3070\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n
\u306a\u304a\u3001AWS Lambda\u306b\u3064\u3044\u3066\u4ee5\u4e0b\u306e\u307a\u30fc\u30b8\u3067\u3082\u7d39\u4ecb\u3057\u3066\u3044\u307e\u3059\u3002\u3088\u308d\u3057\u3051\u308c\u3070\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n