How to use AWS Cross Account access for S3 file downloads
Our company has many requirements for enabling cross-account access with AWS. This requirement was around for using S3 and accessing the data from S3 for our in-house Ingestion Service. To give a brief about Ingestion Service, it’s part of our Datamanagement ecosystem which is used to ingest data from different source platforms and dump the data to S3. These different source platforms also include AWS S3.
Our existing service on our AWS EC2, when it requires access to S3 files, stored in a different AWS account, we find it difficult to keep updating our access/secret keys. A couple of companies have a requirement to rotate IAM user credentials over a defined period.
We have utilized the cross-account IAM role access strategy, wherein the roles can now assume roles via temporary credentials (obtained every time we query).
How to set it up?
- Create Role: Set a role from your current account, which would be shared with the third-party clients. Allow that role to assume roles of accessing the resources, for example, S3 bucket or something which is of the need.
- Role Access: Share the role identification with the client, ideally it's the complete arn of the role, for example, arn:aws:iam::<acount-id>:role/clients/our-client
- Switch Roles: There are a couple of ways to switch roles and access the resources. I will be referring to programmatic access to fetch S3 files.
Switch Role
For programmatic switching the role, we tried to find out the best way to allow our clients to access our services and we caught inspiration from AWS documentation on Switching to an IAM Role (AWS API). This document provided us with insights on how we can leverage the script to trigger the access of data in S3.
The Script for the rescue
To provide a gist of it, here is the script (we modified this to fit fetching of files and downloading wherever needed):How to Execute:
python download_from_s3_via_assume_role.py -a <aws_account_id> -r <aws_role_name> -b <bucket-name> -p <prefix-path>
For example:
Comments
Post a Comment