{"id":11175,"date":"2013-12-29T02:28:30","date_gmt":"2013-12-28T20:58:30","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11175"},"modified":"2014-12-17T09:08:36","modified_gmt":"2014-12-17T03:38:36","slug":"role-based-s3-access-from-a-specific-ec2-instance","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/role-based-s3-access-from-a-specific-ec2-instance\/","title":{"rendered":"IAM Role based access to S3 bucket."},"content":{"rendered":"<p style=\"text-align: justify;\">In one of our projects, we came across a requirement where we were required to fetch a file containing important data from S3 in order to use it. Due to Security concerns we were not keen on storing access keys on the EC2 instance. Which basically meant that we could not configure S3 on that very system as configuring S3 would create a configuration file (.s3cfg) with access keys stored on it.<\/p>\n<p style=\"text-align: justify;\">The architecture is described in image below.<\/p>\n<p style=\"text-align: justify;\"><a href=\"\/blog\/wp-ttn-blog\/uploads\/2013\/12\/S3-RoleBasedAccess-New-Page-2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-11180\" title=\"S3-RoleBasedAccess \" src=\"\/blog\/wp-ttn-blog\/uploads\/2013\/12\/S3-RoleBasedAccess-New-Page-2.png\" alt=\"S3-RoleBasedAccess \" width=\"2241\" height=\"1118\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">To accomplish this we followed the steps mentioned below<\/p>\n<ol>\n<li style=\"text-align: justify;\"><span style=\"color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, 'Nimbus Sans L', sans-serif; font-style: normal;\">Created an AWS Role named &#8220;Worker&#8221; which EC2 instances can assume with no specific permission policy.<\/span><\/li>\n<p>A role in AWS IAM defines the permissions for service requests and it is assumed by AWS resources like EC2 Instance. The benefit of using roles was that we didn&#8217;t have to configure S3 separately on the instance. S3 in this case used ROLE credentials which are temporary and rotated automatically.<\/p>\n<li style=\"text-align: justify;\"><span style=\"color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, 'Nimbus Sans L', sans-serif; font-style: normal;\">Created an EC2 instance with Role &#8220;Worker&#8221; and Ip address : 54.254.196.37<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, 'Nimbus Sans L', sans-serif; font-style: normal;\">Added policy to our bucket &#8220;com.intelligrape.rolebasedaccess.test&#8221; which only allows EC2 instances with Role &#8220;worker&#8221; and IP address &#8220;54.254.196.37&#8221; to access the file named &#8220;SIPD&#8221;.<\/span><\/li>\n<\/ol>\n<pre><code> \r\n\"Version\": \"2008-10-17\",\r\n\"Id\": \"Policy1388257451238\",\r\n\"Statement\": [ \r\n      { \"Sid\": \"1232343455\",\r\n        \"Effect\": \"Allow\",\r\n        \"Principal\": { \"AWS\": \"arn:aws:iam::10987654321:role\/worker\" },    \/\/arn:aws:iam::accountnumber:role\/rolename\r\n        \"Action\": \"s3:GetObject\",      \/\/ actions allowed, only allowed to fetch object.\r\n        \"Resource\": \"arn:aws:s3:::com.intelligrape.rolebasedaccess.test\/SIPD\", \/\/\"arn:aws:s3:::bucketname\/file\"\r\n        \"Condition\": {\r\n               \"IpAddress\": {\"aws:SourceIp\": \"54.254.196.37\/32\"}     \/\/ Ip address to allow access to\r\n                     } \r\n               } \r\n         ] \r\n}\r\n<\/code><\/pre>\n<p>(Do not forget to remove the comments before putting it to use.)<br \/>\nAnd its done, with this we were able to limit access to S3 bucket to EC2 instance with specific IP address.<\/p>\n<p>More about IAM Roles can be learned at <a href=\"http:\/\/aws.amazon.com\/iam\/faqs\/#iam_role_management_anchor\" title=\"AWS IAM Documentation page\">AWS Documentation Page.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of our projects, we came across a requirement where we were required to fetch a file containing important data from S3 in order to use it. Due to Security concerns we were not keen on storing access keys on the EC2 instance. Which basically meant that we could not configure S3 on that [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":93},"categories":[1174],"tags":[248,1256,1167,1254,1255,670],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11175"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11175"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11175\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}