---
    AWSTemplateFormatVersion: '2010-09-09'
    Description: AWS CloudFormation template to create an EC2 report on multi-account using Amazon Amazon S3, EventBridge, 
      AWS Lambda, and AWS Systems Manager Automation. 
    Resources:        
      WindowsCWAgentSSMParameter:
        Type: AWS::SSM::Parameter
        Properties:
          Name: !Sub ['WindowsCWAgentConfig-${RandomGUID}', { RandomGUID: !Select [0, !Split ["-", !Select [2, !Split ["/", !Ref AWS::StackId ]]]] }]
          Type: String
          Value:
            |-
              {
                  "metrics": {
                      "append_dimensions": {
                          "InstanceId": "${aws:InstanceId}"
                      },
                      "metrics_collected": {
                          "Memory": {
                              "measurement": [
                                  "Available MBytes",
                                  "% Committed Bytes In Use"                    
                              ],
                              "metrics_collection_interval": 60
                          }
                      }
                  }
              }
          Description: SSM Parameter for running date command.
          Tags:
            Environment: !Ref AWS::StackId
      AWSSystemsManagerAutomationRole:
          Type: 'AWS::IAM::Role'
          Properties:
            RoleName: !Sub ['AWS-SystemsManager-AutomationRole-${RandomGUID}', { RandomGUID: !Select [0, !Split ["-", !Select [2, !Split ["/", !Ref AWS::StackId ]]]] }]
            Description: !Sub "Role create as part of cloudformation stack id ${AWS::StackId}."
            AssumeRolePolicyDocument:
              Version: 2012-10-17
              Statement:
                - Effect: Allow
                  Principal:
                    Service: ssm.amazonaws.com
                  Action:
                    - 'sts:AssumeRole'
            ManagedPolicyArns:
              - 'arn:aws:iam::aws:policy/service-role/AmazonSSMAutomationRole'
            Path: /
            Tags:
              - Key: CloudFormation StackId
                Value: !Ref AWS::StackId
      SystemsManagerAutomationDocument: 
        Type: AWS::SSM::Document
        Properties:
          DocumentFormat: YAML
          DocumentType: Automation
          Name:
            !Sub
              - 'WindowsCWAgent_runbook-${RandomGUID}'
              - { RandomGUID: !Select [0, !Split ["-", !Select [2, !Split ["/", !Ref AWS::StackId ]]]] }
          Content:
            description: SSM Automation for Back up all databases in SQL Server 2008 (or later) to SQL Server
            schemaVersion: '0.3'
            assumeRole: '{{AutomationAssumeRole}}'
            parameters:
              InstanceId:
                type: String
                description: "(Required) The instance running Windows Server 2008 R2 or above,
                  SQL Server 2008 or above, with the SSM agent installed."
              AutomationAssumeRole:
                type: String
                description: '(Required) The Amazon Resource Name (ARN) of the IAM role that allows Automation to perform the actions on your behalf. If no role is specified, Systems Manager Automation uses your IAM permissions to operate this runbook.'
                default: !GetAtt AWSSystemsManagerAutomationRole.Arn
            mainSteps:
            - name: InstallCWAgent
              action: aws:runCommand
              maxAttempts: 3
              onFailure: Continue
              inputs:
                DocumentName: AWS-ConfigureAWSPackage
                InstanceIds:
                  - "{{ InstanceId}}"
                Parameters:
                  action: Install
                  name: "AmazonCloudWatchAgent"
            - name: ConfigureCWAgent
              action: aws:runCommand
              maxAttempts: 3
              onFailure: Continue
              inputs:
                DocumentName: AmazonCloudWatch-ManageAgent
                InstanceIds:
                  - "{{ InstanceId }}"
                Parameters:
                  optionalConfigurationLocation: !Ref WindowsCWAgentSSMParameter
      WindowsCWAgentConfigAssociation:
        Type: AWS::SSM::Association
        Properties:
          AssociationName: !Sub ['WindowsCWAgentConfigAssociation-${RandomGUID}', { RandomGUID: !Select [0, !Split ["-", !Select [2, !Split ["/", !Ref AWS::StackId ]]]] }]
          Name: !Ref SystemsManagerAutomationDocument
          AutomationTargetParameterName: InstanceId
          Targets:
            - Key: InstanceIds 
              Values:
                - '*'
    Outputs:
      AWSSystemsManagerAutomationRole:
        Description: AWS Systems Manager Automation Execution Role
        Value: !Ref AWSSystemsManagerAutomationRole
      WindowsCWAgentSSMParameter:
        Description: AWS Systems Manager Automation Execution Role
        Value: !Ref WindowsCWAgentSSMParameter
      SystemsManagerAutomationDocument:
        Description: AWS Systems Manager Automation Execution Role
        Value: !Ref SystemsManagerAutomationDocument