Skip to content

Instantly share code, notes, and snippets.

@jakebrinkmann
Forked from vranystepan/README.md
Created August 28, 2024 19:53
Show Gist options
  • Select an option

  • Save jakebrinkmann/26abfd176dafad0dbbf017a57a8fa0e9 to your computer and use it in GitHub Desktop.

Select an option

Save jakebrinkmann/26abfd176dafad0dbbf017a57a8fa0e9 to your computer and use it in GitHub Desktop.

Revisions

  1. @vranystepan vranystepan revised this gist May 28, 2019. 2 changed files with 17 additions and 2 deletions.
    16 changes: 16 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,22 @@

    ## Example

    set AWS env. variables

    ```bash
    export ASSUME_ROLE_ARN="arn:aws:iam::000000000000:role/role00001"
    export AWS_ACCESS_KEY_ID="..."
    export AWS_SECRET_ACCESS_KEY="..."
    ```

    optionally you can set session token for MFA

    ```bash
    export AWS_SESSION_TOKEN="..."
    ```

    and then load the helper

    ```bash
    wget "${ASSUME_HELPER_URL}" -O ./init.sh
    . ./init.sh
    3 changes: 1 addition & 2 deletions init.sh
    Original file line number Diff line number Diff line change
    @@ -49,5 +49,4 @@ unset AWS_SESSION_TOKEN

    # export AWS environment variables
    export AWS_CONFIG_FILE
    export AWS_SHARED_CREDENTIALS_FILE
    export AWS_DEFAULT_PROFILE
    export AWS_SHARED_CREDENTIALS_FILE
  2. @vranystepan vranystepan revised this gist May 28, 2019. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions init.sh
    Original file line number Diff line number Diff line change
    @@ -23,14 +23,13 @@ mkdir -p "$(pwd)/.aws"
    # configure aws cli behaviour
    AWS_CONFIG_FILE="$(pwd)/.aws/config"
    AWS_SHARED_CREDENTIALS_FILE="$(pwd)/.aws/credentials"
    AWS_DEFAULT_PROFILE="sub-account"

    cat <<EOT > "${AWS_CONFIG_FILE}"
    [profile main]
    region = eu-central-1
    output = json
    [profile sub-account]
    [profile default]
    role_arn = ${ASSUME_ROLE_ARN}
    source_profile = main
    EOT
    @@ -39,14 +38,16 @@ cat <<EOT > "${AWS_SHARED_CREDENTIALS_FILE}"
    [main]
    aws_access_key_id = ${AWS_ACCESS_KEY_ID}
    aws_secret_access_key = ${AWS_SECRET_ACCESS_KEY}
    aws_session_token = ${AWS_SESSION_TOKEN}
    EOT

    # unset AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY,
    # otherwise AWS_DEFAULT_PROFILE won't work
    unset AWS_ACCESS_KEY_ID
    unset AWS_SECRET_ACCESS_KEY
    unset AWS_SESSION_TOKEN

    # export AWS environment variables
    export AWS_CONFIG_FILE
    export AWS_SHARED_CREDENTIALS_FILE
    export AWS_DEFAULT_PROFILE
    export AWS_DEFAULT_PROFILE
  3. @vranystepan vranystepan revised this gist May 27, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion init.sh
    Original file line number Diff line number Diff line change
    @@ -49,4 +49,4 @@ unset AWS_SECRET_ACCESS_KEY
    # export AWS environment variables
    export AWS_CONFIG_FILE
    export AWS_SHARED_CREDENTIALS_FILE
    export AWS_DEFAULT_PROFILE%
    export AWS_DEFAULT_PROFILE
  4. @vranystepan vranystepan revised this gist May 27, 2019. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions init.sh
    Original file line number Diff line number Diff line change
    @@ -21,9 +21,9 @@ check_empty AWS_SECRET_ACCESS_KEY
    # create temporary directory for aws config & credentials
    mkdir -p "$(pwd)/.aws"
    # configure aws cli behaviour
    export AWS_CONFIG_FILE="$(pwd)/.aws/config"
    export AWS_SHARED_CREDENTIALS_FILE="$(pwd)/.aws/credentials"
    export AWS_DEFAULT_PROFILE="sub-account"
    AWS_CONFIG_FILE="$(pwd)/.aws/config"
    AWS_SHARED_CREDENTIALS_FILE="$(pwd)/.aws/credentials"
    AWS_DEFAULT_PROFILE="sub-account"

    cat <<EOT > "${AWS_CONFIG_FILE}"
    [profile main]
    @@ -44,4 +44,9 @@ EOT
    # unset AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY,
    # otherwise AWS_DEFAULT_PROFILE won't work
    unset AWS_ACCESS_KEY_ID
    unset AWS_SECRET_ACCESS_KEY
    unset AWS_SECRET_ACCESS_KEY

    # export AWS environment variables
    export AWS_CONFIG_FILE
    export AWS_SHARED_CREDENTIALS_FILE
    export AWS_DEFAULT_PROFILE%
  5. @vranystepan vranystepan revised this gist May 27, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -7,3 +7,6 @@ wget "${ASSUME_HELPER_URL}" -O ./init.sh
    . ./init.sh
    aws eks list-clusters --region eu-central-1
    ```

    ## Caveats
    Do not execute this helper as script. As we need to modify environment variables in the current proces - helper has to be sourced with `source` or `.`
  6. @vranystepan vranystepan revised this gist May 27, 2019. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,9 @@
    # Simple AWS IAM role assume for CI/CD environments

    ## Example

    ```bash
    wget "${ASSUME_HELPER_URL}" -O ./init.sh
    . ./init.sh
    aws eks list-clusters --region eu-central-1
    ```
  7. @vranystepan vranystepan created this gist May 27, 2019.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    # Simple AWS IAM role assume for CI/CD environments

    47 changes: 47 additions & 0 deletions init.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/bin/sh

    check_empty () {
    # $1 is variable name

    # Write status
    echo "checking variable ${1} ..."

    # check if variable is empty - unset or ''
    if [ -z "$(eval echo \$"$1")" ]; then
    echo "${1} is empty!"
    exit 1
    fi
    }

    # perform validation
    check_empty ASSUME_ROLE_ARN
    check_empty AWS_ACCESS_KEY_ID
    check_empty AWS_SECRET_ACCESS_KEY

    # create temporary directory for aws config & credentials
    mkdir -p "$(pwd)/.aws"
    # configure aws cli behaviour
    export AWS_CONFIG_FILE="$(pwd)/.aws/config"
    export AWS_SHARED_CREDENTIALS_FILE="$(pwd)/.aws/credentials"
    export AWS_DEFAULT_PROFILE="sub-account"

    cat <<EOT > "${AWS_CONFIG_FILE}"
    [profile main]
    region = eu-central-1
    output = json
    [profile sub-account]
    role_arn = ${ASSUME_ROLE_ARN}
    source_profile = main
    EOT

    cat <<EOT > "${AWS_SHARED_CREDENTIALS_FILE}"
    [main]
    aws_access_key_id = ${AWS_ACCESS_KEY_ID}
    aws_secret_access_key = ${AWS_SECRET_ACCESS_KEY}
    EOT

    # unset AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY,
    # otherwise AWS_DEFAULT_PROFILE won't work
    unset AWS_ACCESS_KEY_ID
    unset AWS_SECRET_ACCESS_KEY