description: SSM document to terminate a Sagemaker Notebook instance schemaVersion: '0.3' parameters: ProvisionedProductId: type: String description: 'The ID of the Sagemaker provisioned product being terminated.' allowedPattern: ^pp-[a-zA-Z0-9_\-]+$ TerminateToken: type: String description: '(Required) The unique token ID assigned to this request.' allowedPattern: ^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$ EnvId: type: String description: 'The ID of the environment resource stored in DDB' allowedPattern: ^env-[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$ mainSteps: - name: Terminate action: 'aws:executeAwsApi' maxAttempts: 3 timeoutSeconds: 30 inputs: Service: servicecatalog Api: TerminateProvisionedProduct ProvisionedProductId: '{{ ProvisionedProductId }}' TerminateToken: '{{ TerminateToken }}' outputs: - Name: RecordId Type: String Selector: '$.RecordDetail.RecordId' nextStep: WaitForTerminating description: | ## Terminate Terminate the provisioned Sagemaker Notebook product through service catalog ## Inputs * ProvisionedProductId: The Id of the provisioned product. * TerminateToken: An idempotency token that uniquely identifies the termination request. ## Outputs * RecordId: The Id of this request response. - name: WaitForTerminating action: 'aws:waitForAwsResourceProperty' timeoutSeconds: 600 inputs: Service: servicecatalog Api: DescribeRecord Id: '{{ Terminate.RecordId }}' DesiredValues: - SUCCEEDED - FAILED PropertySelector: '$.RecordDetail.Status' # aws.waitForAwsResourceProperty does not support outputs so creating another step nextStep: GetRecordDetail description: | ## WaitForTerminating Allow automation to wait for SUCCEEDED/FAILED status of the termination before continuing the automation ## Inputs * Id: the Id of the `Terminate` request response * DesiredValues: The expected status of the provisioned product on which to continue the automation: SUCCEEDED/FAILED ## Outputs * None (aws.waitForAwsResourceProperty does not support outputs. Output are put into the next step) - name: GetRecordDetail action: 'aws:executeAwsApi' inputs: Service: servicecatalog Api: DescribeRecord Id: '{{ Terminate.RecordId }}' outputs: - Name: ProductStatus Type: String Selector: '$.RecordDetail.Status' - Name: ErrorMessage Type: StringList Selector: '$.RecordDetail.RecordErrors' nextStep: ChooseByStatus description: | ## GetRecordDetail Get request response record details ## Inputs * Id: the Id of the `Terminate` request response ## Outputs * ProductStatus: The status of the provisioned product * ErrorMessage: Error messages to the request - name: ChooseByStatus action: 'aws:branch' inputs: Choices: - NextStep: PushMetadataToEventBridge Variable: '{{ GetRecordDetail.ProductStatus }}' StringEquals: SUCCEEDED - NextStep: PushFailureStatusToEventBridge Variable: '{{ GetRecordDetail.ProductStatus }}' StringEquals: FAILED Default: PushFailureStatusToEventBridge description: | ## ChooseByStatus Run conditional steps based on request status. If Termination SUCCEEDED, run`PushMetadataToEventBridge`; if FAILED, run `PushFailureStatusToEventBridge`. default: `PushFailureStatusToEventBridge` - name: PushMetadataToEventBridge action: 'aws:executeAwsApi' inputs: Service: events Api: PutEvents Entries: - Detail: '{ "EnvId": "{{ EnvId }}", "ProvisionedProductId": "{{ ProvisionedProductId }}", "RecordId": "{{ Terminate.RecordId }}", "EnvType": "sagemakerNotebook", "Operation": "Terminate", "Status": "TERMINATED" }' DetailType: 'Terminate' EventBusName: 'default' Source: 'automation' # This is being used for updating env in statusHandler lambda isEnd: true description: | ## PushMetadataToEventBridge Send metadata of this termination request to Amazon EventBridge - name: PushFailureStatusToEventBridge action: 'aws:executeAwsApi' inputs: Service: events Api: PutEvents Entries: - Detail: '{ "EnvId": "{{ EnvId }}", "ProvisionedProductId": "{{ ProvisionedProductId }}", "RecordId": "{{ Terminate.RecordId }}", "EnvType": "sagemakerNotebook", "Operation": "Terminate", "Status": "TERMINATING_FAILED", "ErrorMessage": {{ GetRecordDetail.ErrorMessage }} }' DetailType: 'Terminate' EventBusName: 'default' Source: 'automation' # This is being used for updating env in statusHandler lambda isEnd: true description: | ## PushFailureStatusToEventBridge Send error messages of this termination request to Amazon EventBridge