aws-secretsmanager-get-secrets GitHub Action

repository·main·Indexed 41 days ago

https://github.com/aws-actions/aws-secretsmanager-get-secrets

GitHub Action version 3.0.0 for retrieving secrets from AWS Secrets Manager and injecting them as masked environment variables. Supports retrieval by name, ARN, or prefix, with options to parse JSON secrets into individual variables and customize name transformations (uppercase, lowercase, or none).

Tokens
2.6K
Snippets
10
Records
18
Agent score
50%

What's inside aws-secretsmanager-get-secrets

  1. Parse JSON secrets into environment variables

    main

    By default, parse-json-secrets is false, and the entire secret value is set as a single environment variable.

    Set parse-json-secrets: true to create individual environment variables for every key-value pair within a JSON secret.

    Naming Convention: When parsing JSON, the environment variable name is constructed as {SECRET_NAME}_{JSON_KEY_NAME}.

    Warning: If your JSON uses case-sensitive keys that result in duplicate environment variable names (e.g., "name" and "Name"), the action will fail. In such cases, set parse-json-secrets: false and parse the JSON manually in a subsequent step.

    - name: Get Secrets and Parse JSON
      uses: aws-actions/aws-secretsmanager-get-secrets@v3
      with:
        secret-ids: |
          test/secret
          ,secret2
        parse-json-secrets: true
  2. Configure AWS credentials for aws-secretsmanager-get-secrets

    main

    Before using this action, you must configure AWS credentials and the AWS Region in your GitHub environment. It is recommended to use the configure-aws-credentials action to assume a role directly using a GitHub OIDC provider to use short-lived credentials.

    The IAM role assumed by the action requires the following permissions:

    • GetSecretValue on the target secrets.
    • ListSecrets on all secrets.
    • Decrypt on the KMS key (optional, only if secrets are encrypted with a customer-managed key).
  3. Use aws-secretsmanager-get-secrets in a GitHub workflow

    main

    Add a step to your workflow using aws-actions/aws-secretsmanager-get-secrets@v3. You can retrieve secrets by their name, ARN, or name prefix. You can also map secrets to specific environment variable names (aliases) or parse JSON secrets into individual environment variables.

    - name: Step name
      uses: aws-actions/aws-secretsmanager-get-secrets@v3
      with:
        secret-ids: |
          secretId1
          ENV_VAR_NAME, secretId2
        name-transformation: (Optional) uppercase|lowercase|none
        parse-json-secrets: (Optional) true|false
        auto-select-family-attempt-timeout: (Optional) positive integer
  4. Configure name-transformation

    main

    By default, the action transforms secret names into environment variable names using only uppercase letters, numbers, and underscores, ensuring they do not start with a number. You can customize this behavior:

    • uppercase (default): Converts letters to uppercase.
    • lowercase: Converts letters to lowercase.
    • none: Preserves the original case of the letters.
  5. Handle JSON secrets and nested environment variables

    main

    If a secret value is a valid JSON object, the action can automatically parse it and inject its keys as nested environment variables.

    Behavior:

    • The action recursively traverses the JSON object.
    • Nested keys are appended to the parent environment variable name using underscores.
    • Example: A secret named DATABASE containing {"host": "localhost"} will result in an environment variable DATABASE_HOST=localhost.

    Note: Only JSON objects (not arrays or nulls) are treated as injectable secret maps.

  6. Configure secret-ids and aliases

    main

    The secret-ids parameter accepts Secret ARNs, names, and name prefixes.

    Mapping to Environment Variables (Aliases)

    To set a specific environment variable name, provide the name followed by a comma before the secret ID:

    • ENV_VAR_1, secretId creates an environment variable named ENV_VAR_1 from secretId.

    Using Prefixes

    To retrieve all secrets matching a pattern, use a prefix followed by an asterisk (minimum 3 characters):

    • dev* matches all secrets starting with dev. (Maximum 100 secrets retrieved).
    • Note: If a prefix matches multiple secrets and you have also specified an alias, the action will fail.

    Blank Aliases

    • If parse-json-secrets: true and you use a blank alias (e.g., ,secretId), the environment variables will be named after the JSON keys directly (the secret name is not included in the variable name).
    • If parse-json-secrets: false and you use a blank alias (e.g., ,secretId), the action behaves as if no alias was specified (using the transformed secret name).
    secret-ids: |
      exampleSecretName
      arn:aws:secretsmanager:us-east-2:123456789012:secret:test1-a1b2c3
      dev*
      ENV_VAR_NAME, secretId2
      ,secret2
  7. Configure auto-select-family-attempt-timeout

    main
    The auto-select-family-attempt-timeout parameter (default 1000) specifies the timeout in milliseconds for attempting to connect to the first IP address in a dual-stack DNS lookup. This is useful when GitHub Action workers are geographically distant from the AWS region. The value must be a positive integer greater than or equal to 10.
  8. Transform secret names into environment variable names

    main

    The action converts AWS Secret names into valid GitHub Actions environment variables. By default, names are converted to UPPERCASE.

    Rules for valid environment variable names:

    • Must consist only of uppercase letters, digits, and underscores.
    • Cannot begin with a number (if a secret name starts with a digit, an underscore is prepended).
    • Invalid characters are replaced with underscores.

    You can customize this behavior using a nameTransformation function (e.g., uppercase, lowercase, or none).

  9. Use aliases to prevent environment variable collisions

    main

    When retrieving secrets, you can provide an alias to ensure the resulting environment variable has a specific name. This is critical if multiple secrets would otherwise transform into the same environment variable name.

    Format: ALIAS,SECRET_NAME_OR_ARN

    Example: MY_APP_SECRET,prod/api/key will result in an environment variable named MY_APP_SECRET.

    Constraints:

    • The alias must be a valid environment variable name (uppercase, numbers, underscores).
    • If you use an alias with a prefix search (e.g., ALIAS,prefix/*), the alias must be unique to the search result.
  10. Retrieve secrets by name and ARN

    main

    You can mix and match different ways to identify secrets in the secret-ids list.

    - name: Get secrets by name and by ARN
      uses: aws-actions/aws-secretsmanager-get-secrets@v3
      with:
        secret-ids: |
          exampleSecretName
          arn:aws:secretsmanager:us-east-2:123456789012:secret:test1-a1b2c3
          0/test/secret
          /prod/example/secret
          SECRET_ALIAS_1,test/secret
          SECRET_ALIAS_2,arn:aws:secretsmanager:us-east-2:123456789012:secret:test2-a1b2c3
          ,secret2