Skip to content

Commit c7b6f63

Browse files
committed
feat: destroy deletes tagged OpenSearch domain and AMP workspace
If the domain and AMP workspace were created by the CLI (tagged with observability-stack), destroy now deletes them too. Reused resources without the tag are left untouched. Signed-off-by: Kyle Hounslow <kylhouns@amazon.com>
1 parent bc720be commit c7b6f63

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

aws/cli-installer/src/destroy.mjs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Deletes in reverse dependency order: EC2 → Application → DQS → OSIS → IAM → (preserves AOS/AMP).
44
*/
55
import { OSISClient, DeletePipelineCommand, GetPipelineCommand } from '@aws-sdk/client-osis';
6-
import { OpenSearchClient, ListApplicationsCommand, DeleteApplicationCommand, DeleteDirectQueryDataSourceCommand, GetApplicationCommand, DescribeDomainCommand } from '@aws-sdk/client-opensearch';
6+
import { OpenSearchClient, ListApplicationsCommand, DeleteApplicationCommand, DeleteDirectQueryDataSourceCommand, GetApplicationCommand, DescribeDomainCommand, DeleteDomainCommand } from '@aws-sdk/client-opensearch';
77
import { IAMClient, DeleteRolePolicyCommand, DeleteRoleCommand, ListRolePoliciesCommand, ListAttachedRolePoliciesCommand, DetachRolePolicyCommand } from '@aws-sdk/client-iam';
88
import { printStep, printSuccess, printWarning, printInfo, createSpinner } from './ui.mjs';
99
import { teardownDemoInstance } from './ec2-demo.mjs';
@@ -150,7 +150,36 @@ export async function destroy(cfg) {
150150
if (e.name !== 'ResourceNotFoundException') printWarning(`Secret cleanup: ${e.message}`);
151151
}
152152

153+
// 7. OpenSearch domain (if tagged with this stack)
154+
try {
155+
const { ResourceGroupsTaggingAPIClient, GetResourcesCommand } = await import('@aws-sdk/client-resource-groups-tagging-api');
156+
const tagging = new ResourceGroupsTaggingAPIClient({ region });
157+
const { ResourceTagMappingList } = await tagging.send(new GetResourcesCommand({
158+
TagFilters: [{ Key: 'observability-stack', Values: [pipelineName] }],
159+
ResourceTypeFilters: ['es:domain'],
160+
}));
161+
for (const r of ResourceTagMappingList || []) {
162+
const domainName = r.ResourceARN.split('/').pop();
163+
const spinner = createSpinner(`Deleting OpenSearch domain '${domainName}'...`);
164+
await os.send(new DeleteDomainCommand({ DomainName: domainName }));
165+
spinner.stop(`Domain '${domainName}' deleted`);
166+
}
167+
} catch (e) { printWarning(`Domain cleanup: ${e.message}`); }
168+
169+
// 8. AMP workspace (if tagged with this stack)
170+
try {
171+
const { AMPClient, ListWorkspacesCommand, DeleteWorkspaceCommand } = await import('@aws-sdk/client-amp');
172+
const amp = new AMPClient({ region });
173+
const { workspaces } = await amp.send(new ListWorkspacesCommand({ alias: pipelineName }));
174+
for (const w of workspaces || []) {
175+
if (w.alias === pipelineName) {
176+
const spinner = createSpinner(`Deleting AMP workspace '${w.alias}'...`);
177+
await amp.send(new DeleteWorkspaceCommand({ workspaceId: w.workspaceId }));
178+
spinner.stop(`AMP workspace '${w.alias}' deleted`);
179+
}
180+
}
181+
} catch (e) { printWarning(`AMP cleanup: ${e.message}`); }
182+
153183
console.error();
154184
printSuccess('Destroy complete');
155-
printInfo('Note: OpenSearch domain and AMP workspace were preserved (shared resources)');
156185
}

0 commit comments

Comments
 (0)