{"id":118,"date":"2017-11-23T16:04:09","date_gmt":"2017-11-23T14:04:09","guid":{"rendered":"http:\/\/developer.ps\/?p=118"},"modified":"2026-09-22T20:52:17","modified_gmt":"2026-09-22T18:52:17","slug":"manage-aws-ec2-snapshots","status":"publish","type":"post","link":"https:\/\/developer.ps\/index.php\/2017\/11\/23\/manage-aws-ec2-snapshots\/","title":{"rendered":"Manage AWS EC2 Snapshots"},"content":{"rendered":"<p>Create and delete snapshots according to retention plan.<\/p>\n<ol>\n<li>IAM account:\n<ul>\n<li>\u00a0In the AWS Management Console, go to IAM service.\u00a0 On the right side click on Users then Add User:<br \/>\n<div id='gallery-1' class='gallery galleryid-118 gallery-columns-3 gallery-size-medium'><figure class='gallery-item'>\n\t\t\t<div class='gallery-icon landscape'>\n\t\t\t\t<a href='https:\/\/developer.ps\/index.php\/2017\/11\/23\/manage-aws-ec2-snapshots\/aws_1\/'><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"94\" src=\"https:\/\/developer.ps\/wp-content\/uploads\/2017\/11\/aws_1-300x94.png\" class=\"attachment-medium size-medium\" alt=\"AWS IAM User\" aria-describedby=\"gallery-1-120\" srcset=\"https:\/\/developer.ps\/wp-content\/uploads\/2017\/11\/aws_1-300x94.png 300w, https:\/\/developer.ps\/wp-content\/uploads\/2017\/11\/aws_1-768x241.png 768w, https:\/\/developer.ps\/wp-content\/uploads\/2017\/11\/aws_1-1024x321.png 1024w, https:\/\/developer.ps\/wp-content\/uploads\/2017\/11\/aws_1.png 1882w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>\n\t\t\t<\/div>\n\t\t\t\t<figcaption class='wp-caption-text gallery-caption' id='gallery-1-120'>\n\t\t\t\tAWS IAM User\n\t\t\t\t<\/figcaption><\/figure>\n\t\t<\/div>\n<\/li>\n<li>\u00a0Type In <strong>user name<\/strong> and select <strong>Programmatic access<\/strong>. Then <strong>Next:Permissions<\/strong><\/li>\n<li>\u00a0On the next page select &#8220;<strong>Attach existing policy directly<\/strong>&#8221; then click on <strong>Create Policy<\/strong><\/li>\n<li>\u00a0Click on the <strong>JSON<\/strong> tab then copy\/paste the code below(we will use one policy for creating and deleting snapshots):\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\">{\r\n    \"Version\": \"2012-10-17\",\r\n    \"Statement\": [\r\n        {\r\n            \"Sid\": \"Stmt1426256275000\",\r\n            \"Effect\": \"Allow\",\r\n            \"Action\": [\r\n                \"ec2:CreateSnapshot\",\r\n                \"ec2:CreateTags\",\r\n                \"ec2:DeleteSnapshot\",\r\n                \"ec2:DescribeSnapshots\",\r\n                \"ec2:DescribeVolumes\",\r\n        \"ec2:DescribeInstances\"\t\t\t\t\r\n            ],\r\n            \"Resource\": [\r\n                \"*\"\r\n            ]\r\n        },{\r\n      \"Sid\": \"Stmt1422916495000\",\r\n      \"Effect\": \"Allow\",\r\n      \"Action\": [\r\n        \"ec2:DeleteSnapshot\",\r\n        \"ec2:DescribeSnapshots\"\r\n      ],\r\n      \"Resource\": [\r\n        \"*\"\r\n      ]\r\n    }\r\n    ]\r\n}<\/pre>\n<p>&nbsp;<\/li>\n<li>\u00a0Click on <strong>Review policy <\/strong><\/li>\n<li>\u00a0Give it a <strong>Name<\/strong> and <strong>Description<\/strong>, such as CMD-SNAPSHOTS-POLICY. Then Click on <strong>Create policy<\/strong><\/li>\n<li>\u00a0Back to the Permissions Page. Click on the <strong>Refresh<\/strong> button and type in the filter the name of the policy you&#8217;ve just created.\u00a0 Should see the new policy. Select it, then click on <strong><strong><strong>Next: Preview <\/strong><\/strong><\/strong>and on the next page <strong>Create User<\/strong><br \/>\n<div id='gallery-2' class='gallery galleryid-118 gallery-columns-3 gallery-size-medium'><figure class='gallery-item'>\n\t\t\t<div class='gallery-icon landscape'>\n\t\t\t\t<a href='https:\/\/developer.ps\/wp-content\/uploads\/2017\/11\/aws_2.png'><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"136\" src=\"https:\/\/developer.ps\/wp-content\/uploads\/2017\/11\/aws_2-300x136.png\" class=\"attachment-medium size-medium\" alt=\"IAM User permissions\" srcset=\"https:\/\/developer.ps\/wp-content\/uploads\/2017\/11\/aws_2-300x136.png 300w, https:\/\/developer.ps\/wp-content\/uploads\/2017\/11\/aws_2-768x349.png 768w, https:\/\/developer.ps\/wp-content\/uploads\/2017\/11\/aws_2-1024x465.png 1024w, https:\/\/developer.ps\/wp-content\/uploads\/2017\/11\/aws_2.png 1818w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>\n\t\t\t<\/div><\/figure>\n\t\t<\/div>\n<\/li>\n<li>\u00a0From the next page, make a copy of the <strong> Access key ID <\/strong>&amp; <strong><strong>Secret access key<\/strong><\/strong><\/li>\n<li>On the server, create a new file, and save it to <strong>$HOME\/.awssecret<\/strong>, with the following format:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Access key id\r\nSecret access key<\/pre>\n<p>&nbsp;<\/li>\n<\/ul>\n<\/li>\n<li>Using ec2-consistent-snapshot from <a href=\"https:\/\/github.com\/alestic\/ec2-consistent-snapshot\">https:\/\/github.com\/alestic\/ec2-consistent-snapshot<\/a> to take snapshots.On a Centos 7 system, Enable epel:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">wget dl.fedoraproject.org\/pub\/epel\/7\/x86_64\/Packages\/e\/epel-release-7-11.noarch.rpm\r\nrpm -ivh epel-release-7-10.noarch.rpm<\/pre>\n<p>Then install the required Modules:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">yum install \\\r\n  perl-DBI \\\r\n  perl-DBD-M ySQL \\\r\n  perl-Getopt-Long \\\r\n  perl-Pod-Usage \\\r\n  perl-File-Slurp \\\r\n  perl-IO-Socket-SSL \\\r\n  perl-Net-Amazon-EC2 \\\r\n  perl-Time-HiRes \\\r\n  perl-Net-SSLeay \\\r\n  perl-Params-Validate \\\r\n  ca-certificates \\\r\n  perl-DateTime\r\n<\/pre>\n<p>If you are using cPanel, install the required Perl Modules by login to WHM, then <strong>Software<\/strong> -&gt; <strong>Install Perl Modules<\/strong>. Then install the following:<\/p>\n<p><em>DBD::mysql<\/em><br \/>\n<em>Getopt::Long<\/em><br \/>\n<em>Pod::Usage<\/em><br \/>\n<em>File::Basename<\/em><br \/>\n<em>File::Slurp<\/em><br \/>\n<em>IO::Dir<\/em><br \/>\n<em>IO::Socket::SSL<\/em><br \/>\n<em>LWP::UserAgent<\/em><br \/>\n<em>Time::HiRes<\/em><br \/>\n<em>Net::Amazon::EC2<\/em><br \/>\n<em>DateTime::Locale<\/em><br \/>\n<em>DateTime::TimeZone<\/em><\/p>\n<p>Not 100% sure if this is all the needed module, you can see what&#8217;s missing when you run the script. Just make a note of the error and install the missing module.<\/p>\n<p>After that download the script from : <a href=\"https:\/\/github.com\/alestic\/ec2-consistent-snapshot\">https:\/\/github.com\/alestic\/ec2-consistent-snapshot<\/a><\/p>\n<p>Save it to a folder on your system, I choose \/aws<br \/>\nMake it executable: chmod +x \/aws\/ec2-consistent-snapshot<\/li>\n<li>To delete old\u00a0 snapshots, I&#8217;ve used: ec2-expire-snapshots from <a href=\"https:\/\/github.com\/alestic\/ec2-expire-snapshots\">https:\/\/github.com\/alestic\/ec2-expire-snapshots<br \/>\n<\/a>Needed Perl Modules<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">yum install perl-Date-Manip \\\r\n perl-DateTime \\\r\n perl-DateTime-TimeZone \\\r\n perl-DateTime-Format-ISO8601 \\\r\n perl-File-Slurp \\\r\n perl-Time-HiRes \\\r\n perl-Params-Validate \\\r\n ca-certificates \\\r\n perl-DBD-MySQL \\\r\n perl-Net-Amazon-EC2<\/pre>\n<p>If there is a problem with the package perl-Net-Amazon-EC2. Then use cpan (<a href=\"https:\/\/goo.gl\/fdEPvU\">https:\/\/goo.gl\/fdEPvU<\/a>):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">yum install -y expat-devel\r\ncpan App::cpanminus  # a better package manager for CPAN\r\n\/usr\/local\/bin\/cpanm Net::Amazon::EC2<\/pre>\n<p>Cpanel (Bold = installed in previous step):<\/p>\n<p>Date::Manip<br \/>\nDateTime<br \/>\n<strong>DateTime::TimeZone<\/strong><br \/>\nDateTime::Format::ISO8601<br \/>\n<strong>File::Slurp<\/strong><br \/>\n<strong>Time::HiRes<\/strong><br \/>\nParams::Validate<br \/>\n<strong>Net::Amazon::EC2<\/strong><br \/>\nMoose::Exception::ValidationFailedForInlineTypeConstraint<\/p>\n<p>After that download the script from: <a href=\"https:\/\/github.com\/alestic\/ec2-expire-snapshots\">https:\/\/github.com\/alestic\/ec2-expire-snapshots<\/a><br \/>\nSave it to the same folder on your system:<br \/>\nMake it executable: chmod +x \/aws\/<a href=\"https:\/\/github.com\/alestic\/ec2-expire-snapshots\">ec2-expire-snapshots<\/a><\/li>\n<li>Create a script and cron job to run the previous scripts:<br \/>\ncreate a file called: aws_snapshots<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">volume='vol-ID'\r\n\r\necho \"AWS Snaphot backup started\"\r\necho \"Date: `date`\"\r\necho \"\"\r\n\/aws\/ec2-consistent-snapshot --mysql --debug --region eu-west-1 $volume\r\necho \"AWS Snaphot backup Ended\"\r\necho \"\"\r\necho \"Removing old snapshots\"\r\n\/aws\/ec2-expire-snapshots \\\r\n  --region eu-west-1 \\\r\n  --keep-most-recent 1 \\\r\n  --keep-first-daily 7 \\\r\n  --keep-first-weekly 4 \\\r\n  --keep-first-monthly 6 \\\r\n  $volume \r\necho \"done\"<\/pre>\n<p>(Better explore the options offered by ec2-consistent-snapshot &amp; ec2-expire-snapshots, and change the script accordingly)<\/p>\n<p>Make the script runnable: chmod +x \/aws\/aws_snapshots<\/p>\n<p>add it to your cron job to run daily<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Create and delete snapshots according to retention plan. IAM account: \u00a0In the AWS Management Console, go to IAM service.\u00a0 On the right side click on [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,14,13],"tags":[],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry","category-amazon-ec2","category-centos","category-linux"],"_links":{"self":[{"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/comments?post=118"}],"version-history":[{"count":27,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/posts\/118\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}