@@ -56,15 +56,39 @@ def handler(event, context):
5656def create_or_update_gateway (props , gateway_name ):
5757 """Create or update AgentCore Gateway using existing Cognito resources"""
5858 region = props ['Region' ]
59+
60+ # Initialize gateway client
61+ client = GatewayClient (region_name = region )
62+
63+ # Check if gateway already exists
64+ try :
65+ control_client = boto3 .client ("bedrock-agentcore-control" , region_name = region )
66+ resp = control_client .list_gateways (maxResults = 10 )
67+ existing_gateways = [g for g in resp .get ("items" , []) if g .get ("name" ) == gateway_name ]
68+
69+ if existing_gateways :
70+ existing_gateway = existing_gateways [0 ]
71+ logger .info (f"Gateway { gateway_name } already exists, returning existing configuration" )
72+ return {
73+ 'gateway_url' : existing_gateway .get ('gatewayUrl' ),
74+ 'gateway_id' : existing_gateway .get ('gatewayId' ),
75+ 'gateway_arn' : existing_gateway .get ('gatewayArn' )
76+ }
77+ except Exception as e :
78+ logger .info (f"Error checking for existing gateway: { e } " )
79+
80+ # Gateway doesn't exist, create it
81+ logger .info (f"Gateway { gateway_name } does not exist, creating new one" )
82+ return create_gateway (props , gateway_name , client )
83+
84+
85+ def create_gateway (props , gateway_name , client ):
86+ """Create new AgentCore Gateway"""
87+ region = props ['Region' ]
5988 lambda_arn = props ['LambdaArn' ]
6089 user_pool_id = props ['UserPoolId' ]
6190 client_id = props ['ClientId' ]
6291
63- logger .info (f"Creating gateway: { gateway_name } " )
64-
65- # Initialize gateway client
66- client = GatewayClient (region_name = region )
67-
6892 # Create JWT authorizer config using existing Cognito resources
6993 authorizer_config = {
7094 "customJWTAuthorizer" : {
0 commit comments