{"id":611,"date":"2020-02-15T11:22:13","date_gmt":"2020-02-15T02:22:13","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2020\/02\/15\/lambda-mysql2-rds"},"modified":"2020-02-15T11:22:13","modified_gmt":"2020-02-15T02:22:13","slug":"lambda-mysql2-rds","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2020\/02\/15\/lambda-mysql2-rds\/","title":{"rendered":"AWS Lambda\u304b\u3089Ruby + Mysql2\u3067Amazon RDS\u306b\u3064\u306a\u3044\u3067\u307f\u305f"},"content":{"rendered":"
\u3053\u308c\u307e\u3067 \u30d0\u30c3\u30c1\u306e\u5185\u5bb9\u3068\u3057\u3066\u306f\u3001 \u30d0\u30c3\u30c1\u306e\u5185\u5bb9\u3092\u3067\u304d\u308b\u3060\u3051\u66f8\u304d\u63db\u3048\u305a\u306b \u307e\u305a\u306f\u4f5c\u696d\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u4f5c\u6210\u3002 \u4e0a\u8a18\u3067\u5229\u7528\u3057\u3066\u3044\u308b A sandboxed local environment that replicates the live AWS Lambda environment almost identically \u2013 including installed software and libraries, file structure and permissions, environment variables, context objects and behaviors \u2013 even the user and running process are the same.<\/p>\n<\/blockquote>\n \u30ed\u30fc\u30ab\u30eb\u30de\u30b7\u30f3\u3067\u3001 \u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3001\u30b3\u30f3\u30c6\u30ca\u3092\u30d3\u30eb\u30c9\u3059\u308b\u3002 \u3046\u307e\u304f\u52d5\u4f5c\u3059\u308c\u3070 \u30b3\u30f3\u30c6\u30ca\u3067 \u4f5c\u6210\u3057\u305f\u30b9\u30af\u30ea\u30d7\u30c8\u306b\u3001\u5b9f\u884c\u6a29\u9650\u3092\u4ed8\u4e0e\u3059\u308b\u3002<\/p>\n \u5b9f\u884c\u3059\u308b\u3068\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306a\u7d50\u679c\u304c\u51fa\u3066\u6b63\u5e38\u306b \u5408\u308f\u305b\u3066\u4f5c\u696d\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b \u524d\u8ff0\u306e\u3001\u30ed\u30fc\u30ab\u30eb\u30de\u30b7\u30f3\u3067 \u554f\u984c\u306a\u304f\u52d5\u4f5c\u3057\u3066\u3044\u308c\u3070\u4e0b\u8a18\u306e\u3088\u3046\u306a\u7d50\u679c\u304c\u8868\u793a\u3055\u308c\u308b\u3002<\/p>\n \u3061\u3083\u3093\u3068 \u3053\u3053\u307e\u3067\u78ba\u8a8d\u3067\u304d\u308c\u3070\u3001 \u30db\u30b9\u30c8\u540d\u3001\u30e6\u30fc\u30b6\u30fc\u540d\u3001\u30d1\u30b9\u30ef\u30fc\u30c9\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u540d\u306a\u3069\u306f\u3001\u74b0\u5883\u5909\u6570\u304b\u3089\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304f\u3068\u826f\u3044\u3002<\/p>\n <\/p>\n \u3053\u308c\u307e\u3067 Amazon EC2 \u3067\u52d5\u304b\u3057\u3066\u3044\u305f1\u65e51\u56de\u52d5\u4f5c\u3059\u308b Ruby \u3067\u66f8\u304b\u308c\u305f\u30d0\u30c3\u30c1\u3092\u3001\u4e3b\u306b\u30b3\u30b9\u30c8\u30c0\u30a6\u30f3\u3092\u76ee\u7684\u3068\u3057\u3066 AWS Lambda \u306b\u79fb\u884c\u3057\u3001\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9\u5316\u3057\u305f\u3002 \u30d0\u30c3\u30c1\u306e\u5185\u5bb9\u3068\u3057\u3066\u306f\u3001RDS \u3067\u7a3c\u50cd\u3057\u3066\u3044\u308b MySQL \u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002 \u30d0\u30c3\u30c1\u306e\u5185\u5bb9\u3092\u3067\u304d\u308b\u3060\u3051\u66f8\u304d\u63db\u3048\u305a\u306b AWS Lambda \u304b\u3089 Amazon RDS \u3078\u63a5\u7d9a\u3059\u308b\u969b\u306b\u3001\u5c11\u3057\u624b […]<\/p>\n","protected":false},"author":3,"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\/611"}],"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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=611"}],"version-history":[{"count":0,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/611\/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=611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}Amazon EC2<\/code> \u3067\u52d5\u304b\u3057\u3066\u3044\u305f1\u65e51\u56de\u52d5\u4f5c\u3059\u308b
Ruby<\/code> \u3067\u66f8\u304b\u308c\u305f\u30d0\u30c3\u30c1\u3092\u3001\u4e3b\u306b\u30b3\u30b9\u30c8\u30c0\u30a6\u30f3\u3092\u76ee\u7684\u3068\u3057\u3066
AWS Lambda<\/code> \u306b\u79fb\u884c\u3057\u3001\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9\u5316\u3057\u305f\u3002<\/p>\n
RDS<\/code> \u3067\u7a3c\u50cd\u3057\u3066\u3044\u308b
MySQL<\/code> \u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002 <\/p>\n
AWS Lambda<\/code> \u304b\u3089
Amazon RDS<\/code> \u3078\u63a5\u7d9a\u3059\u308b\u969b\u306b\u3001\u5c11\u3057\u624b\u9806\u304c\u8907\u96d1\u3060\u3063\u305f\u306e\u3067\u3001\u4eca\u56de\u306f\u305d\u306e\u65b9\u6cd5\u3092\u5099\u5fd8\u9332\u3082\u517c\u306d\u3066\u6574\u7406\u3057\u306a\u304c\u3089\u3084\u3063\u3066\u307f\u308b\u3002<\/p>\n
\u4f5c\u696d\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u4f5c\u6210<\/h3>\n
\u4eca\u56de\u306f\u3001mysql2_test<\/code> \u3068\u3057\u305f\u3002<\/p>\n
$ mkdir mysql2_test\n$ cd mysql2_test<\/code><\/pre>\n
Dockerfile \u4f5c\u6210<\/h3>\n
Mysql2<\/code> \u306e
gem<\/code> \u3092\u52d5\u304b\u3059\u305f\u3081\u306b\u306f\u3001
mysql-devel<\/code> \u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u5fc5\u8981\u3068\u306a\u308b\u3002
Dockerfile<\/code> \u3092\u4e0b\u8a18\u306e\u3088\u3046\u306b\u4f5c\u6210\u3059\u308b\u3002<\/p>\n
FROM lambci\/lambda:build-ruby2.5\nWORKDIR \/var\/task\nRUN yum -y install mysql-devel\nRUN gem update bundler\nCMD "\/bin\/bash"<\/code><\/pre>\n
lambci\/lambda<\/code> \u306e\u30b3\u30f3\u30c6\u30ca\u30a4\u30e1\u30fc\u30b8\u306f\u3001https:\/\/github.com\/lambci\/docker-lambda<\/a> \u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u8aac\u660e\u304c\u3042\u308b\u3068\u304a\u308a\u3001<\/p>\n
\n
AWS Lambda<\/code> \u74b0\u5883\u3092\u307b\u307c\u540c\u3058\u3088\u3046\u306b\u518d\u73fe\u3059\u308b\u3082\u306e\u3068\u306a\u3063\u3066\u3044\u308b\u3002
\u4eca\u56de\u306f Ruby<\/code> \u74b0\u5883\u3092\u4f7f\u3046\u306e\u3067\u3001
build-ruby2.5<\/code> \u306e\u30bf\u30b0\u3092\u6307\u5b9a\u3057\u305f\u3002 <\/p>\n
Docker\u30b3\u30f3\u30c6\u30ca\u3092\u30d3\u30eb\u30c9<\/h3>\n
my-lambda<\/code> \u3068\u30bf\u30b0\u4ed8\u3051\u3057\u305f\u3002 <\/p>\n
$ docker build -t my-lambda .<\/code><\/pre>\n
handler.rb \u4f5c\u6210<\/h3>\n
Mysql2<\/code> \u304c\u52d5\u4f5c\u3057\u3066\u3044\u308b\u304b\u78ba\u8a8d\u3059\u308b\u305f\u3081\u306e
handler.rb<\/code> \u3092\u4f5c\u6210\u3002<\/p>\n
require 'mysql2'\n\ndef main(event:, context:)\n {\n mysql2_version: Mysql2::VERSION\n }\nend<\/code><\/pre>\n
Mysql2<\/code> \u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u8868\u793a\u3055\u308c\u308b\u3082\u306e\u3002<\/p>\n
Gemfile \u4f5c\u6210<\/h3>\n
Gemfile<\/code> \u3092\u4f5c\u6210\u3059\u308b\u3002 <\/p>\n
source 'https:\/\/rubygems.org'\n\ngem 'mysql2'<\/code><\/pre>\n
bundle install<\/code> \u51e6\u7406\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u4f5c\u6210<\/h3>\n
bundle install<\/code> \u3092\u5b9f\u884c\u3059\u308b\u306e\u3060\u304c\u3001
bundle config<\/code> \u3067\u3044\u308d\u3044\u308d\u3068\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u8a2d\u5b9a\u3057\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u306e\u3067\u3001\u30b9\u30af\u30ea\u30d7\u30c8\u5316\u3059\u308b\u3002
bundle_install.sh<\/code> \u3092\u4e0b\u8a18\u306e\u5185\u5bb9\u3067\u4f5c\u6210\u3057\u305f\u3002 <\/p>\n
#!\/bin\/bash\nbundle config --local build.mysql2 --with-mysql2-config=\/usr\/lib64\/mysql\/mysql_config\nbundle config --local silence_root_warning true\nbundle install --path vendor\/bundle --clean\nmkdir -p \/var\/task\/lib\ncp -a \/usr\/lib64\/mysql\/*.so.* \/var\/task\/lib\/<\/code><\/pre>\n
\u4f5c\u6210\u3057\u305f\u30b9\u30af\u30ea\u30d7\u30c8\u30d5\u30a1\u30a4\u30eb\u306b\u5b9f\u884c\u6a29\u9650\u4ed8\u4e0e<\/h3>\n
$ chmod 755 .\/bundle_install.sh<\/code><\/pre>\n
bundle_install.sh<\/code> \u5b9f\u884c<\/h3>\n
bundle_install.sh<\/code> \u3092\u6700\u521d\u306e\u65b9\u3067\u30d3\u30eb\u30c9\u3057\u305f
Docker<\/code> \u30b3\u30f3\u30c6\u30ca\u5185(
my-lambda<\/code>)\u3067\u5b9f\u884c\u3059\u308b\u3002
\u4f5c\u696d\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092 \/var\/task<\/code> \u306b\u30dc\u30ea\u30e5\u30fc\u30e0\u30de\u30a6\u30f3\u30c8\u3057\u3066\u5b9f\u884c\u3002 <\/p>\n
$ docker run --rm -it -v $PWD:\/var\/task my-lambda .\/bundle_install.sh<\/code><\/pre>\n
gem<\/code> \u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u308b\u306f\u305a\u3002 <\/p>\n
[DEPRECATED] The `--clean` flag is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. Instead please use `bundle config set clean 'true'`, and stop using this flag\n[DEPRECATED] The `--path` flag is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. Instead please use `bundle config set path 'vendor\/bundle'`, and stop using this flag\nFetching gem metadata from https:\/\/rubygems.org\/.\nResolving dependencies...\nUsing bundler 2.1.4\nFetching mysql2 0.5.3\nInstalling mysql2 0.5.3 with native extensions\nBundle complete! 1 Gemfile dependency, 2 gems now installed.\nBundled gems are installed into `.\/vendor\/bundle<\/code><\/pre>\n
lib<\/code> \u3084
vendor\/bundle<\/code> \u304c\u3067\u304d\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3059\u308b\u3002 <\/p>\n
\u95a2\u6570\u3092\u30c6\u30b9\u30c8\u5b9f\u884c<\/h3>\n
AWS Lambda<\/code> \u74b0\u5883\u3092\u307b\u307c\u540c\u3058\u3088\u3046\u306b\u518d\u73fe\u53ef\u80fd\u306a
lambci\/lambda<\/code> \u306e\u30b3\u30f3\u30c6\u30ca\u3092\u4f7f\u3063\u3066\u5b9f\u969b\u306b\u52d5\u4f5c\u3092\u30c6\u30b9\u30c8\u3057\u3066\u307f\u308b\u3002
\u30b3\u30de\u30f3\u30c9\u306f\u4e0b\u8a18\u306e\u901a\u308a\u3002<\/p>\n$ docker run --rm -v $PWD:\/var\/task lambci\/lambda:ruby2.5 handler.main<\/code><\/pre>\n
START RequestId: 7e30ee66-2d38-1ade-6e49-a2f107f206b4 Version: $LATEST\nEND RequestId: 7e30ee66-2d38-1ade-6e49-a2f107f206b4\nREPORT RequestId: 7e30ee66-2d38-1ade-6e49-a2f107f206b4 Init Duration: 1859.26 ms Duration: 25.85 ms Billed Duration: 100 ms Memory Size: 1536 MB Max Memory Used: 22 MB\n\n{"mysql2_version":"0.5.3"}<\/code><\/pre>\n
Mysql2<\/code> \u304c\u52d5\u4f5c\u3067\u304d\u3066\u3044\u308b\u306e\u304c\u78ba\u8a8d\u3067\u304d\u305f\u3002<\/p>\n
AWS \u74b0\u5883\u306b\u5408\u308f\u305b\u3066
handler.rb<\/code> \u3092\u4fee\u6b63<\/h3>\n
Amazon RDS<\/code> \u3078\u63a5\u7d9a\u3059\u308b\u60c5\u5831\u3092\u5165\u308c\u3066\u5b9f\u969b\u306e\u74b0\u5883\u3067\u8a66\u3059\u3002 <\/p>\n
require 'mysql2'\n\ndef main(event:, context:)\n client = Mysql2::Client.new(host: ENV["DB_HOST"],\n username: ENV["DB_USER"],\n password: ENV["DB_PASSWORD"],\n database: ENV["DB_NAME"])\n results = client.query("SHOW DATABASES;").to_a\n puts results\nend<\/code><\/pre>\n
zip \u30d5\u30a1\u30a4\u30eb\u4f5c\u6210<\/h3>\n
AWS Lambda<\/code> \u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u305f\u3081\u306b\u3001
deploy.zip<\/code> \u3068\u3044\u3046\u540d\u524d\u3067
zip<\/code> \u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n
zip -q -r deploy.zip .<\/code><\/pre>\n
\u5b9f\u884c<\/h3>\n
AWS Lambda<\/code> \u3092
VPC<\/code> \u5185\u306e\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u30b5\u30d6\u30cd\u30c3\u30c8\u306b\u4f5c\u6210\u3001\u74b0\u5883\u5909\u6570\u3084\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30fc\u30b0\u30eb\u30fc\u30d7\u3092\u9069\u5207\u306b\u8a2d\u5b9a\u3057\u305f\u4e0a\u3067\u3001\u5148\u7a0b\u306e
deploy.zip<\/code> \u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u3002
\u5b9f\u884c\u3057\u3066\u307f\u308b\u3068\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u63a5\u7d9a\u3067\u304d\u305f\u3002<\/p>\n\u53c2\u8003URL<\/h2>\n
\n