View a markdown version of this page

範例:AgentCore CLI 建立的預設閘道和目標的授權 - Amazon Bedrock AgentCore

範例:AgentCore CLI 建立的預設閘道和目標的授權

如果您使用 AgentCore CLI 建立閘道和 Lambda 目標,CLI 會自動為您設定 JWT 型傳入授權和 IAM 型傳出授權。

注意

如果您使用 agentcore invoke 叫用閘道,CLI 會自動處理身分驗證。只有在您想要以程式設計方式叫用閘道 (例如,使用 AWS SDK 或 curl ) 時,才需要以下步驟。

對於程式設計存取,您將使用下列項目授權 :

  • 使用 JWT 的傳入授權 – 從自動為您設定的 Amazon Cognito 授權取得存取權杖,並將其包含在授權標頭中。請參閱以下範例,了解如何取得字符。

  • 使用 IAM 的傳出授權 – AgentCore CLI 會為您設定下列許可,因此您不需要任何額外的設定:

    • 您的閘道服務角色具有叫用 Lambda 中所有函數的許可 (由 BedrockAgentCoreFullAccess 受管政策提供)。

    • 建立的 Lambda 函數已使用您的閘道服務角色設定為Principal可叫用它的 。

您可以在 Amazon Cognito 的協助下,透過收集下列資訊,取得 AgentCore CLI 為閘道建立的存取字符:

  • 權杖端點 – 在閘道的授權方組態中的探索 URL 中尋找 。如果您使用 API,您可以透過傳送 ListGateways 請求或閘道的 GetGateway 請求來尋找探索 URL。

  • 用戶端 ID – 在 Amazon Cognito 使用者集區資訊中尋找 。如果您使用 API,您可以針對與閘道相關聯的使用者集區傳送 ListUserPools 請求或 DescribeUserPool 請求。

  • 用戶端秘密 – 在 Amazon Cognito 使用者集區應用程式用戶端資訊中尋找 。如果您使用 API,您可以傳送 DescribeUserPoolClient 請求,指定與閘道相關聯的用戶端 ID 和使用者集區 ID。

首先,使用下列其中一種方法收集這些值。選取下列其中一種方法:

範例
Console
  1. 取得字符端點

    1. 在 https://# 開啟 AgentCore 主控台。 https://console.aws.amazon.com/bedrock-agentcore/home

    2. 從左側導覽窗格中,選擇閘道

    3. 閘道區段中,選取您的閘道。

    4. 傳入身分區段中,請注意下列值:

      • 允許用戶端 – 可存取閘道的用戶端 IDs。

      • 探索 URL – 格式應為 https://cognito-idp.${Region}.amazonaws.com/${UserPoolId}/.well-known/openid-configuration 。存放下列值:

        • ${UserPoolId} – 從探索 URL 擷取。

        • 權杖端點 – 選取探索 URL 連結,然後在 token_endpoint 欄位中尋找值。

  2. 取得用戶端 ID用戶端秘密

    1. https://console.aws.amazon.com/cognito 開啟 Amazon Cognito 主控台。

    2. 從左側導覽窗格中,選取使用者集區

    3. 選取與您的閘道相關聯的使用者集區 ID。

    4. 從左側導覽窗格中,選擇應用程式用戶端

    5. 選取用戶端 ID 符合字符端點允許用戶端的應用程式用戶端。存放 ID 值。

    6. 用戶端秘密 下,選取顯示用戶端秘密並存放該值。

AWS CLI
  1. 在終端機中執行 AgentCore get-gateway命令,並在--gateway-identifier引數中指定閘道 ID,如下列範例所示:

    aws bedrock-agentcore-control get-gateway --gateway-identifier my-gateway-id
  2. 從回應中,記下 authorizerConfiguration 欄位discoveryUrl的 :

    • ${UserPoolId} 從 URL 存放 。格式應為 https://cognito-idp.${Region}.amazonaws.com/${UserPoolId}/.well-known/openid-configuration

    • 存放allowedClients值。這些是可存取字符端點的用戶端 IDs。

    • 導覽至瀏覽器discoveryUrl中的 ,並存放 token_endpoint值。

  3. 在終端機中執行 Amazon Cognito describe-user-pool-client命令,並在--user-pool-id引數中指定使用者集區 ID,並在 --client-id 欄位中指定允許的用戶端 ID,如下列範例所示:

    aws cognito-idp describe-user-pool-client --user-pool-id my-user-pool-id --client-id my-client-id
  4. 存放 ClientSecret值。

AWS Python SDK (Boto3)
  1. 執行下列 Python 程式碼,將字符端點、用戶端 ID 和用戶端秘密儲存為變數:

    import requests import boto3 # Initialize AWS clients agentcore_client = boto3.client("bedrock-agentcore-control") cognito_client = boto3.client("cognito-idp") # Replace with your actual gateway ID gateway_id = "my-gateway-id" # Get discovery URL and first allowed client auth_config = agentcore_client.get_gateway(gatewayIdentifier=gateway_id)["authorizerConfiguration"]["customJWTAuthorizer"] discovery_url, client_id = auth_config["discoveryUrl"], auth_config["allowedClients"][0] # Get token endpoint from discovery URL discovery_url_json = requests.get(discovery_url).json() token_endpoint, user_pool_id = discovery_url_json["token_endpoint"], discovery_url_json["issuer"].split("/")[-1] # Get client secret client_secret = cognito_client.describe_user_pool_client( UserPoolId=user_pool_id, ClientId=client_id )["UserPoolClient"]["ClientSecret"]

其次,使用您收集的值從字符端點存取字符。選取下列其中一種方法:

範例
curl
  1. 在終端機中執行下列命令,取代字符端點、用戶端 ID 和用戶端秘密值。

    curl --http1.1 -X POST ${TokenEndpoint} \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "grant_type=client_credentials&client_id=${ClientId}&client_secret=${ClientSecret}"

    字符位於回應的 access_token 欄位中,而 token_type 欄位應指定 Bearer

Python requests package
  1. 執行下列 Python 程式碼以取得您的存取權杖。此程式碼假設您存放上一個步驟的 token_endpointclient_idclient_secret值:

    import requests import json def get_access_token(token_endpoint, client_id, client_secret): headers={ "Content-Type": "application/x-www-form-urlencoded" } payload={ "grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret } response = requests.post(token_endpoint, headers=headers, data=payload) return response.json() # Replace the argument values as necessary, if you didn't previously store them as these variables access_token_response = get_access_token( token_endpoint=token_endpoint, client_id=client_id, client_secret=client_secret ) access_token = access_token_response["access_token"]