Skip to content
Application identity

Configure AWS Workload Identity

Authenticate AWS workloads to Keycard with an IAM-issued identity token.

An AWS workload gets a short-lived token for its IAM role from AWS STS, with your Zone as its audience. It sends the token to Keycard along with the Client ID of the Application credential it authenticates as. Keycard checks the token’s signature with your account’s public keys, then checks its audience, subject, and claims. This works on any compute with an IAM role, such as Lambda, ECS, EC2, and EKS pods.

  • Familiarity with the concepts in Workload Identity, and with AWS’s outbound identity federation docs
  • A Keycard Zone and an Application for the workload
  • Your Zone issuer URL, from Settings → Connection in Keycard Console
  • The Keycard CLI, signed in with keycard auth signin --org <org-id>
  • An IAM role for the workload, and permission to manage IAM in its account

Outbound identity federation is off by default. Enable it once per AWS account and note the issuer URL it returns:

Terminal window
aws iam enable-outbound-web-identity-federation

The IssuerIdentifier looks like https://<issuer-id>.tokens.sts.global.api.aws. To look it up later, run aws iam get-outbound-web-identity-federation-info.

Each AWS account has its own issuer. Create one Provider per account:

Terminal window
keycard agent api /zones/<zone-id>/providers --org <org-id> -X POST -d '{
"name": "AWS <account-id>",
"identifier": "https://<issuer-id>.tokens.sts.global.api.aws",
"protocols": {
"oauth2": {
"issuer": "https://<issuer-id>.tokens.sts.global.api.aws"
}
}
}'

Keycard discovers the account’s signing keys from the issuer.

  1. Add the credential to the Application

    Terminal window
    keycard agent api /zones/<zone-id>/application-credentials --org <org-id> -X POST -d '{
    "type": "token",
    "version": 2,
    "application_id": "<application-id>",
    "provider_id": "<aws-provider-id>",
    "subject": "arn:aws:iam::<account-id>:role/<role-name>"
    }'
    Field Value
    subject The ARN of the role that requests the token. If the role has a path, include it: arn:aws:iam::<account-id>:role/<path>/<role-name>.
    allowed_audience Omitted, so it defaults to your Zone issuer URL. The workload requests its token with that audience.
    authentication_condition Optional. The issuer already limits tokens to your account.

    For an example token and every claim AWS includes, see AWS’s Understanding token claims.

  2. Copy the credential’s Client ID

    The response’s identifier is the credential’s Client ID. The workload sends it as client_id. It is not a secret.

  1. Allow the role to request identity tokens

    Attach this policy to the workload’s role. It limits the role to ES384-signed tokens for your Zone:

    {
    "Version": "2012-10-17",
    "Statement": [
    {
    "Effect": "Allow",
    "Action": "sts:GetWebIdentityToken",
    "Resource": "*",
    "Condition": {
    "ForAllValues:StringEquals": {
    "sts:IdentityTokenAudience": "<keycard-issuer-url>"
    },
    "Null": {
    "sts:IdentityTokenAudience": "false"
    },
    "StringEquals": {
    "sts:SigningAlgorithm": "ES384"
    }
    }
    }
    ]
    }
  2. Set environment variables

    Set these on the workload, for example in the Lambda configuration or the ECS task definition:

    Variable Value
    KEYCARD_ISSUER Your Zone issuer URL
    KEYCARD_CLIENT_ID The credential’s Client ID
    KEYCARD_RESOURCE_URL The Resource the workload needs a token for
  3. Request an AWS token and exchange it

    Request a token with your Zone issuer URL as the audience and ES384 as the signing algorithm. Keycard also accepts RS256. Then send it as client_assertion, with the credential’s Client ID as client_id, in a client_credentials grant:

    Terminal window
    pip install boto3 keycardai-oauth
    import os
    import boto3
    from keycardai.oauth import Client, GrantType
    aws_token = boto3.client("sts", region_name=os.environ["AWS_REGION"]).get_web_identity_token(
    Audience=[os.environ["KEYCARD_ISSUER"]],
    SigningAlgorithm="ES384",
    )["WebIdentityToken"]
    with Client(os.environ["KEYCARD_ISSUER"]) as client:
    response = client.client_credentials_grant(
    client_assertion=aws_token,
    client_assertion_type=GrantType.JWT_BEARER_CLIENT_ASSERTION,
    client_id=os.environ["KEYCARD_CLIENT_ID"],
    resource=os.environ["KEYCARD_RESOURCE_URL"],
    )
    access_token = response.access_token

Run the workload. The exchange returns an access token, and a credentials:issue event shows on the Application’s Activity tab in Keycard Console.

Keycard returns invalid_client with Invalid client assertion when the signature, audience, subject, or condition doesn’t match. The Audit Log records details on the failure. Decode the token with the workload’s role to compare its claims against the credential:

Terminal window
aws sts get-web-identity-token --audience "$KEYCARD_ISSUER" --signing-algorithm ES384 \
--query WebIdentityToken --output text \
| jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson | {iss, aud, sub}'
  • OutboundWebIdentityFederationDisabledException from AWS: outbound identity federation isn’t enabled in the account the role belongs to. Run aws iam enable-outbound-web-identity-federation there.
  • iss doesn’t match the Provider: the role is in a different account than the Provider. Register a Provider for that account’s issuer.
  • sub doesn’t match: the subject is the role ARN, including any path. It is not the assumed-role session ARN.