<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Raj Pandurangan on Medium]]></title>
        <description><![CDATA[Stories by Raj Pandurangan on Medium]]></description>
        <link>https://medium.com/@rajganeshp?source=rss-75e8953082fd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*-G9-9hP7Cv3yGYaX.jpg</url>
            <title>Stories by Raj Pandurangan on Medium</title>
            <link>https://medium.com/@rajganeshp?source=rss-75e8953082fd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 08:55:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rajganeshp/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[AWS Lab: EC2 Access to S3 using IAM Role]]></title>
            <link>https://medium.com/@rajganeshp/aws-lab-ec2-access-to-s3-using-iam-role-cedefbbbc118?source=rss-75e8953082fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/cedefbbbc118</guid>
            <dc:creator><![CDATA[Raj Pandurangan]]></dc:creator>
            <pubDate>Thu, 12 Jan 2023 17:26:07 GMT</pubDate>
            <atom:updated>2023-01-12T17:26:07.052Z</atom:updated>
            <content:encoded><![CDATA[<h3>Scenario:</h3><p>An application running on an Amazon EC2 instance needs to access an Amazon S3 bucket to read and write files. However, you do not want to hardcode the AWS access key and secret access key in the EC2 instance.</p><h3>Solution:</h3><p>You can use an IAM role to grant the EC2 instance the necessary permissions to access the S3 bucket.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jSTlALDvaGAmiOLb" /></figure><p>To set this up, you would create an IAM role with permissions to access the S3 bucket and then attach the role to the EC2 instance. The EC2 instance can then use the role’s temporary security credentials to access the S3 bucket.</p><p>To create an IAM role and attach it to an Amazon Elastic Compute Cloud (EC2), and then allow the EC2 instance to access an Amazon Simple Storage Service (S3)bucket using the command line. Here’s an example of how you might do this:</p><p>First of all, please make sure to configure the AWS CLI.</p><h4>Step 1:</h4><p>Once the CLI is configured, you can create an IAM policy using the create-policy command. For example, to create a policy called &quot;s3-access-policy.json&quot; with permissions to access S3, you might use a command like this:</p><pre>aws iam create-policy --policy-name s3-access-policy --policy-document file://s3-access-policy.json</pre><p>The s3-access-policy.json file in this example would contain a JSON document describing the permissions that you want to grant to the role. You can find more information on creating policy documents at <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html">Policies and permissions in IAM</a></p><p>Here’s an example policy document that would allow the policy to read and write objects in an S3 bucket called my-s3-bucket1232023:</p><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [{<br>        &quot;Effect&quot;: &quot;Allow&quot;,<br>        &quot;Action&quot;: [<br>            &quot;s3:ListBucket&quot;,<br>            &quot;s3:GetObject&quot;,<br>            &quot;s3:PutObject&quot;<br>        ],<br>        &quot;Resource&quot;: [<br>            &quot;arn:aws:s3:::my-s3-bucket1232023&quot;,<br>            &quot;arn:aws:s3:::my-s3-bucket1232023/*&quot;<br>        ]<br>    }]<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/0*U1D1h093bw9tl5Ot" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fslNks2RQ9UdTBHe" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XnyoHy8DwMg6A02H" /></figure><h4>Step 2:</h4><p>Create an policy which is an JSON file that defines trust relationship of IAM role like here we are creating ec2-assume-policy.json file which would contain a JSON document describing the permissions that you want to grant to the role.</p><p>Here’s an example policy document that would allow the role to be assumed by EC2 instances:</p><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [{<br>        &quot;Effect&quot;: &quot;Allow&quot;,<br>        &quot;Principal&quot;: {<br>            &quot;Service&quot;: &quot;ec2.amazonaws.com&quot;<br>        },<br>        &quot;Action&quot;: &quot;sts:AssumeRole&quot;<br>    }]<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/564/0*xsFLcI7cGStGHWmg" /></figure><h4>Step 3:</h4><p>Once the trust policy has been created, you can create an IAM role using the create-role command. you can create an IAM Role using &#39;<strong>create-role</strong>&#39; command from <a href="https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iam/create-role.html">aws-cli-create-role-command-docs</a></p><pre>aws iam create-role --role-name MyEC2RoleToAccessS3 --assume-role-policy-document file://ec2-assume-policy.json</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-q5GVY9Qe4z8vVvF" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TTth7MIeODZd9SPE" /></figure><h4>Step 4:</h4><p>Once the role has been created, you can attach it to an EC2 Instance using the attach-role-policy command as seen below.</p><pre>`aws iam attach-role-policy --role-name MyEC2RoleToAccessS3 --policy-arn &quot;arn:aws:iam::642434777320:policy/s3-access-policy&quot;`</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aia_5_VlwF5Gg44H" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HejvVuegD7nRqART" /></figure><h4>Step 5:</h4><p>Call the create-instance-profile command, followed by add-role-to-instance-profile command to create the IAM Instance profile MyEC2RoleToAccessS3</p><pre>aws iam create-instance-profile --instance-profile-name MyEC2RoleToAccessS3InstProfile<br>aws iam add-role-to-instance-profile --role-name MyEC2RoleToAccessS3 --instance-profile-name MyEC2RoleToAccessS3InstProfile</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QES51t5HYcOrbh4y" /></figure><h4>Step 6:</h4><p>Finally, attach the IAM role to an existing EC2 instance that was originally launched without an IAM role using the associate-iam-instance-profile command to attach the instance profile MyEC2RoleToAccessS3 for the newly created IAM Role, MyEC2RoleToAccessS3. For example:</p><pre>aws ec2 associate-iam-instance-profile --instance-id i-0963e29cdd7ce325f --iam-instance-profile Name=MyEC2RoleToAccessS3InstProfile</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*EbqMBgknE5Gvs7BY" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*krWr9FNCDMVRjjqG" /></figure><p>After performing all these 6 steps, an EC2 instance will be associated using given IAM Role MyEC2RoleToAccessS3, and Now, the EC2 instance will have the necessary permissions to access the S3 bucket through the IAM role.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qEyWWK1ynmywjDeG" /></figure><p>As we have given permission for only accessing the specific bucket (<strong>my-s3-bucket1232023</strong>), that’s why we are only available to access that and not able to access any other bucket except this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yXLkoy-QXYaRCHF8" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cedefbbbc118" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>