description: Primary document for SQL License Tracking Solution schemaVersion: '0.3' assumeRole: '{{ AutomationAssumeRole }}' outputs: - deleteCustomInventory.result - invokeSecondarySQLServerLicenseTrackingSolutionAutomation.result parameters: InstanceId: description: (Required) Use * to target all instances in this account or specify an individual instance ID type: String default: '*' allowedPattern : '\*|^[m]{0,1}i-[a-z0-9]{8,17}' TagKey: description: (Optional) This parameter is required if you want to target all managed instances using tags type: String default: 'LicenseTrackingSolution-ManagedInstance' TagValue: description: (Optional) This parameter is required if you want to target all managed instances using tags type: String default: 'true' Region: type: String description: (Optional) Specify the AWS Region where you are deploying this document otherwise the region in which this document is executed will be used default: '{{global:REGION}}' AccountId: type: String description: (Optional) Specify the AWS Account ID where you are deploying this document otherwise the account in which this document is executed will be used default: '{{global:ACCOUNT_ID}}' SQLServerEELicenseConfiguration: type: String description: License Configuration for SQL Enterprise Edition or leave it as - default: '-' SQLServerEXPLicenseConfiguration: type: String description: License Configuration for SQL Express Edition or leave it as - default: '-' SQLServerSTDLicenseConfiguration: type: String description: License Configuration for SQL Standard Edition or leave it as - default: '-' SQLServerWEBLicenseConfiguration: type: String description: License Configuration for SQL Web Edition or leave it as - default: '-' SQLServerDEVLicenseConfiguration: type: String description: License Configuration for SQL Developer Edition or leave it as - default: '-' AutomationAssumeRole: type: String description: 'The IAM role for this execution. If no role is specified, then AWS Systems Manager Automation will be unable to complete.' mainSteps: - name: deleteCustomInventory description: 'This clean up step deletes all the metadata associated with the custom inventory type created for MSSQL [Custom:SQLServer] on all the EC2 instances.' action: 'aws:executeScript' inputs: Runtime: python3.7 Handler: script_handler Script: |- import sys import boto3 import json import botocore import time def script_handler(events, context): client = boto3.client('ssm') get_inventory_response = client.get_inventory_schema( TypeName='Custom:SQLServer' ) if get_inventory_response['Schemas']: delete_inventory_response = client.delete_inventory( TypeName='Custom:SQLServer', SchemaDeleteOption='DeleteSchema' ) deletion_id = delete_inventory_response['DeletionId'] wait_loop = True while wait_loop == True: describe_inventory_deletions_response = client.describe_inventory_deletions( DeletionId=deletion_id ) if describe_inventory_deletions_response['InventoryDeletions']: if describe_inventory_deletions_response['InventoryDeletions'][0]['LastStatus'] == "Complete": status = "Custom:SQLServer schema has been deleted successfuly. DeletionID: "+deletion_id wait_loop = False break else: time.sleep(60) else: status = "Custom:SQLServer type doesn't exist in the Inventory so continuing to the next step." return {'message': status} outputs: - Name: result Selector: $.Payload.message Type: String isCritical: true onFailure: Abort nextStep: invokeSecondarySQLServerLicenseTrackingSolutionAutomation - name: invokeSecondarySQLServerLicenseTrackingSolutionAutomation description: This step invokes the SecondarySQLServerLicenseTrackingSolutionDocument Automation action: 'aws:executeScript' inputs: Runtime: python3.7 Handler: script_handler Script: |- import sys import boto3 import json import botocore def script_handler(events, context): client = boto3.client('ssm') instanceId = events['instanceId'] accountId = events['accountId'] tagKey = events['tagKey'] tagValue = events['tagValue'] region = events['region'] sqlServerEELicenseConfig = events['sqlServerEELicenseConfig'] sqlServerSTDLicenseConfig = events['sqlServerSTDLicenseConfig'] sqlServerEXPLicenseConfig = events['sqlServerEXPLicenseConfig'] sqlServerWEBLicenseConfig = events['sqlServerWEBLicenseConfig'] sqlServerDEVLicenseConfig = events['sqlServerDEVLicenseConfig'] automationAssumeRole = events['automationAssumeRole'] if instanceId == '*': key = 'tag:'+tagKey values = tagValue else: key = 'ParameterValues' values = instanceId response = client.start_automation_execution( DocumentName='Secondary-SQLServerLicenseTrackingSolution-Document', Parameters={ 'Region': [ region ], 'AccountId': [ accountId ], 'InstanceId': [ instanceId ], 'TagKey': [ tagKey ], 'TagValue': [ tagValue ], 'SQLServerEELicenseConfiguration': [ sqlServerEELicenseConfig ], 'SQLServerSTDLicenseConfiguration': [ sqlServerSTDLicenseConfig ], 'SQLServerEXPLicenseConfiguration': [ sqlServerEXPLicenseConfig ], 'SQLServerWEBLicenseConfiguration': [ sqlServerWEBLicenseConfig ], 'SQLServerDEVLicenseConfiguration': [ sqlServerDEVLicenseConfig ], 'AutomationAssumeRole': [ automationAssumeRole ] }, TargetParameterName='InstanceId', Targets=[ { 'Key': key, 'Values': [ values ] }, ] ) if response["AutomationExecutionId"]: status = "Secondary-SQLServerLicenseTrackingSolution-Document has been successfuly invoked. Check AutomationExecutionId - " + response["AutomationExecutionId"] + " for more details" else: status = "Secondary-SQLServerLicenseTrackingSolution-Document was not invoked" raise Exception(f"It appears that this step couldn't be completed due to an unknown error. Please check the logs for more details") return {'message': status} InputPayload: instanceId: '{{ InstanceId }}' tagKey: '{{ TagKey }}' tagValue: '{{ TagValue }}' accountId: '{{ AccountId }}' region: '{{ Region }}' sqlServerEELicenseConfig: '{{ SQLServerEELicenseConfiguration }}' sqlServerSTDLicenseConfig: '{{ SQLServerSTDLicenseConfiguration }}' sqlServerEXPLicenseConfig: '{{ SQLServerEXPLicenseConfiguration }}' sqlServerWEBLicenseConfig: '{{ SQLServerWEBLicenseConfiguration }}' sqlServerDEVLicenseConfig: '{{ SQLServerDEVLicenseConfiguration }}' automationAssumeRole: '{{ AutomationAssumeRole }}' outputs: - Name: result Selector: $.Payload.message Type: String isCritical: true onFailure: Abort isEnd: true