Assume Role Way
- create a aws user. This user doesn’t has any policy.
2. After created user account, it should tell you the ACCESS_ID and ACCESS_SECRET, copy that to somewhere.
3. create iam role. This role needs a policy, which has access to s3 bucket
4. This role should also have trust relationship with user account we’ve just created.
5. In local, run below command.
AWS_ACCESS_KEY_ID=xxxx AWS_SECRET_ACCESS_KEY=xxxx aws sts assume-role --role-arn ${assume_role_arn} --role-session-name "RoleSession1"
Then it will output assume role key/secret/session_token. In order to achieve this, just add assuming this account in this role.
6. Copy thekey/secret/session_token and run below command, it executes s3 operations.
AWS_ACCESS_KEY_ID=XXX AWS_SECRET_ACCESS_KEY=XXX AWS_SESSION_TOKEN=XXX aws s3 ls s3://mybucket AWS_ACCESS_KEY_ID=XXX AWS_SECRET_ACCESS_KEY=XXX AWS_SESSION_TOKEN=XXX aws s3 cp /tmp/test.txt s3://mybucket
User Way
We can create a user, the user directly has policy to access S3 bucket. ‘
Then we can directly run below command to access to S3 bucket by the user credential, instead of assumeRole. But this way is not recommended way.