Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save baydakovss/a8c05c485fbe90420da7b4b3677089db to your computer and use it in GitHub Desktop.
Save baydakovss/a8c05c485fbe90420da7b4b3677089db to your computer and use it in GitHub Desktop.
Assume a specific IAM role with a container via docker-compose
#!/bin/bash
# Assume the role using locally configured AWS credentials, then pass the assumed role to the container via environment
# variables!
ASSUMED_ROLE=$(aws sts assume-role \
--role-arn "arn:aws:iam::000000000000:role/our-role-to-assume" \
--role-session-name "session_name" \
--output text)
# 'docker-compose exec' seems to set the shell width incorrectly, here we overcome that by manually setting COLUMNS and
# LINES!
docker-compose exec \
-e COLUMNS=$(tput cols) \
-e LINES=$(tput lines) \
-e AWS_ACCESS_KEY_ID=$(echo $ASSUMED_ROLE | awk '{print $5}') \
-e AWS_SECRET_ACCESS_KEY=$(echo $ASSUMED_ROLE | awk '{print $7}') \
-e AWS_SESSION_TOKEN=$(echo $ASSUMED_ROLE | awk '{print $8}') \
container_name bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment