Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (5.6k points)

I have two accounts let's sat A and B, and I have an S3 bucket in Account B and I want EC2 in my Account  A to access the bucket in Account B. I want to achieve this using IAM roles.

Below is the Role in Account B:

AWSTemplateFormatVersion : '2010-09-09'

 Description: 'Cross account role for S3'

 Parameters:

   AccountId:

   Type: String

   Description: Account ID of admin account (containing user to allow)

 Resources:

 CrossAccountRole:

Type: AWS::IAM::Role

Properties:

  AssumeRolePolicyDocument:

    Statement:

      - Effect: Allow

        Action: sts:AssumeRole

        Principal:

          AWS:

            - !Sub arn:aws:iam::${AccountId}:root

  Path: /

  Policies:

    - PolicyName: my-s3-delegate

      PolicyDocument:

        Statement:

          - Effect: Allow

            Action:

              - s3:ListBucket

              - s3:GetObject

            Resource: "*"

  RootInstanceProfile: 

Type: "AWS::IAM::InstanceProfile"

Properties: 

  Path: "/"

  Roles: 

      - 

        Ref: "CrossAccountRole"

After this how can I attach this to my instance which is in Account A?

1 Answer

0 votes
by (12.4k points)

You can simply add a bucket policy in your account B that will allow access to IAM Role used by the instance:

{

    "Version": "2012-10-17",

    "Statement": [

        {

            "Action": [

                "s3:GetObject",

                "s3:ListBucket"

            ],

            "Effect": "Allow",

            "Resource": [

                "arn:aws:s3:::my-bucket",

                "arn:aws:s3:::my-bucket/*"

            ],

            "Principal": {

                "AWS": [

                    "arn:aws:iam::ACCOUNT-A:role/my-ec2-role"

                ]

            }

        }

    ]

}

 Also, make sure that IAM Role has permission to use S3 to access the bucket:

{

    "Version": "2012-10-17",

    "Statement": [

        {

            "Effect": "Allow",

            "Action": [

                "s3:GetObject",

                "s3:ListBucket"

            ],

            "Resource": [

                "arn:aws:s3:::bucket-b",

                "arn:aws:s3:::bucket-b/*"

            ]

        }

    ]

}

Want to become AWS Expert? Come & join AWS Certification.

Related questions

0 votes
1 answer

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

Browse Categories

...