Skip to main content

Module: ssm-patch-manager

This module provisions AWS SSM Patch manager maintenance window tasks, targets, patch baselines and patch groups and a s3 bucket for storing patch task logs.

Introduction

Acknowledgements

This module was heavily inspired by @jparnaudeau module https://github.com/jparnaudeau/terraform-aws-ssm-patch-management

Usage

For a complete example, see examples/complete.

For automated tests of the complete example using bats and Terratest (which tests and deploys the example on AWS), see test.

module "ssm_patch_manager" {
source = "cloudposse/ssm-patch-manager/aws"
version = "xxxx"
name = "test"
}

Examples

Here is an example of using this module:

Requirements

NameVersion
terraform>= 1.3
aws>= 4.0

Providers

NameVersion
aws>= 4.0

Modules

NameSourceVersion
install_window_labelcloudposse/label/null0.25.0
scan_window_labelcloudposse/label/null0.25.0
ssm_patch_log_s3_bucketcloudposse/s3-bucket/aws4.0.1
thiscloudposse/label/null0.25.0

Resources

NameType
aws_ssm_maintenance_window.install_windowresource
aws_ssm_maintenance_window.scan_windowresource
aws_ssm_maintenance_window_target.target_installresource
aws_ssm_maintenance_window_target.target_scanresource
aws_ssm_maintenance_window_task.task_install_patchesresource
aws_ssm_maintenance_window_task.task_scan_patchesresource
aws_ssm_patch_baseline.baselineresource
aws_ssm_patch_group.install_patchgroupresource
aws_ssm_patch_group.scan_patchgroupresource
aws_caller_identity.currentdata source
aws_iam_policy_document.bucket_policydata source
aws_partition.currentdata source

Inputs

NameDescriptionTypeDefaultRequired
additional_tag_mapAdditional key-value pairs to add to each map in tags_as_list_of_maps. Not added to tags or id.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration.
map(string){}no
approved_patchesA list of explicitly approved patches for the baselinelist(string)[]no
approved_patches_compliance_levelDefines the compliance level for approved patches. This means that if an approved patch is reported as missing, this is the severity of the compliance violation. Valid compliance levels include the following: CRITICAL, HIGH, MEDIUM, LOW, INFORMATIONAL, UNSPECIFIED. The default value is UNSPECIFIED.string"HIGH"no
attributesID element. Additional attributes (e.g. workers or cluster) to add to id,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the delimiter
and treated as a single ID element.
list(string)[]no
bucket_idThe bucket ID to use for the patch log. If no bucket ID is provided, the module will create a new one. This is of type list(string) to work around #41 / https://github.com/hashicorp/terraform/issues/28962.list(string)[]no
cloudwatch_log_group_nameThe name of the CloudWatch log group where you want to send command output. If you don't specify a group name, Systems Manager automatically creates a log group for you. The log group uses the following naming format: aws/ssm/SystemsManagerDocumentName.stringnullno
cloudwatch_log_output_enabledEnables Systems Manager to send command output to CloudWatch Logs.boolfalseno
contextSingle object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as null to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional_tag_map, which are merged.
any
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
no
delimiterDelimiter to be used between ID elements.
Defaults to - (hyphen). Set to "" to use no delimiter at all.
stringnullno
descriptor_formatsDescribe additional descriptors to be output in the descriptors output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
{<br/> format = string<br/> labels = list(string)<br/>}
(Type is any so the map values can later be enhanced to provide additional options.)
format is a Terraform format string to be passed to the format() function.
labels is a list of labels, in order, to pass to format() function.
Label values will be normalized before being passed to format() so they will be
identical to how they appear in id.
Default is {} (descriptors output will be empty).
any{}no
enabledSet to false to prevent the module from creating any resourcesboolnullno
environmentID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'stringnullno
id_length_limitLimit id to this many characters (minimum 6).
Set to 0 for unlimited length.
Set to null for keep the existing setting, which defaults to 0.
Does not affect id_full.
numbernullno
install_maintenance_window_cutoffThe number of hours before the end of the Maintenance Window that Systems Manager stops scheduling new tasks for executionnumber1no
install_maintenance_window_durationThe duration of the maintenence windows (hours)number3no
install_maintenance_window_scheduleThe schedule of the Maintenance Window in the form of a cron or rate expressionstring"cron(0 0 21 ? * WED *)"no
install_maintenance_windows_targetsThe targets to register with the maintenance window. In other words, the instances to run commands on when the maintenance window runs. You can specify targets using instance IDs, resource group names, or tags that have been applied to instances. For more information about these examples formats see (https://docs.aws.amazon.com/systems-manager/latest/userguide/mw-cli-tutorial-targets-examples.html)
list(object({
key : string
values : list(string)
}
)
)
[]no
install_patch_groupsThe targets to register with the maintenance window. In other words, the instances to run commands on when the maintenance window runs. You can specify targets using instance IDs, resource group names, or tags that have been applied to instances. For more information about these examples formats see (https://docs.aws.amazon.com/systems-manager/latest/userguide/mw-cli-tutorial-targets-examples.html)list(string)
[
"TOPATCH"
]
no
install_sns_notification_enabledEnable/disable the SNS notification for install patchesboolfalseno
label_key_caseControls the letter case of the tags keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the tags input.
Possible values: lower, title, upper.
Default value: title.
stringnullno
label_orderThe order in which the labels (ID elements) appear in the id.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present.
list(string)nullno
label_value_caseControls the letter case of ID elements (labels) as included in id,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the tags input.
Possible values: lower, title, upper and none (no transformation).
Set this to title and set delimiter to "" to yield Pascal Case IDs.
Default value: lower.
stringnullno
labels_as_tagsSet of labels (ID elements) to include as tags in the tags output.
Default is to include all labels.
Tags with empty values will not be included in the tags output.
Set to [] to suppress all generated tags.
Notes:
The value of the name tag, if included, will be the id, not the name.
Unlike other null-label inputs, the initial setting of labels_as_tags cannot be
changed in later chained modules. Attempts to change it will be silently ignored.
set(string)
[
"default"
]
no
max_concurrencyThe maximum number of targets this task can be run for in parallelnumber20no
max_errorsThe maximum number of errors allowed before this task stops being schedulednumber50no
nameID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a tag.
The "name" tag is set to the full id string. There is no tag with the value of the name input.
stringnullno
namespaceID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally uniquestringnullno
notification_arnAn Amazon Resource Name (ARN) for a Simple Notification Service (SNS) topic. Run Command pushes notifications about command status changes to this topic.string""no
notification_eventsThe different events for which you can receive notifications. Valid values: All, InProgress, Success, TimedOut, Cancelled, and Failedlist(string)
[
"All"
]
no
notification_typeWhen specified with Command, receive notification when the status of a command changes. When specified with Invocation, for commands sent to multiple instances, receive notification on a per-instance basis when the status of a command changes. Valid values: Command and Invocationstring"Command"no
operating_systemDefines the operating system the patch baseline applies to. Supported operating systems include WINDOWS, AMAZON_LINUX, AMAZON_LINUX_2, SUSE, UBUNTU, CENTOS, and REDHAT_ENTERPRISE_LINUX. The Default value is WINDOWS.string"AMAZON_LINUX_2"no
patch_baseline_approval_rulesA set of rules used to include patches in the baseline. Up to 10 approval rules can be specified.
Each approval_rule block requires the fields documented below (unless marked optional).
approve_after_days and approve_until_date conflict, do not set both in the same approval_rule.

See https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_patch_baseline#approval_rule-block for full details.
list(object({
approve_after_days : optional(number)
approve_until_date : optional(string)
compliance_level : string
enable_non_security : bool
patch_baseline_filters : list(object({
name : string
values : list(string)
}))
}))
[
{
"approve_after_days": 7,
"compliance_level": "HIGH",
"enable_non_security": true,
"patch_baseline_filters": [
{
"name": "PRODUCT",
"values": [
"AmazonLinux2",
"AmazonLinux2.0"
]
},
{
"name": "CLASSIFICATION",
"values": [
"Security",
"Bugfix",
"Recommended"
]
},
{
"name": "SEVERITY",
"values": [
"Critical",
"Important",
"Medium"
]
}
]
}
]
no
reboot_optionWhen you choose the RebootIfNeeded option, the instance is rebooted if Patch Manager installed new patches, or if it detected any patches with a status of INSTALLED_PENDING_REBOOT during the Install operation. Possible values : RebootIfNeeded, NoRebootstring"RebootIfNeeded"no
regex_replace_charsTerraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, "/[^a-zA-Z0-9-]/" is used to remove all characters other than hyphens, letters and digits.
stringnullno
rejected_patchesA list of rejected patcheslist(string)[]no
s3_bucket_prefix_install_logsThe Amazon S3 bucket subfolder for install logsstring"install"no
s3_bucket_prefix_scan_logsThe Amazon S3 bucket subfolder for scan logsstring"scanning"no
s3_log_output_enabledTo enable or disable s3 bucket output for the runCommand logsbooltrueno
scan_maintenance_window_cutoffThe number of hours before the end of the Maintenance Window that Systems Manager stops scheduling new tasks for executionnumber1no
scan_maintenance_window_durationThe duration of the maintenence windows (hours)number3no
scan_maintenance_window_scheduleThe schedule of the Maintenance Window in the form of a cron or rate expression.string"cron(0 0 18 ? * WED *)"no
scan_maintenance_windows_targetsThe map of tags for targetting which EC2 instances will be scaned
list(object({
key : string
values : list(string)
}
)
)
[]no
scan_patch_groupsThe targets to register with the maintenance window. In other words, the instances to run commands on when the maintenance window runs. You can specify targets using instance IDs, resource group names, or tags that have been applied to instances. For more information about these examples formats see (https://docs.aws.amazon.com/systems-manager/latest/userguide/mw-cli-tutorial-targets-examples.html)list(string)
[
"TOSCAN"
]
no
scan_sns_notification_enabledEnable/Disable the SNS notification for scansboolfalseno
service_role_arnThe role that should be assumed when executing the task. If a role is not provided, Systems Manager uses your account's service-linked role. If no service-linked role for Systems Manager exists in your account, it is created for youstringnullno
sns_notification_role_arnAn Amazon Resource Name (ARN) for a Simple Notification Service (SNS) topic. Run Command pushes notifications about command status changes to this topic.string""no
ssm_bucket_policyCustom bucket policy for the SSM log bucketstringnullno
ssm_bucket_versioning_enableTo enable or disable S3 bucket versioning for the log bucket.stringtrueno
stageID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'stringnullno
tagsAdditional tags (e.g. {'BusinessUnit': 'XYZ'}).
Neither the tag keys nor the tag values will be modified by this module.
map(string){}no
task_install_priorityThe priority of the task in the Maintenance Window, the lower the number the higher the priority. Tasks in a Maintenance Window are scheduled in priority order with tasks that have the same priority scheduled in parallel.number1no
task_scan_priorityThe priority of the task in the Maintenance Window, the lower the number the higher the priority. Tasks in a Maintenance Window are scheduled in priority order with tasks that have the same priority scheduled in parallel. Default 1number1no
tenantID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is forstringnullno

Outputs

NameDescription
install_maintenance_window_idSSM Patch Manager install maintenance window ID
install_maintenance_window_target_idSSM Patch Manager install maintenance window target ID
install_maintenance_window_task_idSSM Patch Manager install maintenance windows task ID
install_patch_group_idSSM Patch Manager install patch group ID
patch_baseline_arnSSM Patch Manager patch baseline ARN
scan_maintenance_window_target_idSSM Patch Manager scan maintenance window target ID
scan_maintenance_window_task_idSSM Patch Manager scan maintenance windows task ID
scan_patch_group_idSSM Patch Manager scan patch group ID
ssm_patch_log_s3_bucket_arnSSM Patch Manager s3 log bucket ARN
ssm_patch_log_s3_bucket_idSSM Patch Manager s3 log bucket ID