Skip to content

Commit 1602a7b

Browse files
author
Yuriy Bezsonov
committed
feat(java-spring-ai-agents): fix AWS API queries and add target status polling
1 parent 7d9d887 commit 1602a7b

4 files changed

Lines changed: 62 additions & 11 deletions

File tree

apps/java-spring-ai-agents/scripts/05-mcp-runtime.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ echo "5. Create the AgentCore Runtime"
169169
# Check if runtime already exists
170170
EXISTING_RUNTIME_ID=$(aws bedrock-agentcore-control list-agent-runtimes \
171171
--region ${AWS_REGION} --no-cli-pager \
172-
--query "agentRuntimeSummaries[?agentRuntimeName=='backoffice'].agentRuntimeId | [0]" --output text 2>/dev/null || echo "None")
172+
--query "agentRuntimes[?agentRuntimeName=='backoffice'].agentRuntimeId | [0]" --output text 2>/dev/null || echo "None")
173173

174174
if [ "${EXISTING_RUNTIME_ID}" != "None" ] && [ -n "${EXISTING_RUNTIME_ID}" ]; then
175175
echo "AgentCore Runtime already exists: ${EXISTING_RUNTIME_ID}"

apps/java-spring-ai-agents/scripts/06-mcp-gateway.sh

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,31 @@ else
187187
--query "credentialProviders[?name=='mcp-backoffice-oauth'].credentialProviderArn | [0]" --output text)
188188
fi
189189

190-
# Check if backoffice target exists
190+
# Check if backoffice target exists and its status
191191
EXISTING_BACKOFFICE_TARGET=$(aws bedrock-agentcore-control list-gateway-targets \
192192
--gateway-identifier "${GATEWAY_ID}" --region ${AWS_REGION} --no-cli-pager \
193193
--query "items[?name=='backoffice'].targetId | [0]" --output text 2>/dev/null || echo "None")
194194

195195
if [ "${EXISTING_BACKOFFICE_TARGET}" != "None" ] && [ -n "${EXISTING_BACKOFFICE_TARGET}" ]; then
196-
echo "Backoffice target already exists"
197-
else
196+
# Check if target is FAILED
197+
TARGET_STATUS=$(aws bedrock-agentcore-control get-gateway-target \
198+
--gateway-identifier "${GATEWAY_ID}" --target-id "${EXISTING_BACKOFFICE_TARGET}" \
199+
--region ${AWS_REGION} --no-cli-pager \
200+
--query 'status' --output text 2>/dev/null || echo "UNKNOWN")
201+
202+
if [ "${TARGET_STATUS}" = "FAILED" ]; then
203+
echo "Backoffice target is FAILED, deleting and recreating..."
204+
aws bedrock-agentcore-control delete-gateway-target \
205+
--gateway-identifier "${GATEWAY_ID}" --target-id "${EXISTING_BACKOFFICE_TARGET}" \
206+
--region ${AWS_REGION} --no-cli-pager >/dev/null 2>&1 || true
207+
sleep 10
208+
EXISTING_BACKOFFICE_TARGET="None"
209+
else
210+
echo "Backoffice target already exists (status: ${TARGET_STATUS})"
211+
fi
212+
fi
213+
214+
if [ "${EXISTING_BACKOFFICE_TARGET}" = "None" ] || [ -z "${EXISTING_BACKOFFICE_TARGET}" ]; then
198215
echo "Creating backoffice target"
199216

200217
RUNTIME_ARN="arn:aws:bedrock-agentcore:${AWS_REGION}:${ACCOUNT_ID}:runtime/${MCP_RUNTIME_ID}"
@@ -240,14 +257,31 @@ else
240257
--query "credentialProviders[?name=='mcp-holidays-apikey-provider'].credentialProviderArn | [0]" --output text)
241258
fi
242259

243-
# Check if holidays target exists
260+
# Check if holidays target exists and its status
244261
EXISTING_HOLIDAYS_TARGET=$(aws bedrock-agentcore-control list-gateway-targets \
245262
--gateway-identifier "${GATEWAY_ID}" --region ${AWS_REGION} --no-cli-pager \
246263
--query "items[?name=='holidays'].targetId | [0]" --output text 2>/dev/null || echo "None")
247264

248265
if [ "${EXISTING_HOLIDAYS_TARGET}" != "None" ] && [ -n "${EXISTING_HOLIDAYS_TARGET}" ]; then
249-
echo "Holidays target already exists"
250-
else
266+
# Check if target is FAILED
267+
TARGET_STATUS=$(aws bedrock-agentcore-control get-gateway-target \
268+
--gateway-identifier "${GATEWAY_ID}" --target-id "${EXISTING_HOLIDAYS_TARGET}" \
269+
--region ${AWS_REGION} --no-cli-pager \
270+
--query 'status' --output text 2>/dev/null || echo "UNKNOWN")
271+
272+
if [ "${TARGET_STATUS}" = "FAILED" ]; then
273+
echo "Holidays target is FAILED, deleting and recreating..."
274+
aws bedrock-agentcore-control delete-gateway-target \
275+
--gateway-identifier "${GATEWAY_ID}" --target-id "${EXISTING_HOLIDAYS_TARGET}" \
276+
--region ${AWS_REGION} --no-cli-pager >/dev/null 2>&1 || true
277+
sleep 10
278+
EXISTING_HOLIDAYS_TARGET="None"
279+
else
280+
echo "Holidays target already exists (status: ${TARGET_STATUS})"
281+
fi
282+
fi
283+
284+
if [ "${EXISTING_HOLIDAYS_TARGET}" = "None" ] || [ -z "${EXISTING_HOLIDAYS_TARGET}" ]; then
251285
echo "Creating holidays target"
252286

253287
OPENAPI_SPEC=$(curl -s "https://date.nager.at/openapi/v3.json" | jq -c '

apps/java-spring-ai-agents/scripts/08-aiagent-runtime.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ echo "5. Create the AgentCore Runtime"
177177
# Check if runtime already exists
178178
EXISTING_RUNTIME_ID=$(aws bedrock-agentcore-control list-agent-runtimes \
179179
--region ${AWS_REGION} --no-cli-pager \
180-
--query "agentRuntimeSummaries[?agentRuntimeName=='aiagent'].agentRuntimeId | [0]" --output text 2>/dev/null || echo "None")
180+
--query "agentRuntimes[?agentRuntimeName=='aiagent'].agentRuntimeId | [0]" --output text 2>/dev/null || echo "None")
181181

182182
if [ "${EXISTING_RUNTIME_ID}" != "None" ] && [ -n "${EXISTING_RUNTIME_ID}" ]; then
183183
echo "AgentCore Runtime already exists: ${EXISTING_RUNTIME_ID}"

apps/java-spring-ai-agents/scripts/11-mcp-currency.sh

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,31 @@ sleep 10
145145
echo ""
146146
echo "5. Create the Gateway target"
147147

148-
# Check if target already exists
148+
# Check if target already exists and its status
149149
EXISTING_TARGET=$(aws bedrock-agentcore-control list-gateway-targets \
150150
--gateway-identifier "${GATEWAY_ID}" --region ${AWS_REGION} --no-cli-pager \
151151
--query "items[?name=='currency'].targetId | [0]" --output text 2>/dev/null || echo "None")
152152

153153
if [ "${EXISTING_TARGET}" != "None" ] && [ -n "${EXISTING_TARGET}" ]; then
154-
echo "Currency target already exists"
155-
else
154+
# Check if target is FAILED
155+
TARGET_STATUS=$(aws bedrock-agentcore-control get-gateway-target \
156+
--gateway-identifier "${GATEWAY_ID}" --target-id "${EXISTING_TARGET}" \
157+
--region ${AWS_REGION} --no-cli-pager \
158+
--query 'status' --output text 2>/dev/null || echo "UNKNOWN")
159+
160+
if [ "${TARGET_STATUS}" = "FAILED" ]; then
161+
echo "Currency target is FAILED, deleting and recreating..."
162+
aws bedrock-agentcore-control delete-gateway-target \
163+
--gateway-identifier "${GATEWAY_ID}" --target-id "${EXISTING_TARGET}" \
164+
--region ${AWS_REGION} --no-cli-pager >/dev/null 2>&1 || true
165+
sleep 10
166+
EXISTING_TARGET="None"
167+
else
168+
echo "Currency target already exists (status: ${TARGET_STATUS})"
169+
fi
170+
fi
171+
172+
if [ "${EXISTING_TARGET}" = "None" ] || [ -z "${EXISTING_TARGET}" ]; then
156173
echo "Creating currency target"
157174

158175
LAMBDA_TOOLS='[

0 commit comments

Comments
 (0)