Skip to content

AccessDeniedException when calling ChatSync API in Amazon Q App using Google-based IAM Identity Center Auth

0

Hi, I'm setting up a Q Business application using IAM Identity Center (with Google as the IdP via SSO). I'm trying to call the chat_sync API using temporary credentials assumed via assume_role_with_web_identity, and while the list_applications API works correctly the and chat_sync API call fails with:

botocore.errorfactory.AccessDeniedException: An error occurred (AccessDeniedException) when calling the ChatSync operation: User is not authorized for this service call.

I can access the Q app using web UI successfully without any issues.

Here’s what I’ve implemented:

I assume an IAM role using a Google ID token:

import boto3

def assume_role_with_google_token(google_id_token, role_arn, session_name="google-session"):
    sts_client = boto3.client("sts")

    response = sts_client.assume_role_with_web_identity(
        RoleArn=role_arn,
        RoleSessionName=session_name,
        WebIdentityToken=google_id_token,
        DurationSeconds=3600
    )

    credentials = response['Credentials']
    return {
        "access_key": credentials["AccessKeyId"],
        "secret_key": credentials["SecretAccessKey"],
        "session_token": credentials["SessionToken"],
    }

def call_amazon_q(temp_creds, question):
    client = boto3.client(
        "qbusiness",
        region_name="us-east-1",
        aws_access_key_id=temp_creds["access_key"],
        aws_secret_access_key=temp_creds["secret_key"],
        aws_session_token=temp_creds["session_token"]
    )

    print(client.list_applications())
    response = client.chat_sync(
        applicationId="abc",
        userMessage = question
        )

    return response

print("Hi")



aws_creds = assume_role_with_google_token(your_id_token, role_arn)
print(aws_creds)
message = "What is posh policy"
response = call_amazon_q(aws_creds, message)

IAM policy attached to the role:

json

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "qbusiness:ListApplications",
        "qbusiness:ChatSync"
      ],
      "Resource": "*"
    }
  ]
}

Questions:

  1. Is chat_sync compatible with temporary credentials assumed via assume_role_with_web_identity?
  2. Does ChatSync require the user identity to be explicitly added to the Amazon Q app (even for identity center users)?
  3. Are there any additional conditions or trust policies required for the assumed role when using chat_sync?
  4. Should the applicationId be configured to allow access for federated users explicitly?
  5. Do I need to use a different principal or session configuration when working with Google SSO tokens?
  6. Can this API be used programmatically for users who have never accessed the Q app through the console?
  7. Is there a difference in behavior for chat_sync between identity-based users and identity center-based users?
  8. Any diagnostics/logs I can check in AWS to trace the reason for the AccessDeniedException?

Appreciate any insights or examples from those who've successfully integrated Amazon Q with federated identities. Thanks!

1 Answer
0

The AccessDeniedException you're encountering with the ChatSync operation in Amazon Q Business is related to how user identity is handled for API access. This is a common issue that has been reported by several users.

The key issue is that Amazon Q Business now requires identity-aware sessions for API access, and the approach has changed significantly from previous implementations. Here's what's happening:

  1. You can no longer directly call the ChatSync API with standard IAM credentials, even when using federated identity credentials from assume_role_with_web_identity. The identity of the user needs to be implicitly included in the session.

  2. For ChatSync to work properly, you need to implement a more secure, identity-aware approach:

  • Get an idToken from your SSO Identity Provider (Google in your case)
  • Exchange this token for an identity-aware token from IAM Identity Center
  • Use the IAM Identity Center token to assume an IAM role, obtaining temporary identity-aware session credentials
  • Use these credentials to call the ChatSync API
  1. When you run the ChatSync API with a particular user's credentials for the first time, you might see an error message like "User does not have a subscription for the given application." This is expected behavior, and the user is automatically subscribed to the Amazon Q Business application on this call. Running the command again with the same credentials should work.

  2. Identity-aware sessions must be activated in your organization's management account for Amazon Q Business to authenticate users and call APIs properly.

To resolve this issue, I recommend checking out the AWS sample repository for custom web experiences with Amazon Q Business, which demonstrates the proper implementation pattern. The solution involves a more complex token exchange flow that ensures the user's identity is properly carried through to the Amazon Q Business service.

For users who have never accessed the Q app through the console, they may need to be subscribed to the application first, which can happen automatically on their first API call attempt (though it may result in an initial error that resolves on subsequent attempts).

As for diagnostics, you can verify your current identity using the AWS STS get-caller-identity command to confirm which role you're assuming, and check that the role has the appropriate permissions.
Sources
AccessDeniedException - ChatSync operation (IAM Identity Center) | AWS re:Post
Amazon Q Business chat-sync CLI command always failing | AWS re:Post
Amazon Q Business - chatSync method not accessible via API | AWS re:Post
Implement secure API access to your Amazon Q Business applications with IAM federation user access management | AWS Machine Learning Blog
Troubleshoot Amazon Q Business set up errors with IAM Identity Center | AWS re:Post

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.