{"id":1869,"date":"2018-10-10T08:00:52","date_gmt":"2018-10-09T23:00:52","guid":{"rendered":"https:\/\/www.itc109.com\/knowledge\/?p=1869"},"modified":"2021-09-30T08:57:48","modified_gmt":"2021-09-29T23:57:48","slug":"lambda-snapshot-rds","status":"publish","type":"post","link":"https:\/\/www.itc109.com\/knowledge\/database\/lambda-snapshot-rds","title":{"rendered":"AWS RDS \u2013 Lambda\u3092\u7528\u3044\u305fRDS\u306e\u30b9\u30ca\u30c3\u30d7\u30b7\u30e7\u30c3\u30c8\u53d6\u5f97\uff06\u4e16\u4ee3\u7ba1\u7406"},"content":{"rendered":"<h4><strong>\u6982\u8981<\/strong><\/h4>\n<p>RDS\u306e\u81ea\u52d5\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u306fAWS\u306e\u4ed5\u69d8\u3068\u3057\u30661\u65e51\u56de\u3001\u6700\u592735\u65e5\u9593\u3057\u304b\u53d6\u5f97\u3067\u304d\u306a\u3044\u3002<br \/>\n\u203b\u6700\u5927\u306e\u65e5\u6570\u306e\u7de9\u548c\u306e\u7533\u8acb\u3082\u65ad\u3089\u308c\u307e\u3057\u305f\u3002<\/p>\n<p>\u3057\u304b\u3057\u3001\u30b7\u30b9\u30c6\u30e0\u8981\u4ef6\u306b\u3088\u3063\u3066\u306f60\u65e5\u9593\u4fdd\u5b58\u3084\u3001\u4e00\u65e5\uff12\u56de\u306e\u53d6\u5f97\u304c\u5fc5\u8981\u306a\u30b1\u30fc\u30b9\u3082\u60f3\u5b9a\u3055\u308c\u308b\u3002<\/p>\n<p>\u81ea\u52d5\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u306f\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u6642\u9593\u3092\u6307\u5b9a\u3057\u3066\u3044\u3066\u3082\u3001\u76f4\u8fd1\u306e\u30c7\u30fc\u30bf\u307e\u3067\u306e\u5fa9\u5143\u304c\u53ef\u80fd\u3068\u3044\u3046\u3053\u3068\u304b\u3089\u3001\u81ea\u52d5\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u306f\u6709\u52b9\u6d3b\u7528\u3057\u305f\u3044\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4eca\u56de\u306f\u3001\u8981\u4ef6\u306b\u6cbf\u3063\u305f\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u983b\u5ea6\u3084\u56de\u6570\u3092\u884c\u3046\u305f\u3081\u306b\u3001EC2\u3067\u7528\u3044\u305f Lambda \u3067\u306e\u30b9\u30ca\u30c3\u30d7\u30b7\u30e7\u30c3\u30c8\u306e\u53d6\u5f97\u3092\u7528\u3044\u305f\u8a2d\u5b9a\u306b\u3064\u3044\u3066\u8a18\u8f09\u3002<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>\u624b\u9806<\/strong><\/h4>\n<p>\u203bLamda\u3067\u95a2\u6570\u3092\u4f5c\u6210\u3059\u308b\u307e\u3067\u306e\u624b\u9806\u306f\u3001\u904e\u53bb\u306e\u6295\u7a3f\u3092\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><a href=\"https:\/\/www.itc109.com\/knowledge\/aws\/aws-ebs-snapshot-generation\" target=\"_blank\" rel=\"noopener\">AWS EBS \u2013 \u30b9\u30ca\u30c3\u30d7\u30b7\u30e7\u30c3\u30c8\u306e\u81ea\u52d5\u53d6\u5f97\uff06\u4e16\u4ee3\u7ba1\u7406<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>\u95a2\u6570\u30b3\u30fc\u30c9\u306b\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8cbc\u308a\u4ed8\u3051\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-plain\" data-lang=\"Plain Text\"><code>import boto3\r\nimport time\r\nfrom botocore.client import ClientError\r\nfrom datetime import datetime, timedelta, tzinfo\r\n\r\nrds = boto3.client('rds')\r\n\r\nclass JST(tzinfo):\r\n    def utcoffset(self, dt):\r\n        return timedelta(hours=9)\r\n    def dst(self, dt):\r\n        return timedelta(0)\r\n    def tzname(self, dt):\r\n        return 'JST'\r\n\r\ndef create_snapshot(prefix, instanceid):\r\n    snapshotid = \"-\".join([prefix, datetime.now().strftime(\"%Y-%m-%d-%H-%M\")])\r\n    \r\n    for i in range(0, 5):\r\n        try:\r\n            snapshot = rds.create_db_snapshot(\r\n                DBSnapshotIdentifier=snapshotid,\r\n                DBInstanceIdentifier=instanceid\r\n            )\r\n            return\r\n        except ClientError as e:\r\n                print(str(e))\r\n        time.sleep(1)\r\n\r\ndef delete_snapshots(prefix, days):\r\n    snapshots = rds.describe_db_snapshots()\r\n    now = datetime.utcnow().replace(tzinfo=None)\r\n    for snapshot in snapshots['DBSnapshots']:\r\n        # creating snapshot\r\n        if not snapshot.has_key('SnapshotCreateTime'):\r\n            continue\r\n        \r\n        delta = now - snapshot['SnapshotCreateTime'].replace(tzinfo=None)\r\n        if snapshot['DBSnapshotIdentifier'].startswith(prefix) and delta.days &gt;= days:\r\n            rds.delete_db_snapshot(DBSnapshotIdentifier=snapshot['DBSnapshotIdentifier'])\r\n\r\ndef lambda_handler(event, context):\r\n    delete_days = 60\r\n    snapshot_prefix = \"rds-INSTANCENAME\"\r\n    instances = [\"INSTANCENAME\"]\r\n    \r\n    for instance in instances:\r\n        create_snapshot(snapshot_prefix, instance)\r\n        \r\n    delete_snapshots(snapshot_prefix, delete_days)<\/code><\/pre>\n<\/div>\n<pre class=\"lang:default decode:true \"><span style=\"font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 1rem;\">\u4ee5\u4e0b\u306e\u9805\u76ee\u306b\u3064\u3044\u3066\u306f\u9069\u6642\u5909\u66f4\u3059\u308b\u3002<\/span><\/pre>\n<p>\u3000<strong>delete_days = 60<\/strong><br \/>\n\u3000\u3000\u8a2d\u5b9a\u5024\u4ee5\u524d\u306e\u65e5\u6570\u3092\u7d4c\u904e\u3057\u305f\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b<\/p>\n<p>\u3000<strong>snapshot_prefix = &#8220;rds-INSTANCENAME&#8221;<\/strong><br \/>\n\u3000\u3000\u30d7\u30ec\u30d5\u30a3\u30c3\u30af\u30b9\u540d\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u5024\u3092\u8a2d\u5b9a\u3059\u308b\u3002<\/p>\n<p>\u3000<strong>instances = [&#8220;INSTANCENAME&#8221;]<\/strong><br \/>\n\u3000\u3000DB\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u540d\u3092\u8a2d\u5b9a\u3059\u308b\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u3042\u3068\u306f\u3001CloudWatch Events \u3067\u597d\u304d\u306a\u30b9\u30b1\u30b8\u30e5\u30fc\u30eb\u3092\u8a2d\u5b9a\u3059\u308c\u3070\u8981\u4ef6\u306b\u8fd1\u3065\u3051\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3000\uff11\u65e5\uff12\u56de(08:00 JTS,15:00 JTS)\u306e\u5834\u5408<br \/>\n\u3000\u3000cron(0 23 * * ? *) \u3068 cron(0 6 * * ? *) \u3092\u4f5c\u6210 \u203bUTC<\/p>\n<p>\u3000\uff11\u6708\uff11\u56de(\u6bce\u6708\uff11\u65e5 0:00 JTS)\u306e\u5834\u5408<br \/>\n\u3000\u3000cron(0 15 1 * ? *) \u3068\u3057\u3066\u4f5c\u6210 \u203bUTC \u00a0 \u00a0<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>\u88dc\u8db3<\/strong><\/h4>\n<p><strong>IAM \u30ed\u30fc\u30eb\u306e\u6a29\u9650\u306b\u3064\u3044\u3066<\/strong><\/p>\n<p>RDS \u306e\u30b9\u30ca\u30c3\u30d7\u30b7\u30e7\u30c3\u30c8\u3092 Lambda \u3067\u4f5c\u6210\u3059\u308b\u5834\u5408\u306f\u3001IAM\u30ed\u30fc\u30eb\u306b\u4ee5\u4e0b\u306e\u8a2d\u5b9a\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<p>\u30ea\u30b9\u30c8<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<th>\u30a2\u30af\u30b7\u30e7\u30f3<\/th>\n<th>\u30ea\u30bd\u30fc\u30b9<\/th>\n<th>\u30ea\u30af\u30a8\u30b9\u30c8\u6761\u4ef6<\/th>\n<\/tr>\n<tr>\n<td>DescribeDBInstances<\/td>\n<td>\u3059\u3079\u3066\u306e\u30ea\u30bd\u30fc\u30b9<\/td>\n<td>\u306a\u3057<\/td>\n<\/tr>\n<tr>\n<td>DescribeDBSnapshot<\/td>\n<td>\u3059\u3079\u3066\u306e\u30ea\u30bd\u30fc\u30b9<\/td>\n<td>\u306a\u3057<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u66f8\u304d\u8fbc\u307f<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<th>\u30a2\u30af\u30b7\u30e7\u30f3<\/th>\n<th>\u30ea\u30bd\u30fc\u30b9<\/th>\n<th>\u30ea\u30af\u30a8\u30b9\u30c8\u6761\u4ef6<\/th>\n<\/tr>\n<tr>\n<td>CreateDBSnapshot<\/td>\n<td>\u3059\u3079\u3066\u306e\u30ea\u30bd\u30fc\u30b9<\/td>\n<td>\u306a\u3057<\/td>\n<\/tr>\n<tr>\n<td>DeleteDBSnapshot<\/td>\n<td>\u3059\u3079\u3066\u306e\u30ea\u30bd\u30fc\u30b9<\/td>\n<td>\u306a\u3057<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong>\u30b9\u30ca\u30c3\u30d7\u30b7\u30e7\u30c3\u30c8\u306e\u5236\u9650\u306b\u3064\u3044\u3066<\/strong><\/p>\n<p>\u672c\u624b\u9806\u3067\u624b\u52d5\u30b9\u30ca\u30c3\u30d7\u30b7\u30e7\u30c3\u30c8\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u3059\u304c\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u624b\u52d5\u30b9\u30ca\u30c3\u30d7\u30b7\u30e7\u30c3\u30c8\u53d6\u5f97\u6570\u306f\u5236\u9650\u3055\u308c\u3066\u3044\u307e\u3059\u3002 <a href=\"https:\/\/docs.aws.amazon.com\/ja_jp\/AmazonRDS\/latest\/UserGuide\/CHAP_Limits.html\" target=\"_blank\" rel=\"noopener\">Amazon RDS \u306e\u5236\u9650<\/a> \u8981\u4ef6\u306b\u5408\u308f\u305b\u3066\u7533\u8acb\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u304c\u3001\u4e00\u5ea6\u3082\u30b9\u30ca\u30c3\u30d7\u30b7\u30e7\u30c3\u30c8\u3092\u53d6\u5f97\u3057\u3066\u3044\u306a\u3044\u72b6\u614b\u3060\u3068\u7533\u8acb\u304c\u53d6\u308a\u4e0b\u3052\u3089\u308c\u307e\u3057\u305f\u306e\u3067\u3001\u4e00\u5b9a\u671f\u9593\u5b9f\u884c\u3055\u305b\u305f\u5f8c\u7533\u8acb\u3059\u308b\u307b\u3046\u304c\u826f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 RDS\u306e\u81ea\u52d5\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u306fAWS\u306e\u4ed5\u69d8\u3068\u3057\u30661\u65e51\u56de\u3001\u6700\u592735\u65e5\u9593\u3057\u304b\u53d6\u5f97\u3067\u304d\u306a\u3044\u3002 \u203b\u6700\u5927\u306e\u65e5\u6570\u306e\u7de9\u548c\u306e\u7533\u8acb\u3082\u65ad\u3089\u308c\u307e\u3057\u305f\u3002 \u3057\u304b\u3057\u3001\u30b7\u30b9\u30c6\u30e0\u8981\u4ef6\u306b\u3088\u3063\u3066\u306f60\u65e5\u9593\u4fdd\u5b58\u3084\u3001\u4e00\u65e5\uff12\u56de\u306e\u53d6\u5f97\u304c\u5fc5\u8981\u306a\u30b1\u30fc\u30b9\u3082\u60f3\u5b9a\u3055\u308c\u308b&#8230;<\/p>\n","protected":false},"author":3,"featured_media":23,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[23],"tags":[105,70,59],"class_list":["post-1869","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database","tag-lambda","tag-rds","tag-snapshot"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.itc109.com\/knowledge\/wp-content\/uploads\/sites\/2\/logo\/logo-aws.png","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pacZwp-u9","_links":{"self":[{"href":"https:\/\/www.itc109.com\/knowledge\/wp-json\/wp\/v2\/posts\/1869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itc109.com\/knowledge\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itc109.com\/knowledge\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itc109.com\/knowledge\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itc109.com\/knowledge\/wp-json\/wp\/v2\/comments?post=1869"}],"version-history":[{"count":26,"href":"https:\/\/www.itc109.com\/knowledge\/wp-json\/wp\/v2\/posts\/1869\/revisions"}],"predecessor-version":[{"id":2516,"href":"https:\/\/www.itc109.com\/knowledge\/wp-json\/wp\/v2\/posts\/1869\/revisions\/2516"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itc109.com\/knowledge\/wp-json\/wp\/v2\/media\/23"}],"wp:attachment":[{"href":"https:\/\/www.itc109.com\/knowledge\/wp-json\/wp\/v2\/media?parent=1869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itc109.com\/knowledge\/wp-json\/wp\/v2\/categories?post=1869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itc109.com\/knowledge\/wp-json\/wp\/v2\/tags?post=1869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}