AWS CDK CfnMaintenanceWindowTask/taskInvocationParameters - TypeScript

AWS CDK CfnMaintenanceWindowTask/taskInvocationParameters - TypeScript

我正在使用 AWS CDK、库 @aws-cdk/aws-ssm 和 TypeScript 创建 CfnMaintenanceWindowTask。我正在使用 AWS docs for cfn 中的示例代码,例如“ 创建一个 运行 命令任务,该任务使用维护 window 目标 ID 来定位实例” .我尝试转换为 CDK 的 JSON cfn 代码示例。

"TaskInvocationParameters": {
                "MaintenanceWindowRunCommandParameters": {
                    "Parameters": {
                        "Operation": [
                            "Install"
                        ],
                        "RebootOption": [
                            "NoReboot"
                        ]
                    }
                }

在 CfnMaintenanceWindowTask 对象中,我想使用下面的 TypeScript 代码定义 属性 taskInvocationParameters问题是由于 属性 taskInvocationParameters 出现以下错误,我无法执行此操作:

"TS2322: 类型'{ maintenanceWindow运行CommandParameters: { 参数: { 操作: string[]; BaselineTags: { key: string; values: string[]; }[] ; RebootOption: string[]; }[]; }[]; }' 不可分配给类型 'IResolvable | TaskInvocationParametersProperty | undefined'."

我的实施可能有误,但我看不出问题出在哪里。

const maintenanceWindowRunCommandParametersWindowsParametersBaselineTags = {
            key: 'PatchBaseline',
            values: [
                'Windows-Server-CritImp-7app-CDK'
            ]
        };    

const maintenanceWindowRunCommandParametersWindowsParameters = {
            Operation: [
                'Install'
            ],
            BaselineTags:[
                maintenanceWindowRunCommandParametersWindowsParametersBaselineTags
            ],
            RebootOption: [
                'NoReboot'
            ]
        };

const maintenanceWindowRunCommandParametersWindows = {
            Parameters: [
                maintenanceWindowRunCommandParametersWindowsParameters
            ],
        };    

const taskInvocationParametersRunPatchBaselineAssociationWindows = {
                maintenanceWindowRunCommandParameters: [
                    maintenanceWindowRunCommandParametersWindows
                ]
            };
    
    const maintenanceWindowTaskWindowsServer = new ssm.CfnMaintenanceWindowTask(this, 'Maintenance-task-Windows-CDK',{
                name: 'Maintenance-task-Windows-CDK',
                priority: 0,
                maxConcurrency: '2',
                maxErrors: '2',
                taskType: 'RUN_COMMAND',
                taskArn: 'AWS-RunPatchBaselineAssociation',
                taskInvocationParameters: taskInvocationParametersRunPatchBaselineAssociationWindows,
                windowId: maintenanceWindowEveryDayScanOnly.ref,
                targets: [
                    maintenanceWindowTaskWindowsServersTargets
                ]
            });

更新

我已经实现了@Hcaertnit 的代码。部署后我收到以下错误。

Failed resources: 10:42:06 | CREATE_FAILED | AWS::SSM::MaintenanceWindowTask | Maintenance-task-Windows-CDK (MaintenancetaskWindowsCDK) Cannot deserialize instance of java.lang.String out of START_OBJECT token at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.amazonaws.services.ssm.model.RegisterTaskWithMaintenanceWindowRequest["TaskInvocationParameters"]->com.amazonaws.services.ssm.model.MaintenanceWindowTaskInvoc ationParameters["MaintenanceWindowRunCommandParameters"]->com.amazonaws.services.ssm.model.MaintenanceWindowRunCommandParameters["Parameters"]->java.util.LinkedHashMap["BaselineTags"]->java.util.ArrayList[0])

像这样将前 4 个变量合并为一个:

const taskInvocationParametersRunPatchBaselineAssociationWindows = {
    maintenanceWindowRunCommandParameters: {
        parameters: {
            Operation: ["Install"],
            BaselineTags: [
                {
                    key: "PatchBaseline",
                    values: ["Windows-Server-CritImp-7app-CDK"],
                }
            ],
            RebootOption: ["NoReboot"],
        },
    },
};
  1. maintenanceWindowRunCommandParameters 接受一个 object;你向它传递了一个数组,而在该数组中你传递了对象。
  2. 那个对象有一个parameters(实际上不是Parameters)属性,这是一个对象;您再次将数组传递给 属性,其中包含正确的对象。

我像上面那样将变量合并为一个,因为所有的长变量名变得非常难以管理。

错误:

Failed resources: 10:42:06 | CREATE_FAILED | AWS::SSM::MaintenanceWindowTask | Maintenance-task-Windows-CDK (MaintenancetaskWindowsCDK) Cannot deserialize instance of java.lang.String out of START_OBJECT token at [Source: UNKNOWN; line: -1, column: -1]

是由于 BaselineTags 参数实施不准确造成的。此参数是 AWS-RunPatchBaselineAssociation 运行 命令的一部分。此参数有以下定义。

"BaselineTags": {
      "type": "String",
      "description": "(Optional) The baseline tags to use during the patching operation.",
      "allowedPattern": "(^$)|^Key=(.){1,256},Values=(.){0,256}[^,]$",
      "default": ""
    },

知道了,正确的代码格式是这样的:

const maintenanceWindowTaskWindowsServer = new ssm.CfnMaintenanceWindowTask(this, 'Maintenance-task-Windows-CDK',{
            name: 'Maintenance-task-Windows-CDK',
            description: 'Maintenance windows task used for Windows Servers.',
            priority: 0,
            maxConcurrency: '2',
            maxErrors: '2',
            taskType: 'RUN_COMMAND',
            taskArn: 'AWS-RunPatchBaselineAssociation',
            taskInvocationParameters: {
                maintenanceWindowRunCommandParameters: {
                    parameters: {
                        Operation: ["Install"],
                        BaselineTags: ["Key=PatchBaseline,Values=Windows-Server-CritImp-7app-CDK"],
                        RebootOption: ["NoReboot"]
                    }
                }
            },
            windowId: maintenanceWindowEveryDayScanOnly.ref,
            targets: [
                {
                    key: 'WindowTargetIds',
                    values: [
                        maintenanceWindowTargetWindowsServer.ref
                    ]
                }
            ]
        });