{"id":1745,"date":"2015-11-08T00:01:44","date_gmt":"2015-11-07T15:01:44","guid":{"rendered":"http:\/\/www.magtranetwork.com\/?p=1745"},"modified":"2020-09-30T04:48:21","modified_gmt":"2020-09-29T19:48:21","slug":"aws_lambda_python_aws_cli","status":"publish","type":"post","link":"https:\/\/www.magtranetwork.com\/aws\/aws_lambda_python_aws_cli.html","title":{"rendered":"AWS Lambda Python \u3067 AWS CLI \u3092\u5b9f\u884c\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"

AWS Lambda\u3067Python\u8a00\u8a9e\u3092\u4f7f\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u3001AWS Lambda\u3092\u4f7f\u7528\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u958b\u767a\u3001\u30b7\u30b9\u30c6\u30e0\u69cb\u7bc9\u306f\u66f4\u306b\u5e45\u3092\u5e83\u3052\u307e\u3057\u305f\u3002<\/p>\n

AWS Lambda Python\u3001API Gateway\u3092\u4f7f\u7528\u3059\u308c\u3070\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9(\u30b5\u30fc\u30d0\u306e\u5b8c\u5168\u30de\u30cd\u30fc\u30b8\u30c9)\u306a\u30b7\u30b9\u30c6\u30e0\u69cb\u7bc9\u304c\u5b9f\u73fe\u53ef\u80fd\u3067\u3059\u3002<\/p>\n

\u3057\u304b\u3057\u3001\u4e00\u65b9\u3067AWS Lambda\u306f\u307e\u3060\u65e9\u719f\u306a\u90e8\u5206\u3082\u3042\u308aAWS Lambda\u95a2\u6570\u304c\u5b9f\u884c\u3055\u308c\u308b\u30b5\u30fc\u30d0\u3067\u306f\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u306a\u3044\u30e9\u30a4\u30d6\u30e9\u30ea\u3082\u591a\u304f\u3001\u73fe\u6642\u70b9\u3067AWS CLI\u3082\u5b9f\u884c\u3067\u304d\u306a\u3044\u72b6\u614b\u3067\u3059\u3002<\/p>\n

\u305d\u3053\u3067\u4eca\u56de\u306f\u73fe\u72b6\u306eAWS Lambda Python\u306e\u74b0\u5883\u3067AWS CLI\u3092\u5b9f\u884c\u3059\u308b\u65b9\u6cd5\u3092\u5099\u5fd8\u9332\u3068\u3057\u3066\u8a18\u8f09\u3057\u3066\u304a\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

AWS Lambda Python \u3067 AWS CLI \u3092\u5b9f\u884c\u3059\u308b\u65b9\u6cd5<\/h2>\n

AWS CLI\u3092\u5b9f\u884c\u3059\u308bAWS Lambda Python\u7528\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u4f5c\u6210\u3068\u5fc5\u8981\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u30ed\u30fc\u30ab\u30eb\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h4>\n
\r\n[magtranetwork@localhost ~]$ mkdir aws_cli\r\n[magtranetwork@localhost aws_cli]$ cd aws_cli\r\n[magtranetwork@localhost aws_cli]$ pip install awscli -t .\/\r\n[magtranetwork@localhost aws_cli]$ pip install simplejson -t .\/\r\n[magtranetwork@localhost aws_cli]$ pip install argparse -t .\/\r\n<\/pre>\n

aws\u30b3\u30de\u30f3\u30c9\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u4f5c\u6210\u3068\u52d5\u4f5c\u78ba\u8a8d<\/h4>\n
\r\n[magtranetwork@localhost aws_cli]$ vim aws\r\n<\/pre>\n
\r\n#!\/usr\/bin\/python\r\n# Copyright 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.\r\n\r\n# Licensed under the Apache License, Version 2.0 (the "License"). You\r\n# may not use this file except in compliance with the License. A copy of\r\n# the License is located at\r\n\r\n#     https:\/\/aws.amazon.com\/apache2.0\/\r\n\r\n# or in the "license" file accompanying this file. This file is\r\n# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF\r\n# ANY KIND, either express or implied. See the License for the specific\r\n# language governing permissions and limitations under the License.\r\nimport sys\r\nimport os\r\n\r\nif os.environ.get('LC_CTYPE', '') == 'UTF-8':\r\n    os.environ['LC_CTYPE'] = 'en_US.UTF-8'\r\nimport awscli.clidriver\r\n\r\n\r\ndef main():\r\n    return awscli.clidriver.main()\r\n\r\n\r\nif __name__ == '__main__':\r\n    sys.exit(main())\r\n<\/pre>\n
\r\n[magtranetwork@localhost aws_cli]$ .\/aws --version\r\naws-cli\/1.9.5 Python\/2.6.6 Linux\/2.6.32-573.7.1.el6.i686 botocore\/1.3.5\r\n<\/pre>\n

AWS Lambda Python\u306e\u5b9f\u884c\u30b9\u30af\u30ea\u30d7\u30c8\u4f5c\u6210<\/h4>\n

\u4eca\u56de\u306f\u30c6\u30b9\u30c8\u7528\u306b\u300cprint(_(“.\/aws s3 ls”))\u300d\u3092\u5b9f\u884c\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

\r\n[magtranetwork@localhost aws_cli]$ vim lambda_function.py\r\n<\/pre>\n
\r\nimport commands\r\nimport json\r\nimport os\r\nfrom cStringIO import StringIO\r\nimport re\r\n\r\nprint('Loading function')\r\n\r\ndef _(cmd):\r\n    return commands.getoutput(cmd) \r\n    \r\ndef lambda_handler(event, context):\r\n    print(_(".\/aws s3 ls"))\r\n<\/pre>\n

\u30d5\u30a1\u30a4\u30eb\u3078\u306e\u5b9f\u884c\u6a29\u9650\u4ed8\u4e0e\u3068ZIP\u30d5\u30a1\u30a4\u30eb\u306e\u4f5c\u6210<\/h4>\n
\r\n[magtranetwork@localhost aws_cli]$ chmod 755 -R *\r\n[magtranetwork@localhost aws_cli]$ zip -r aws_cli.zip *\r\n<\/pre>\n

AWS Lambda Python\u3067\u5b9f\u884c<\/h4>\n

zip\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u305f\u5f8c\u306fAWS Lambda Python\u3067\u5b9f\u884c\u3059\u308b\u306e\u307f\u3067\u3059\u3002
\nAWS Lambda Python\u3067\u5b9f\u884c\u3059\u308b\u969b\u306b\u306fRole\u306b\u4ed8\u4e0e\u3055\u308c\u3066\u3044\u308b\u30dd\u30ea\u30b7\u30fc\u306bAWS CLI\u3067\u5b9f\u884c\u3059\u308b\u30b3\u30de\u30f3\u30c9\u306b\u95a2\u3059\u308b\u6a29\u9650\u304c\u4ed8\u4e0e\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

AWS Lambda\u3067Python\u8a00\u8a9e\u3092\u4f7f\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u3001AWS Lambda\u3092\u4f7f\u7528\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u958b\u767a\u3001\u30b7\u30b9\u30c6\u30e0\u69cb\u7bc9\u306f\u66f4\u306b\u5e45\u3092\u5e83\u3052\u307e\u3057\u305f\u3002 AWS Lambda Python\u3001API Gateway\u3092\u4f7f\u7528\u3059\u308c\u3070 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[3,81,31,79,80],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/posts\/1745"}],"collection":[{"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/comments?post=1745"}],"version-history":[{"count":15,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/posts\/1745\/revisions"}],"predecessor-version":[{"id":9073,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/posts\/1745\/revisions\/9073"}],"wp:attachment":[{"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/media?parent=1745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/categories?post=1745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/tags?post=1745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}