Deleting products using the Amazon CLI
Amazon Service Catalog allows you to use the Amazon Command Line Interface (Amazon CLI) to delete products from your portfolio. The Amazon CLI is an open source tool that enables you to interact with Amazon services using commands in your command-line shell. The Amazon Service Catalog force-delete function requires an Amazon CLI alias, which is a shortcut you can create in the Amazon CLI to shorten commands or scripts that you frequently use.
Prerequisites
-
Install and configure the Amazon CLI. For more information, see Installing or updating the latest version of the Amazon CLI
and Configuration basics . Use a minimum Amazon CLI version of 1.11.24 or 2.0.0. -
The delete product CLI alias requires a bash-compatible terminal and the JQ command-line JSON processor. For more information about installing the Command-line JSON processor, see Download jq
. -
Create a Amazon CLI Alias to batch
DisassociationAPI calls, enabling you to delete a product in a single command.
To successfully delete a product, you must disassociate all resources associated with the product first.
Examples of product resource associations include portfolio associations, budgets, Tag Options, and
Service Actions. When using the CLI to delete a product, the CLI force-delete-product alias enables
you to call the Disassociate
API to disassociate any resources that would prevent the DeleteProduct API. This avoids a seperate call for
individual disassociations.
Note
The file paths shown in the procedures below may vary depending on which operating system you use to perform these actions.
Creating an Amazon CLI alias to delete Amazon Service Catalog products
When using the Amazon CLI to delete a Amazon Service Catalog product, the CLI force-delete-product alias
enables you to call the
Disassociate
API to disassociate any resources that would prevent the DeleteProduct call.
Create an alias file in your Amazon CLI configuration folder
-
In the Amazon CLI console, navigate to the configuraiton folder. By default, the configuration folder path is
~/.aws/on Linux and macOS, or%USERPROFILE%\.aws\on Windows. -
Create a sub-folder named
cliusing file navigation or by entering the following command in your preferred terminal:$ mkdir -p ~/.aws/cliThe resulting
clifolder default path is~/.aws/cli/on Linux and MacOS, or%USERPROFILE%\.aws\clion Windows. -
In the new
clifolder, create a text file namedaliaswith no file extension. You can create thealiasfile using file navigation or by entering the following command in your preferred terminal:$ touch ~/.aws/cli/alias -
Enter
[toplevel]on the first line. -
Save the file.
Next, you can add the force-delete-product alias to your alias file by
manually pasting the alias script into the file,
or by using a command in the terminal window.
Manually add the force-delete-product alias to your alias file
-
In the Amazon CLI console, navigate to your Amazon CLI configuration folder and open the
aliasfile. -
Enter the following code alias into the file, below the
[toplevel]line:[command servicecatalog] force-delete-product = !f() { if [ "$#" -ne 1 ]; then echo "Illegal number of parameters" exit 1 fi if [[ "$1" != prod-* ]]; then echo "Please provide a valid product id." exit 1 fi productId=$1 describeProductAsAdminResponse=$(aws servicecatalog describe-product-as-admin --id $productId) listPortfoliosForProductResponse=$(aws servicecatalog list-portfolios-for-product --product-id $productId) tagOptions=$(echo "$describeProductAsAdminResponse" | jq -r '.TagOptions[].Id') budgetName=$(echo "$describeProductAsAdminResponse" | jq -r '.Budgets[].BudgetName') portfolios=$(echo "$listPortfoliosForProductResponse" | jq -r '.PortfolioDetails[].Id') provisioningArtifacts=$(echo "$describeProductAsAdminResponse" | jq -r '.ProvisioningArtifactSummaries[].Id') provisioningArtifactServiceActionAssociations=() for provisioningArtifactId in $provisioningArtifacts; do listServiceActionsForProvisioningArtifactResponse=$(aws servicecatalog list-service-actions-for-provisioning-artifact --product-id $productId --provisioning-artifact-id $provisioningArtifactId) serviceActions=$(echo "$listServiceActionsForProvisioningArtifactResponse" | jq -r '[.ServiceActionSummaries[].Id] | join(",")') if [[ -n "$serviceActions" ]]; then provisioningArtifactServiceActionAssociations+=("${provisioningArtifactId}:${serviceActions}") fi done echo "Before deleting a product, the following associated resources must be disassociated. These resources will not be deleted. This action may take some time, depending on the number of resources being disassociated." echo "Portfolios:" for portfolioId in $portfolios; do echo "\t${portfolioId}" done echo "Budgets:" if [[ -n "$budgetName" ]]; then echo "\t${budgetName}" fi echo "Tag Options:" for tagOptionId in $tagOptions; do echo "\t${tagOptionId}" done echo "Service Actions on Provisioning Artifact:" for association in "${provisioningArtifactServiceActionAssociations[@]}"; do echo "\t${association}" done read -p "Are you sure you want to delete ${productId}? y,n " if [[ ! $REPLY =~ ^[Yy]$ ]]; then exit fi for portfolioId in $portfolios; do echo "Disassociating ${portfolioId}" aws servicecatalog disassociate-product-from-portfolio --product-id $productId --portfolio-id $portfolioId done if [[ -n "$budgetName" ]]; then echo "Disassociating ${budgetName}" aws servicecatalog disassociate-budget-from-resource --budget-name "$budgetName" --resource-id $productId fi for tagOptionId in $tagOptions; do echo "Disassociating ${tagOptionId}" aws servicecatalog disassociate-tag-option-from-resource --tag-option-id $tagOptionId --resource-id $productId done for association in "${provisioningArtifactServiceActionAssociations[@]}"; do associationPair=(${association//:/ }) provisioningArtifactId=${associationPair[0]} serviceActionsList=${associationPair[1]} serviceActionIds=${serviceActionsList//,/ } for serviceActionId in $serviceActionIds; do echo "Disassociating ${serviceActionId} from ${provisioningArtifactId}" aws servicecatalog disassociate-service-action-from-provisioning-artifact --product-id $productId --provisioning-artifact-id $provisioningArtifactId --service-action-id $serviceActionId done done echo "Deleting product ${productId}" aws servicecatalog delete-product --id $productId }; f -
Save the file.
Use the terminal window to add the force-delete-product alias to your alias file
-
Open your terminal window and run the following command
$ cat >> ~/.aws/cli/alias -
Paste the alias script to the terminal window, and then press CTRL+D to exit the
catcommand.
Call the force-delete-product alias
-
In your terminal window, run the following command to call the delete product alias
$ aws servicecatalog force-delete-product {product-id}The example below shows the
force-delete-productalias command and its resulting response$ aws servicecatalog force-delete-product prod-123Before deleting a product, the following associated resources must be disassociated. These resources will not be deleted. This action may take some time, depending on the number of resources being disassociated. Portfolios: port-123 Budgets: budgetName Tag Options: tag-123 Service Actions on Provisioning Artifact: pa-123:act-123 Are you sure you want to delete prod-123? y,n -
Enter
yto confirm you want to delete the product.
After successfully deleting the product, the terminal window displays the following results
Disassociating port-123
Disassociating budgetName
Disassociating tag-123
Disassociating act-123 from pa-123
Deleting product prod-123
Additional resources
For more information about Amazon CLI, using aliases, and deleting Amazon Service Catalog products, review the following resources:
-
Creating and using Amazon CLI aliases in the Amazon Command Line Interface (CLI) user guide.
-
Amazon CLI alias repository
git repository. -
Amazon re:Invent 2016: The Effective Amazon CLI User
on YouTube.