Finish the AI setup after each create or update
CloudFormation can't set an assistant's default AI agent or associate a security profile with an AI agent. After every create or update, run the console steps script. It does two things:
It makes the knowledge agent the assistant's Self service default.
It associates each AI agent and its published version with its security profile.
Until you run it, the assistant's existing default answers the flow's contacts, and the
AI agents' tools fail. Agent assist needs no default. The AiConsoleSteps
output repeats these steps.
In the stack's Region, paste the script at the prompt, keep set, and press Enter to accept each default. It is done when it
prints an Associated line for each AI agent. What it does with the default and the
security profiles:
The Self service default applies to every self-service flow on the assistant. When the current default is another AI agent that the assistant still has, the script offers
keep, which changes nothing.It saves the default it replaces in
, in the folder you run it from (your home folder in CloudShell). When the default it replaces is one of the stack's own AI agents, as when you runstack-name-ai-agent-defaults-before.txtsetagain, it saves nothing.removeputs the saved default back. Run it from the same folder.Use the script for the security profiles, not the Security Profiles list on the AI agent's page. An association with the AI agent alone, without its published version, doesn't allow its tools.
If it prints an error, see Self-service and the Connect assistant.
( set -euo pipefail ask() { local reply read -r -p "$1 [$2]: " reply printf '%s' "${reply:-$2}" } # CloudShell sets AWS_REGION to the console's Region; elsewhere the CLI's configured Region is the default region=$(ask "Region of the stack" "${AWS_REGION:-$(aws configure get region 2>/dev/null || echo us-east-1)}") stack=$(ask "Stack name" "connect-sf-real-time") action=$(ask "set (after a create or update) or remove (before a delete)" "set") [[ "$action" == set || "$action" == remove ]] || { echo "Type set or remove." >&2; exit 2; } cfn() { aws cloudformation describe-stacks --region "$region" --stack-name "$stack" --query "Stacks[0].$1" --output text; } output() { cfn "Outputs[?OutputKey=='$1'].OutputValue | [0]"; } status=$(output ConnectAssistantStatus 2> /dev/null) || { echo "Can't read stack $stack in $region." >&2; exit 2; } [[ "$status" == Created\ * || "$status" == Reused\ * ]] || { echo "Stack $stack has no ConnectAssistantStatus output." >&2; exit 2; } assistant_id=${status##*/} instance_arn=$(cfn "Parameters[?ParameterKey=='InstanceArn'].ParameterValue | [0]") instance_id=${instance_arn##*/} saved="$stack-ai-agent-defaults-before.txt" use_case=Connect.SelfService # The latest published version of the AI agent whose output prefix is $1 (Knowledge, Account or AgentAssist). latest() { local agent_arn agent_arn=$(output "$1AgentArn") aws qconnect list-ai-agent-versions --region "$region" --assistant-id "$assistant_id" --ai-agent-id "${agent_arn##*/}" \ --query "max(aiAgentVersionSummaries[].versionNumber)" --output text } # Associates (set) or disassociates (remove) the AI agent whose output prefix is $1, and its latest version, with the # security profile its output names. Does nothing if the stack has no such AI agent. profiles() { local agent_arn name profile profile_id version verb arn agent_arn=$(output "$1AgentArn") [[ "$agent_arn" != None ]] || return 0 name=$(output "$1AgentName") profile=$(output "$1AgentSecurityProfile") profile_id=$(aws connect list-security-profiles --region "$region" --instance-id "$instance_id" \ --query "SecurityProfileSummaryList[?Name=='$profile'].Id | [0]" --output text) version=$(latest "$1") if [[ "$profile_id" == None ]]; then # Nothing to associate or remove; on remove, carry on with the other AI agents and the default. echo "No security profile $profile in the instance." >&2 [[ "$action" == remove ]] && return 0 exit 1 fi if [[ "$version" == None && "$action" == set ]]; then echo "No published version of $name." >&2; exit 1; fi verb=associate-security-profiles [[ "$action" == set ]] || verb=disassociate-security-profiles local arns=("$agent_arn") v if [[ "$version" != None ]]; then if [[ "$action" == set ]]; then arns+=("$agent_arn:$version") else # On remove, every version from 1 to the latest, so no older version keeps the profile. for ((v = 1; v <= version; v++)); do arns+=("$agent_arn:$v"); done fi fi for arn in "${arns[@]}"; do if [[ "$action" == set ]]; then aws connect "$verb" --region "$region" --instance-id "$instance_id" --security-profiles "Id=$profile_id" \ --entity-type AI_AGENT --entity-arn "$arn" else # Removing an association that isn't there is not an error here. aws connect "$verb" --region "$region" --instance-id "$instance_id" --security-profiles "Id=$profile_id" \ --entity-type AI_AGENT --entity-arn "$arn" 2> /dev/null || true fi done if [[ "$action" == set ]]; then echo "Associated $profile with $name and its version $version."; else echo "Removed $profile from $name."; fi } if [[ "$action" == set ]]; then current=$(aws qconnect get-assistant --region "$region" --assistant-id "$assistant_id" \ --query "assistant.orchestratorConfigurationList[?orchestratorUseCase=='$use_case'].aiAgentId | [0]" --output text) # Show the default AI agent by name when the assistant still has it current_label=$current if [[ "${current%%:*}" != None ]]; then current_name=$(aws qconnect get-ai-agent --region "$region" --assistant-id "$assistant_id" --ai-agent-id "${current%%:*}" \ --query aiAgent.name --output text 2> /dev/null || true) [[ -z "$current_name" || "$current_name" == None ]] || current_label="$current_name ($current)" fi echo "The assistant's Self service default is now ${current_label/None/not set}. It applies to every self-service flow on the assistant." # Offer a change only when the default is not set or is already one of this stack's AI agents; # otherwise the default is an AI agent your own flows may use, so Enter keeps it. knowledge_id=$(output KnowledgeAgentArn); knowledge_id=${knowledge_id##*/} account_id=$(output AccountAgentArn); account_id=${account_id##*/} current_id=${current%%:*} current_version= [[ "$current" != *:* ]] || current_version=${current##*:} case "$current_id" in None) offer=knowledge ;; "$knowledge_id" | "$account_id") # One of this stack's AI agents: the default must name its latest version, which this update published. # Offer the same AI agent's latest version, so Enter keeps the admin's choice of agent. if [[ "$current_id" == "$knowledge_id" ]]; then mine=Knowledge offer=knowledge; else mine=Account offer=account; fi newest=$(latest "$mine") if [[ -z "$current_version" || "$newest" == None || "$current_version" != "$newest" ]]; then echo "That is $(output "${mine}AgentName") version ${current_version:-none}, but its latest version is ${newest/None/none}:" echo "self-service contacts don't get this update's AI agent until the default names the latest version." fi ;; *) offer=keep # Another AI agent: check that it and its version still exist; if not, offer the knowledge agent if ! aws qconnect get-ai-agent --region "$region" --assistant-id "$assistant_id" --ai-agent-id "$current_id" \ > /dev/null 2>&1; then echo "That AI agent no longer exists on the assistant." offer=knowledge elif [[ -n "$current_version" && $(aws qconnect list-ai-agent-versions --region "$region" --assistant-id "$assistant_id" \ --ai-agent-id "$current_id" --query "length(aiAgentVersionSummaries[?versionNumber==\`$current_version\`])" \ --output text 2> /dev/null || echo 0) == 0 ]]; then echo "That AI agent has no version $current_version on the assistant." offer=knowledge else echo "That AI agent is not one of this stack's. Setting knowledge or account moves every self-service flow" echo "on this assistant, your own included, to that AI agent. Type keep to leave it as it is." fi ;; esac if [[ $(output AccountAgentArn) != None ]]; then echo "The account agent reads the account in each contact's accountId. Make it the default only if every self-service" echo "flow on this assistant sets accountId from verified data, or to -, before its Connect assistant block." choice=$(ask "Self service default: knowledge, account, or keep (change nothing)" "$offer") else [[ "$offer" != account ]] || offer=keep choice=$(ask "Self service default: knowledge, or keep (change nothing)" "$offer") fi case $choice in knowledge) prefix=Knowledge ;; account) prefix=Account ;; keep) prefix= ;; *) echo "Type knowledge, account or keep." >&2; exit 2 ;; esac if [[ -n "$prefix" ]]; then agent_arn=$(output "${prefix}AgentArn") version=$(latest "$prefix") [[ "$version" != None ]] || { echo "No published version of $(output "${prefix}AgentName")." >&2; exit 1; } # Never save one of this stack's own AI agents as the default to put back: remove runs before the stack's delete, # which deletes them. A set run again, from another folder, would otherwise save the stack's agent there. if [[ "$current_id" != None && ( "$current_id" == "$knowledge_id" || "$current_id" == "$account_id" ) ]]; then if grep -q "^$use_case=" "$saved" 2>/dev/null; then replaced="It replaced this stack's own AI agent; remove puts back the default saved earlier in $saved." else replaced="It replaced this stack's own AI agent, so remove has nothing to put back." fi else grep -q "^$use_case=" "$saved" 2>/dev/null || echo "$use_case=$current" >> "$saved" replaced="The one it replaced is saved in $saved." fi aws qconnect update-assistant-ai-agent --region "$region" --assistant-id "$assistant_id" --ai-agent-type ORCHESTRATION \ --orchestrator-use-case "$use_case" --configuration "aiAgentId=${agent_arn##*/}:$version" > /dev/null echo "Set $(output "${prefix}AgentName") (version $version) as the Self service default. $replaced" fi for prefix in Knowledge Account AgentAssist; do profiles "$prefix"; done else for prefix in Knowledge Account AgentAssist; do profiles "$prefix"; done previous=$(grep "^$use_case=" "$saved" 2>/dev/null | head -n 1 | cut -d= -f2- || true) if [[ -n "$previous" && $(ask "Put back the Self service default saved in $saved ($previous)? yes or no" "yes") == yes ]]; then if [[ "$previous" == None ]]; then aws qconnect remove-assistant-ai-agent --region "$region" --assistant-id "$assistant_id" \ --ai-agent-type ORCHESTRATION --orchestrator-use-case "$use_case" else aws qconnect update-assistant-ai-agent --region "$region" --assistant-id "$assistant_id" --ai-agent-type ORCHESTRATION \ --orchestrator-use-case "$use_case" --configuration "aiAgentId=$previous" > /dev/null fi # Forget it, so a later set on a stack of the same name saves the default as it is then. rm -f "$saved" echo "Put back the Self service default." elif [[ -z "$previous" ]]; then echo "No saved Self service default: if the assistant's Self service default is one of this stack's AI agents, set another one in the console." fi fi )
Before you delete the stack, or change Account agent for verified
contacts, run the
script with remove. Connect Customer refuses to delete a security
profile that is still associated with an AI agent, so otherwise the delete can
fail with DELETE_FAILED on the profiles; then run it and delete
again.