configure-aws-credentials

repository·main·Indexed 25 days ago

https://github.com/aws-actions/configure-aws-credentials

A GitHub Action (v6.2.3) used to authenticate GitHub Actions workflows to AWS. It supports multiple authentication methods, including OpenID Connect (OIDC) for secure temporary credentials, IAM User static credentials, and role chaining. The action allows for the configuration of named AWS profiles, HTTP proxy settings, custom session tags, and STS retry mechanisms.

Tokens
17.4K
Snippets
23
Records
58
Agent score
84%

What's inside configure-aws-credentials

  1. Quick Start with GitHub OIDC (Recommended)

    main

    The recommended way to authenticate to AWS from GitHub Actions is using OpenID Connect (OIDC). This method provides temporary credentials and avoids the need to store long-lived secrets in GitHub.

    To set this up:

    1. Create an IAM Identity Provider in your AWS account for GitHub OIDC.
    2. Create an IAM Role with a trust policy that allows GitHub Actions to assume it. The trust policy must use sts:AssumeRoleWithWebIdentity and include a condition matching your GitHub organization, repository, and branch/environment.
    3. Attach Permissions to the IAM Role for the specific AWS resources your workflow needs to access.
    4. Configure your Workflow by setting permissions: id-token: write and using the aws-actions/configure-aws-credentials action with role-to-assume and aws-region.
    # Need ID token write permission to use OIDC
    permissions:
      id-token: write
    
    jobs:
      run_job_with_aws:
        runs-on: ubuntu-latest
        steps:
          - name: Configure AWS Credentials
            uses: aws-actions/configure-aws-credentials@v6.2.3
            with:
              role-to-assume: <Role ARN you created in step 2>
              aws-region: <AWS Region you want to use>
          - name: Additional steps
            run: |
              # Your commands that require AWS credentials
              aws sts get-caller-identity
  2. Security Best Practices for AWS Credentials in GitHub Actions

    main

    To secure your AWS credentials within GitHub Actions, follow these recommendations:

    • Use temporary credentials via OIDC whenever possible.
    • Do not store credentials in your repository's code; use GitHub Secrets or AWS Secrets Manager.
    • Grant least privilege to your IAM roles, providing only the permissions necessary for the specific workflow.
    • Monitor activity of the credentials used in your workflows.
    • Rotate long-lived credentials periodically.
    • Exercise caution when running Actions in non-ephemeral environments or triggering workflows on pull_request_target events.
  3. Configure OIDC Authentication for GitHub Actions

    main

    Use GitHub's OIDC provider to obtain short-lived AWS credentials. This is the recommended method for securing deployments.

    To implement OIDC:

    1. Set the id-token: write permission in your GitHub workflow.
    2. Configure an IAM Identity Provider in AWS with:
      • Provider Type: OIDC
      • Provider URL: https://token.actions.githubusercontent.com
      • Audience: sts.amazonaws.com (or your custom audience).
    3. Create an IAM role with a trust policy that uses a Condition to restrict access to specific GitHub repositories/organizations.
    4. Specify the role's ARN in the role-to-assume input of the action.

    Note: Avoid naming your IAM role GitHubActions as it may cause issues.

    - name: Configure AWS Credentials
      uses: aws-actions/configure-aws-credentials@v6.2.3
      with:
        role-to-assume: arn:aws:iam::123456789100:role/my-github-actions-role
  4. Use role chaining with AWS profiles

    main

    When using static IAM User credentials or assuming one role from another while using profiles, you must enable role-chaining: true and provide the previous profile via the AWS_PROFILE environment variable.

    Scenario 1: Static credentials to a role using a profile Set role-chaining: true and pass the profile name in the env block.

    Scenario 2: Chaining multiple roles When assuming a second role from a first role, the second step must set role-chaining: true and use the first role's profile name in its env block.

    # Scenario 1: Static credentials to a role
    - name: Configure AWS Credentials
      uses: aws-actions/configure-aws-credentials@v6.2.3
      with:
        aws-region: us-east-1
        role-to-assume: arn:aws:iam::123456789100:role/my-role
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-profile: MyProfile1
        role-chaining: true
      env:
        AWS_PROFILE: MyProfile1
    
    # Scenario 2: Chaining multiple roles
    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v6.2.3
      with:
        aws-region: us-east-1
        role-to-assume: arn:aws:iam::123456789100:role/my-first-role
        aws-profile: firstRoleInChain
    
    - name: assume second role
      uses: aws-actions/configure-aws-credentials@v6.2.3
      with:
        aws-region: us-east-2
        role-to-assume: arn:aws:iam::987654321000:role/my-second-role
        role-chaining: true
        aws-profile: secondRoleInChain
      env:
        AWS_PROFILE: firstRoleInChain
  5. Configure named AWS profiles

    main

    By default, the action exports credentials as environment variables. To use named AWS profiles instead, provide the aws-profile input. This writes credentials to ~/.aws/credentials and ~/.aws/config files.

    Key behaviors:

    • Profile names cannot contain whitespace, square brackets, or slashes.
    • When aws-profile is set, credentials are not exported as environment variables unless you also set output-env-credentials: true.
    • The action will not overwrite an existing profile unless overwrite-aws-profile is set to true.
    • Caution: Writing to configuration files means credentials persist in the execution environment even after the action cleanup step. Use this only for unusual authentication scenarios.
    • The action preserves existing profile sections in the files, but comments will be lost.
    - name: Configure AWS Credentials
      uses: aws-actions/configure-aws-credentials@v6.2.3
      with:
        aws-region: us-east-1
        aws-profile: MyProfile1
        output-env-credentials: true
  6. Use the action on AWS Self-Hosted Runners

    main

    When running on self-hosted runners (e.g., in EKS or CodeBuild) where credentials are already provided via environment variables:

    • To export existing credentials: Run the action with role-to-assume set to the default role of the container.
    • To assume a different role (Role Chaining): Use the role-chaining: true flag. This allows the action to fetch the default credentials from the environment before assuming the target role.
    • EKS Pod Identity issues: If you encounter errors related to the packed size of session tags, use role-skip-session-tagging: true to disable the tags set by the action.