|
| 1 | +import * as ssm from 'aws-cdk-lib/aws-ssm'; |
| 2 | +import * as iam from 'aws-cdk-lib/aws-iam'; |
| 3 | +import { Construct } from 'constructs'; |
| 4 | + |
| 5 | +export interface PatchManagerProps { |
| 6 | + /** |
| 7 | + * The EC2 instance ID to apply patches |
| 8 | + */ |
| 9 | + instanceId: string; |
| 10 | + |
| 11 | + /** |
| 12 | + * Custom maintenance window |
| 13 | + * |
| 14 | + * @default - New maintenance window |
| 15 | + * schedule: Sundays at 07:00 UTC, duration: 3 hours, cutoff: 1 hour |
| 16 | + */ |
| 17 | + maintenanceWindow?: ssm.CfnMaintenanceWindow; |
| 18 | +} |
| 19 | + |
| 20 | +export class PatchManager extends Construct { |
| 21 | + constructor(scope: Construct, id: string, props?: PatchManagerProps) { |
| 22 | + super(scope, id); |
| 23 | + |
| 24 | + // IAM role used by the maintenance window |
| 25 | + const maintenanceRole = new iam.Role(this, 'MaintenanceWindowRole', { |
| 26 | + assumedBy: new iam.ServicePrincipal('ssm.amazonaws.com'), |
| 27 | + }); |
| 28 | + |
| 29 | + maintenanceRole.addToPolicy( |
| 30 | + new iam.PolicyStatement({ |
| 31 | + actions: [ |
| 32 | + 'ssm:SendCommand', |
| 33 | + 'ssm:ListCommands', |
| 34 | + 'ssm:ListCommandInvocations', |
| 35 | + ], |
| 36 | + resources: ['*'], |
| 37 | + }), |
| 38 | + ); |
| 39 | + |
| 40 | + // Maintenance Window |
| 41 | + const maintenanceWindow = props?.maintenanceWindow ?? new ssm.CfnMaintenanceWindow( |
| 42 | + this, |
| 43 | + 'PatchMaintenanceWindow', |
| 44 | + { |
| 45 | + name: 'patch-maintenance-window', |
| 46 | + description: 'Weekly patching using AWS default patch baseline', |
| 47 | + schedule: 'cron(0 7 ? * SUN *)', // Sundays 07:00 UTC |
| 48 | + duration: 3, |
| 49 | + cutoff: 1, |
| 50 | + allowUnassociatedTargets: false, |
| 51 | + }, |
| 52 | + ); |
| 53 | + |
| 54 | + // Target EC2 instance by Instance ID |
| 55 | + if (!props?.instanceId) { |
| 56 | + throw new Error('instanceId is required to create PatchManagerStack'); |
| 57 | + } |
| 58 | + const target = new ssm.CfnMaintenanceWindowTarget( |
| 59 | + this, |
| 60 | + 'PatchTarget', |
| 61 | + { |
| 62 | + windowId: maintenanceWindow.ref, |
| 63 | + resourceType: 'INSTANCE', |
| 64 | + targets: [ |
| 65 | + { |
| 66 | + key: 'InstanceIds', |
| 67 | + values: [props.instanceId], |
| 68 | + }, |
| 69 | + ], |
| 70 | + }, |
| 71 | + ); |
| 72 | + |
| 73 | + // Patch task (Install) |
| 74 | + new ssm.CfnMaintenanceWindowTask(this, 'PatchInstallTask', { |
| 75 | + windowId: maintenanceWindow.ref, |
| 76 | + taskArn: 'AWS-RunPatchBaseline', |
| 77 | + taskType: 'RUN_COMMAND', |
| 78 | + priority: 1, |
| 79 | + maxConcurrency: '1', |
| 80 | + maxErrors: '1', |
| 81 | + serviceRoleArn: maintenanceRole.roleArn, |
| 82 | + targets: [ |
| 83 | + { |
| 84 | + key: 'WindowTargetIds', |
| 85 | + values: [target.ref], |
| 86 | + }, |
| 87 | + ], |
| 88 | + taskInvocationParameters: { |
| 89 | + maintenanceWindowRunCommandParameters: { |
| 90 | + parameters: { |
| 91 | + Operation: ['Install'], |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }); |
| 96 | + } |
| 97 | +} |
0 commit comments