@@ -187,14 +187,31 @@ else
187187 --query " credentialProviders[?name=='mcp-backoffice-oauth'].credentialProviderArn | [0]" --output text)
188188fi
189189
190- # Check if backoffice target exists
190+ # Check if backoffice target exists and its status
191191EXISTING_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
195195if [ " ${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)
241258fi
242259
243- # Check if holidays target exists
260+ # Check if holidays target exists and its status
244261EXISTING_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
248265if [ " ${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 '
0 commit comments