How to use AWS Cross Account access for S3 file downloads

At our company, we have many requirements for enabling cross-account access with AWS. This requirement was around for usage of 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 platform also includes 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. As 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 role 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 to 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 its 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:

python download_from_s3_via_assume_role.py -a 111101111111 -r client -b my-custom -p "some-prefix/which-has-files/"




Comments

Popular posts from this blog

SSH using Chrome Secure Shell app with SSH identity (private key and public key)

Load Testing using Apache Bench - Post JSON API

NGinx + Gunicorn + Flask-SocketIO based deployment.