Tutorial: Atualizar uma janela de manutenção (AWS CLI) - AWS Systems Manager

Tutorial: Atualizar uma janela de manutenção (AWS CLI)

Este tutorial demonstra como usar a AWS Command Line Interface (AWS CLI) para atualizar uma janela de manutenção. Ele também mostra como atualizar diferentes tipos de tarefas, incluindo aquelas do AWS Systems Manager Run Command e Automation, do AWS Lambda e do AWS Step Functions.

Os exemplos nesta seção usam as seguintes ações do Systems Manager para atualizar uma janela de manutenção.

Para obter informações sobre como usar o console do Systems Manager para atualizar uma janela de manutenção, consulte Atualizar ou excluir recursos da janela de manutenção (console).

Ao seguir as etapas neste tutorial, substitua os valores em texto itálico vermelho por suas próprias opções e IDs. Por exemplo, substitua o ID da janela de manutenção mw-0c50858d01EXAMPLE e o ID da instânciai-02573cafcfEXAMPLE pelos IDs de recursos criados.

Para atualizar uma janela de manutenção (AWS CLI)
  1. Abra o AWS CLI e execute o seguinte comando para atualizar um destino de forma a incluir um nome e uma descrição.

    Linux & macOS
    aws ssm update-maintenance-window-target \ --window-id "mw-0c50858d01EXAMPLE" \ --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \ --name "My-Maintenance-Window-Target" \ --description "Description for my maintenance window target"
    Windows
    aws ssm update-maintenance-window-target ^ --window-id "mw-0c50858d01EXAMPLE" ^ --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ^ --name "My-Maintenance-Window-Target" ^ --description "Description for my maintenance window target"

    O sistema retorna informações como estas.

    {
        "WindowId": "mw-0c50858d01EXAMPLE",
        "WindowTargetId": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE",
        "Targets": [
            {
                "Key": "InstanceIds",
                "Values": [
                    "i-02573cafcfEXAMPLE"
                ]
            }
        ],
        "Name": "My-Maintenance-Window-Target",
        "Description": "Description for my maintenance window target"
    }
  2. Execute o seguinte comando para usar a opção replace a fim de remover o campo de descrição e adicionar outro destino. O campo de descrição é removido, pois a atualização não inclui o campo (um valor nulo). Especifique um nó adicional que tenha sido configurado para uso com o Systems Manager:

    Linux & macOS
    aws ssm update-maintenance-window-target \ --window-id "mw-0c50858d01EXAMPLE" \ --window-target-id "d208dedf-3f6b-41ff-ace8-8e751EXAMPLE" \ --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE,i-0471e04240EXAMPLE" \ --name "My-Maintenance-Window-Target" \ --replace
    Windows
    aws ssm update-maintenance-window-target ^ --window-id "mw-0c50858d01EXAMPLE" ^ --window-target-id "d208dedf-3f6b-41ff-ace8-8e751EXAMPLE" ^ --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE,i-0471e04240EXAMPLE" ^ --name "My-Maintenance-Window-Target" ^ --replace

    O sistema retorna informações como estas.

    {
        "WindowId": "mw-0c50858d01EXAMPLE",
        "WindowTargetId": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE",
        "Targets": [
            {
                "Key": "InstanceIds",
                "Values": [
                    "i-02573cafcfEXAMPLE",
                    "i-0471e04240EXAMPLE"
                ]
            }
        ],
        "Name": "My-Maintenance-Window-Target"
    }
  3. A opção start-date permite atrasar a ativação de uma janela de manutenção até uma data futura determinada. A opção end-date permite que você defina uma data e hora no futuro após a qual a janela de manutenção não será mais executada. Especifique as opções no formato estendido ISO-8601.

    Execute o comando a seguir para especificar um intervalo de data e hora para execuções de janela de manutenção programadas regularmente:

    Linux & macOS
    aws ssm update-maintenance-window \ --window-id "mw-0c50858d01EXAMPLE" \ --start-date "2020-10-01T10:10:10Z" \ --end-date "2020-11-01T10:10:10Z"
    Windows
    aws ssm update-maintenance-window ^ --window-id "mw-0c50858d01EXAMPLE" ^ --start-date "2020-10-01T10:10:10Z" ^ --end-date "2020-11-01T10:10:10Z"
  4. Execute o comando a seguir para atualizar uma tarefa do Run Command.

    dica

    Se seu destino for uma instância do Amazon Elastic Compute Cloud (Amazon EC2) para o Windows Server, altere df para ipconfig, e AWS-RunShellScript para AWS-RunPowerShellScript no comando a seguir.

    Linux & macOS
    aws ssm update-maintenance-window-task \ --window-id "mw-0c50858d01EXAMPLE" \ --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" \ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \ --task-arn "AWS-RunShellScript" \ --service-role-arn "arn:aws:iam::account-id:role/MaintenanceWindowsRole" \ --task-invocation-parameters "RunCommand={Comment=Revising my Run Command task,Parameters={commands=df}}" \ --priority 1 --max-concurrency 10 --max-errors 4 \ --name "My-Task-Name" --description "A description for my Run Command task"
    Windows
    aws ssm update-maintenance-window-task ^ --window-id "mw-0c50858d01EXAMPLE" ^ --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" ^ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ^ --task-arn "AWS-RunShellScript" ^ --service-role-arn "arn:aws:iam::account-id:role/MaintenanceWindowsRole" ^ --task-invocation-parameters "RunCommand={Comment=Revising my Run Command task,Parameters={commands=df}}" ^ --priority 1 --max-concurrency 10 --max-errors 4 ^ --name "My-Task-Name" --description "A description for my Run Command task"

    O sistema retorna informações como estas.

    {
        "WindowId": "mw-0c50858d01EXAMPLE",
        "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
        "Targets": [
            {
                "Key": "WindowTargetIds",
                "Values": [
                    "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
                ]
            }
        ],
        "TaskArn": "AWS-RunShellScript",
        "ServiceRoleArn": "arn:aws:iam::111122223333:role/MaintenanceWindowsRole",
        "TaskParameters": {},
        "TaskInvocationParameters": {
            "RunCommand": {
                "Comment": "Revising my Run Command task",
                "Parameters": {
                    "commands": [
                        "df"
                    ]
                }
            }
        },
        "Priority": 1,
        "MaxConcurrency": "10",
        "MaxErrors": "4",
        "Name": "My-Task-Name",
        "Description": "A description for my Run Command task"
    }
  5. Adapte e execute o comando a seguir para atualizar uma tarefa do Lambda.

    Linux & macOS
    aws ssm update-maintenance-window-task \ --window-id mw-0c50858d01EXAMPLE \ --window-task-id 4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE \ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \ --task-arn "arn:aws:lambda:region:111122223333:function:SSMTestLambda" \ --service-role-arn "arn:aws:iam:account-id:role/MaintenanceWindowsRole" \ --task-invocation-parameters '{"Lambda":{"Payload":"{\"InstanceId\":\"{{RESOURCE_ID}}\",\"targetType\":\"{{TARGET_TYPE}}\"}"}}' \ --priority 1 --max-concurrency 10 --max-errors 5 \ --name "New-Lambda-Task-Name" \ --description "A description for my Lambda task"
    Windows
    aws ssm update-maintenance-window-task ^ --window-id mw-0c50858d01EXAMPLE ^ --window-task-id 4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE ^ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ^ --task-arn --task-arn "arn:aws:lambda:region:111122223333:function:SSMTestLambda" ^ --service-role-arn "arn:aws:iam:account-id:role/MaintenanceWindowsRole" ^ --task-invocation-parameters '{"Lambda":{"Payload":"{\"InstanceId\":\"{{RESOURCE_ID}}\",\"targetType\":\"{{TARGET_TYPE}}\"}"}}' ^ --priority 1 --max-concurrency 10 --max-errors 5 ^ --name "New-Lambda-Task-Name" ^ --description "A description for my Lambda task"

    O sistema retorna informações como estas.

    {
        "WindowId": "mw-0c50858d01EXAMPLE",
        "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
        "Targets": [
            {
                "Key": "WindowTargetIds",
                "Values": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
            }
        ],
        "TaskArn": "arn:aws:lambda:us-east-2:111122223333:function:SSMTestLambda",
        "ServiceRoleArn": "arn:aws:iam::111122223333:role/MaintenanceWindowsRole",
        "TaskParameters": {},
        "TaskInvocationParameters": {
            "Lambda": {
                "Payload": "e30="
            }
        },
        "Priority": 1,
        "MaxConcurrency": "10",
        "MaxErrors": "5",
        "Name": "New-Lambda-Task-Name",
        "Description": "A description for my Lambda task"
    }
  6. Se você estiver atualizando uma tarefa do Step Functions, adapte e execute o seguinte comando para atualizar os task-invocation-parameters:

    Linux & macOS
    aws ssm update-maintenance-window-task \ --window-id "mw-0c50858d01EXAMPLE" \ --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" \ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \ --task-arn "arn:aws:states:region:execution:SSMStepFunctionTest" \ --service-role-arn "arn:aws:iam:account-id:role/MaintenanceWindowsRole" \ --task-invocation-parameters '{"StepFunctions":{"Input":"{\"InstanceId\":\"{{RESOURCE_ID}}\"}"}}' \ --priority 0 --max-concurrency 10 --max-errors 5 \ --name "My-Step-Functions-Task" \ --description "A description for my Step Functions task"
    Windows
    aws ssm update-maintenance-window-task ^ --window-id "mw-0c50858d01EXAMPLE" ^ --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" ^ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ^ --task-arn "arn:aws:states:region:execution:SSMStepFunctionTest" ^ --service-role-arn "arn:aws:iam:account-id:role/MaintenanceWindowsRole" ^ --task-invocation-parameters '{"StepFunctions":{"Input":"{\"InstanceId\":\"{{RESOURCE_ID}}\"}"}}' ^ --priority 0 --max-concurrency 10 --max-errors 5 ^ --name "My-Step-Functions-Task" ^ --description "A description for my Step Functions task"

    O sistema retorna informações como estas.

    {
        "WindowId": "mw-0c50858d01EXAMPLE",
        "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
        "Targets": [
            {
                "Key": "WindowTargetIds",
                "Values": [
                    "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
                ]
            }
        ],
        "TaskArn": "arn:aws:states:us-east-2:111122223333:execution:SSMStepFunctionTest",
        "ServiceRoleArn": "arn:aws:iam::111122223333:role/MaintenanceWindowsRole",
        "TaskParameters": {},
        "TaskInvocationParameters": {
            "StepFunctions": {
                "Input": "{\"instanceId\":\"{{RESOURCE_ID}}\"}"
            }
        },
        "Priority": 0,
        "MaxConcurrency": "10",
        "MaxErrors": "5",
        "Name": "My-Step-Functions-Task",
        "Description": "A description for my Step Functions task"
    }
  7. Execute o comando a seguir para cancelar o registro de um destino de uma janela de manutenção. Este exemplo usa o parâmetro safe para determinar se o destino é referenciado por qualquer tarefa e, portanto, seguro para ter o registro cancelado.

    Linux & macOS
    aws ssm deregister-target-from-maintenance-window \ --window-id "mw-0c50858d01EXAMPLE" \ --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \ --safe
    Windows
    aws ssm deregister-target-from-maintenance-window ^ --window-id "mw-0c50858d01EXAMPLE" ^ --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ^ --safe

    O sistema retorna informações como estas.

    An error occurred (TargetInUseException) when calling the DeregisterTargetFromMaintenanceWindow operation: 
    This Target cannot be deregistered because it is still referenced in Task: 4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE
  8. Execute o comando a seguir para cancelar o registro de um destino de uma janela de manutenção, mesmo que o destino seja referenciado por uma tarefa. Você pode forçar a operação de cancelamento de registro usando o parâmetro no-safe.

    Linux & macOS
    aws ssm deregister-target-from-maintenance-window \ --window-id "mw-0c50858d01EXAMPLE" \ --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \ --no-safe
    Windows
    aws ssm deregister-target-from-maintenance-window ^ --window-id "mw-0c50858d01EXAMPLE" ^ --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ^ --no-safe

    O sistema retorna informações como estas.

    {
        "WindowId": "mw-0c50858d01EXAMPLE",
        "WindowTargetId": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
    }
    
  9. Execute o comando a seguir para atualizar uma tarefa do Run Command. Este exemplo usa um parâmetro Parameter Store do Systems Manager chamado UpdateLevel, que é formatado da seguinte maneira: '{{ssm:UpdateLevel}}'

    Linux & macOS
    aws ssm update-maintenance-window-task \ --window-id "mw-0c50858d01EXAMPLE" \ --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" \ --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE" \ --task-invocation-parameters "RunCommand={Comment=A comment for my task update,Parameters={UpdateLevel='{{ssm:UpdateLevel}}'}}"
    Windows
    aws ssm update-maintenance-window-task ^ --window-id "mw-0c50858d01EXAMPLE" ^ --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" ^ --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE" ^ --task-invocation-parameters "RunCommand={Comment=A comment for my task update,Parameters={UpdateLevel='{{ssm:UpdateLevel}}'}}"

    O sistema retorna informações como estas.

    {
        "WindowId": "mw-0c50858d01EXAMPLE",
        "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
        "Targets": [
            {
                "Key": "InstanceIds",
                "Values": [
                    "i-02573cafcfEXAMPLE"
                ]
            }
        ],
        "TaskArn": "AWS-RunShellScript",
        "ServiceRoleArn": "arn:aws:iam::111122223333:role/MyMaintenanceWindowServiceRole",
        "TaskParameters": {},
        "TaskInvocationParameters": {
            "RunCommand": {
                "Comment": "A comment for my task update",
                "Parameters": {
                    "UpdateLevel": [
                        "{{ssm:UpdateLevel}}"
                    ]
                }
            }
        },
        "Priority": 10,
        "MaxConcurrency": "1",
        "MaxErrors": "1"
    }
  10. Execute o seguinte comando para atualizar uma tarefa do Automation para especificar os parâmetros WINDOW_ID e WINDOW_TASK_ID para o parâmetro task-invocation-parameters:

    Linux & macOS
    aws ssm update-maintenance-window-task \ --window-id "mw-0c50858d01EXAMPLE" \ --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" \ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE \ --task-arn "AutoTestDoc" \ --service-role-arn "arn:aws:iam:account-id:role/MyMaintenanceWindowServiceRole \ --task-invocation-parameters "Automation={Parameters={InstanceId='{{RESOURCE_ID}}',initiator='{{WINDOW_ID}}.Task-{{WINDOW_TASK_ID}}'}}" \ --priority 3 --max-concurrency 10 --max-errors 5
    Windows
    aws ssm update-maintenance-window-task ^ --window-id "mw-0c50858d01EXAMPLE" ^ --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE" ^ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE ^ --task-arn "AutoTestDoc" ^ --service-role-arn "arn:aws:iam:account-id:role/MyMaintenanceWindowServiceRole ^ --task-invocation-parameters "Automation={Parameters={InstanceId='{{RESOURCE_ID}}',initiator='{{WINDOW_ID}}.Task-{{WINDOW_TASK_ID}}'}}" ^ --priority 3 --max-concurrency 10 --max-errors 5

    O sistema retorna informações como estas.

    {
        "WindowId": "mw-0c50858d01EXAMPLE",
        "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
        "Targets": [
            {
                "Key": "WindowTargetIds",
                "Values": [
                    "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
                ]
            }
        ],
        "TaskArn": "AutoTestDoc",
        "ServiceRoleArn": "arn:aws:iam::111122223333:role/MyMaintenanceWindowServiceRole",
        "TaskParameters": {},
        "TaskInvocationParameters": {
            "Automation": {
                "Parameters": {
                    "multi": [
                        "{{WINDOW_TASK_ID}}"
                    ],
                    "single": [
                        "{{WINDOW_ID}}"
                    ]
                }
            }
        },
        "Priority": 0,
        "MaxConcurrency": "10",
        "MaxErrors": "5",
        "Name": "My-Automation-Task",
        "Description": "A description for my Automation task"
    }