<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pruthvi</title>
    <description>The latest articles on DEV Community by Pruthvi (@pruthvi03).</description>
    <link>https://dev.to/pruthvi03</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1131734%2Feeda7745-633e-4c77-a6ab-981cd3af2d2e.png</url>
      <title>DEV Community: Pruthvi</title>
      <link>https://dev.to/pruthvi03</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pruthvi03"/>
    <language>en</language>
    <item>
      <title>Cross account resource access in IAM</title>
      <dc:creator>Pruthvi</dc:creator>
      <pubDate>Thu, 24 Aug 2023 07:19:48 +0000</pubDate>
      <link>https://dev.to/distinction-dev/cross-account-resource-access-in-iam-3529</link>
      <guid>https://dev.to/distinction-dev/cross-account-resource-access-in-iam-3529</guid>
      <description>&lt;p&gt;For some AWS services, you can grant cross-account access to your resources using IAM. To do this, you can attach a resource policy directly to the resource that you want to share, or use a role as a proxy.&lt;/p&gt;

&lt;p&gt;To share the resource directly, the resource that you want to share must support &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction_access-management.html#intro-access-resource-based-policies"&gt;resource-based policies&lt;/a&gt;. Unlike an identity-based policy for a role, a resource-based policy specifies who (which principal) can access that resource.&lt;/p&gt;

&lt;p&gt;The following list includes some of the AWS services that support resource-based policies. For a complete list of the growing number of AWS services that support attaching permission policies to resources instead of principals, see &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_aws-services-that-work-with-iam.html"&gt;AWS services that work with IAM&lt;/a&gt; and look for the services that have &lt;strong&gt;Yes&lt;/strong&gt; in the &lt;strong&gt;Resource Based&lt;/strong&gt; column.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Amazon S3 buckets&lt;/strong&gt; — The policy is attached to the bucket, but the policy controls access to both the bucket and the objects in it. For more information, see &lt;a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-access-control.html"&gt;Access Control&lt;/a&gt; in the &lt;em&gt;Amazon Simple Storage Service User Guide&lt;/em&gt;. In some cases, it may be best to use roles for cross-account access to Amazon S3. For more information, see the &lt;a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/example-walkthroughs-managing-access.html"&gt;example walkthroughs&lt;/a&gt; in the &lt;em&gt;Amazon Simple Storage Service User Guide&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Amazon Simple Notification Service (Amazon SNS) topics&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Amazon Simple Queue Service (Amazon SQS) queues&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Solution
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Delegating AWS permissions in a resource-based policy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If a resource grants permissions to principals in your account, you can then delegate those permissions to specific IAM identities. Identities are users, groups of users, or roles in your account. You delegate permissions by attaching a policy to the identity. You can grant up to the maximum permissions that are allowed by the resource-owning account.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In cross account access, a principal needs an &lt;code&gt;Allow&lt;/code&gt; in the identity policy &lt;strong&gt;and&lt;/strong&gt; the resource-based policy.&lt;/p&gt;

&lt;p&gt;Assume that a resource-based policy allows all principals in your account full administrative access to a resource. Then you can delegate full access, read-only access, or any other partial access to principals in your AWS account. Alternatively, if the resource-based policy allows only list permissions, then you can delegate only list access. If you try to delegate more permissions than your account has, your principals will still have only list access.&lt;/p&gt;

&lt;p&gt;For example, assume that you manage &lt;code&gt;AccountA&lt;/code&gt; and &lt;code&gt;AccountB&lt;/code&gt;. In AccountA, you have an Amazon SQS Queue named &lt;code&gt;QueueA&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;You attach a resource-based policy to &lt;code&gt;QueueA&lt;/code&gt; that allows all principals in AccountB to perform SQS:SendMessage Action. They can send message to QueueA&lt;/p&gt;

&lt;p&gt;Here we can use &lt;strong&gt;&lt;a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sqs-queuepolicy.html#cfn-sqs-queuepolicy-policydocument"&gt;AWS::SQS::QueuePolicy&lt;/a&gt; or &lt;a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sqs-queueinlinepolicy.html"&gt;AWS::SQS::QueueInlinePolicy&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Amazon SQS Queue Policy&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The following sample is a queue policy that allows AWS account 444455556666(AccountA) to send and receive messages on queue queueA. You add the policy to the resources section of your template.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;SampleSQSPolicy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
  &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AWS::SQS::QueuePolicy&lt;/span&gt;
  &lt;span class="na"&gt;Properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# 444455556666 :- AccountA Id &lt;/span&gt;
    &lt;span class="na"&gt;Queues&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;https://sqs:us-east-2.amazonaws.com/444455556666/queueA"&lt;/span&gt;
    &lt;span class="na"&gt;PolicyDocument&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
      &lt;span class="na"&gt;Statement&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;Action&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
            &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SQS:SendMessage"&lt;/span&gt;
          &lt;span class="na"&gt;Effect&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Allow"&lt;/span&gt;
          &lt;span class="na"&gt;Resource&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;arn:aws:sqs:us-east-2:444455556666:queueA"&lt;/span&gt;
          &lt;span class="na"&gt;Principal&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  
            &lt;span class="na"&gt;AWS&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
              &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;111122223333"&lt;/span&gt; &lt;span class="c1"&gt;# AccountB Id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;AccountA gives AccountB gives access to QueueA by naming AccountB as a principal in the resource-based policy. As a result, AccountB is authorized to perform SQS:SendMessage action on QueueA, and the AccountB administrator can delegate access to its users in AccountB.&lt;br&gt;
The AccountB root user has all of the permissions that are granted to the account. Therefore, the root user has full access to BucketA.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In AccountB, attach a policy such that any resource(ex. lambda) can perform SQS:SendMessage action.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;iamRoleStatements&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;Effect&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Allow"&lt;/span&gt;
      &lt;span class="na"&gt;Action&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sqs:SendMessage"&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sqs:ReceiveMessage"&lt;/span&gt;
      &lt;span class="na"&gt;Resource&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
         &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;arn:aws:sqs:us-east-2:444455556666:queueA"&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more information about complex uses of roles, such as granting access to applications and services, see &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_common-scenarios.html"&gt;Common scenarios for roles: Users, applications, and services&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;🚨 Give access only to entities you trust, and give the minimum level of access necessary. Whenever the trusted entity is another AWS account, any IAM principal can be granted access to your resource. The trusted AWS account can delegate access only to the extent that it has been granted access; it cannot delegate more access than the account itself has been granted.&lt;/p&gt;

&lt;h3&gt;
  
  
  More information about Principal in resource based policy:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_principal.html"&gt;AWS JSON policy elements: Principal - AWS Identity and Access Management&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Reference:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies-cross-account-resource-access.html"&gt;Cross account resource access in IAM - AWS Identity and Access Management&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sqs-queuepolicy.html"&gt;AWS::SQS::QueuePolicy - AWS CloudFormation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sqs-queueinlinepolicy.html"&gt;AWS::SQS::QueueInlinePolicy - AWS CloudFormation&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
