Explore! Cross Accounts Access with IAM Roles from EC2 to S3 with Custom Managed Key
Tags: awsec2, awss3, awskms, awsiam
Published: 2022-06-25 19:31:17

“ I have checked the documents of AWS to resolve the issue of accessing encrypted s3 bucket from ec2 server in cross accounts. So I checked for various solutions and found out that it can be possible using IAM roles and policies. Also adding the s3 bucket access role in KMS. Pricing of Amazon EC2 depends on usage of instance, S3 bucket as per storage size and  KMS custom key costs $1 per key.”

AWS Identity and Access Management (IAM) provides fine-grained access control across all of AWS. With IAM, you can specify who can access which services and resources, and under which conditions. With IAM policies, you manage permissions to your workforce and systems to ensure least-privilege permissions. IAM is an AWS service that is offered at no additional charge. To learn more, read the [IAM](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html).

In this post, you will get to know how to access cross accounts with IAM roles from ec2 to s3 with a custom managed key. Here I have used an ec2 server, a s3 bucket and a custom managed key which is able to add a key user as a s3 role to access s3 bucket from another account ec2 server.

#Prerequisites
You’ll need an Amazon EC2 Server for this post. [Getting started with amazon EC2](https://aws.amazon.com/ec2/getting-started/) provides instructions on how to launch an EC2 Server.

You’ll need an Amazon Simple Storage Service for this post. [Getting started with Amazon Simple Storage Service](https://aws.amazon.com/s3/getting-started/) provides instructions on how to create a bucket in simple storage service.

You’ll need an AWS Key Management Service for this post. [Getting started with AWS KMS](https://aws.amazon.com/kms/getting-started/) provides instructions on how to create a custom key. For this blog, I assume that I have an ec2 server, a s3 bucket and a custom key created.

#Architecture Overview
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9mjumripvj2lngbdaihf.png)
The architecture diagram shows the overall deployment architecture with data flow, amazon ec2, amazon s3, aws kms and IAM role.

#Solution overview
The blog post consists of the following phases:
1. Create IAM Policy and Role of S3 Bucket Access for Cross Account  
2. Create IAM Role for EC2 Service in Another Account
3. Creating of Profile with Role ARN and Testing of Fetching Data from S3 Bucket to EC2 Server

I have a custom key, a ec2 server and a s3 bucket as below →
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vciz4ikpudcrtrzdi1a3.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7m2ajxbmqb3zsur18dvq.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ls7zskfacvmeq1306p99.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1lijodjxdxp0kumx27r.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/csi77i9hc66c5w17u1mo.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8o4m0mau6d4pji4d9pld.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhp2op6m4oikfyn35ydt.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/az6mlo4bmkxvdj748emb.png)

##Phase 1: Create IAM Policy and Role of S3 Bucket Access for Cross Account   
1. Open the IAM console and create an IAM role for a trusted entity for another account. Also create a policy for s3 bucket access and attach it to the role.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v797qqgxemow8wtj4g3j.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qstplnaxl4ajpisvllgu.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkaviqitsmzbi9smx2fw.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4r7n53mwz8fjk199bx8.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjoy8wzc2i8pavqreai4.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0r9si2ghfw8js9xrplhr.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ytr4f1mjqk21s4xj9xf9.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4a2w2yx9n3apn58u0bjq.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plv1apdoo3uxtm2p9v9w.png)

##Phase 2: Create IAM Role for EC2 Service in Another Account 
1. Open the IAM console and create an IAM role for ec2 service. Add a custom inline policy as sts assume role with role arn create in first account. Then add a role to the ec2 server.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfgggjh4pi31m3c2a3t4.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5y1kxwa48jb28g1e6hqy.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ysnc06ydod0c9wwsec9d.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cwyzf6lezc1ymafmp0qc.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z24uccge8d8jxk76vu4s.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xrf07sgvks17344hn948.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57zdu5xafczt4bfhei96.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sc7x3l69uwgmsi2db82c.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mch06wa2vonozxj2te8d.png)

##Phase 3: Creating of Profile with Role ARN and Testing of Fetching Data from S3 Bucket to EC2 Server
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sgu46kefdqoi9llhvsji.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bts7fscuz8rzehuyx9w.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/awncyn5v61fh7p9cfact.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n910l3b6v75zf2ctidnh.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9j18ng6jlm9t4e77wrf.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxfmokrdc2liy0tc4njo.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7x33grmp6bomo65ppq3v.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lugcm89jbhgizlzgg8fc.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/damx9phe4c9ojy4xo51f.png)

#Clean-up
Delete EC2 Instance, KMS Custom Key, IAM Role, IAM Policy, S3 Bucket.

#Pricing
I review the pricing and estimated cost of this example. 

Cost of EC2 Instance = $0.09

Cost of Key Management Service = $1.0

Cost of Data Transfer = $0.0

Cost of S3 = $0.01

Total Cost = $(0.09 + 1.0 + 0.0 + 0.01) = $1.1

#Summary
In this post, I showed “how to access cross accounts with IAM roles from ec2 to s3 with a custom managed key”.

For more details on IAM, Checkout Get started IAM, open the [IAM console](https://us-east-1.console.aws.amazon.com/iamv2/home?region=us-east-1#/home). To learn more, read the [IAM documentation](https://docs.aws.amazon.com/iam/).

Thanks for reading!

Connect with me: [Linkedin](https://www.linkedin.com/in/gargee-bhatnagar-6b7223114)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38sdi4nsp89fnui8h7dk.png)