예: AgentCore CLI에서 생성한 기본 게이트웨이 및 대상에 대한 권한 부여
AgentCore CLI를 사용하여 게이트웨이와 Lambda 대상을 생성한 경우 CLI는 JWT 기반 인바운드 권한 부여 및 IAM 기반 아웃바운드 권한 부여를 자동으로 구성합니다.
를 사용하여 게이트웨이를 호출하면 CLIagentcore invoke가 인증을 자동으로 처리합니다. 아래 단계는 게이트웨이를 프로그래밍 방식으로 호출하려는 경우에만 필요합니다(예: AWS SDK 또는 사용curl).
프로그래밍 방식 액세스를 위해 다음을 사용하여 권한을 부여합니다.
다음 정보를 수집하여 Amazon Cognito의 도움을 받아 AgentCore CLI에서 게이트웨이에 대해 생성한 액세스 토큰을 얻을 수 있습니다.
-
토큰 엔드포인트 - 게이트웨이에 대한 권한 부여자 구성의 검색 URL에서를 찾습니다. API를 사용하는 경우 게이트웨이에 대한 ListGateways 요청 또는 GetGateway 요청을 전송하여 검색 URL을 찾을 수 있습니다.
-
클라이언트 ID - Amazon Cognito 사용자 풀 정보에서를 찾습니다. API를 사용하는 경우 게이트웨이와 연결된 사용자 풀에 대해 ListUserPools 요청 또는 DescribeUserPool 요청을 보낼 수 있습니다.
-
클라이언트 보안 암호 - Amazon Cognito 사용자 풀 앱 클라이언트 정보에서를 찾습니다. API를 사용하는 경우 게이트웨이와 연결된 클라이언트 ID 및 사용자 풀 ID를 지정하여 DescribeUserPoolClient 요청을 보낼 수 있습니다.
먼저 다음 방법 중 하나를 사용하여 이러한 값을 수집합니다. 다음 방법 중 하나를 선택합니다.
예
- Console
-
-
토큰 엔드포인트 가져오기:
-
클라이언트 ID 및 클라이언트 보안 암호 가져오기:
- AWS CLI
-
-
터미널에서 AgentCore get-gateway 명령을 실행하고 다음 예제와 같이 --gateway-identifier 인수에 게이트웨이 ID를 지정합니다.
aws bedrock-agentcore-control get-gateway --gateway-identifier my-gateway-id
-
응답에서 authorizerConfiguration 필드의 discoveryUrl를 기록해 둡니다.
-
URL${UserPoolId}에서를 저장합니다. 형식은 여야 합니다https://cognito-idp.${Region}.amazonaws.com/${UserPoolId}/.well-known/openid-configuration.
-
allowedClients 값을 저장합니다. 토큰 엔드포인트에 액세스할 수 있는 클라이언트 IDs입니다.
-
브라우저discoveryUrl에서 로 이동하여 token_endpoint 값을 저장합니다.
-
터미널에서 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
-
ClientSecret 값을 저장합니다.
- AWS Python SDK (Boto3)
-
-
다음 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
-
-
터미널에서 다음 명령을 실행하여 토큰 엔드포인트, 클라이언트 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
-
-
다음 Python 코드를 실행하여 액세스 토큰을 가져옵니다. 코드는 이전 단계의 token_endpoint , client_id 및 client_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"]