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:
examples/complete- complete example of using this module
Variables
Required Variables
Optional Variables
approved_patches(list(string)) optionalA list of explicitly approved patches for the baseline
Default value:
[ ]approved_patches_compliance_level(string) optionalDefines 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.
Default value:
"HIGH"bucket_id(list(string)) optionalThe 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.Default value:
[ ]cloudwatch_log_group_name(string) optionalThe 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.
Default value:
nullcloudwatch_log_output_enabled(bool) optionalEnables Systems Manager to send command output to CloudWatch Logs.
Default value:
falseinstall_maintenance_window_cutoff(number) optionalThe number of hours before the end of the Maintenance Window that Systems Manager stops scheduling new tasks for execution
Default value:
1install_maintenance_window_duration(number) optionalThe duration of the maintenence windows (hours)
Default value:
3install_maintenance_window_schedule(string) optionalThe schedule of the Maintenance Window in the form of a cron or rate expression
Default value:
"cron(0 0 21 ? * WED *)"install_maintenance_windows_targetsoptionalThe 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)
Type:
list(object({
key : string
values : list(string)
}
)
)Default value:
[ ]install_patch_groups(list(string)) optionalThe 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)
Default value:
[
"TOPATCH"
]install_sns_notification_enabled(bool) optionalEnable/disable the SNS notification for install patches
Default value:
falsemax_concurrency(number) optionalThe maximum number of targets this task can be run for in parallel
Default value:
20max_errors(number) optionalThe maximum number of errors allowed before this task stops being scheduled
Default value:
50notification_arn(string) optionalAn Amazon Resource Name (ARN) for a Simple Notification Service (SNS) topic. Run Command pushes notifications about command status changes to this topic.
Default value:
""notification_events(list(string)) optionalThe different events for which you can receive notifications. Valid values: All, InProgress, Success, TimedOut, Cancelled, and Failed
Default value:
[
"All"
]notification_type(string) optionalWhen 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 Invocation
Default value:
"Command"operating_system(string) optionalDefines 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.
Default value:
"AMAZON_LINUX_2"patch_baseline_approval_rulesoptionalA set of rules used to include patches in the baseline. Up to 10 approval rules can be specified.
Eachapproval_ruleblock requires the fields documented below (unless marked optional).
approve_after_daysandapprove_until_dateconflict, do not set both in the sameapproval_rule.See https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_patch_baseline#approval_rule-block for full details.
Type:
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)
}))
}))Default value:
[
{
"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"
]
}
]
}
]reboot_option(string) optionalWhen 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, NoReboot
Default value:
"RebootIfNeeded"rejected_patches(list(string)) optionalA list of rejected patches
Default value:
[ ]s3_bucket_prefix_install_logs(string) optionalThe Amazon S3 bucket subfolder for install logs
Default value:
"install"s3_bucket_prefix_scan_logs(string) optionalThe Amazon S3 bucket subfolder for scan logs
Default value:
"scanning"s3_log_output_enabled(bool) optionalTo enable or disable s3 bucket output for the runCommand logs
Default value:
truescan_maintenance_window_cutoff(number) optionalThe number of hours before the end of the Maintenance Window that Systems Manager stops scheduling new tasks for execution
Default value:
1scan_maintenance_window_duration(number) optionalThe duration of the maintenence windows (hours)
Default value:
3scan_maintenance_window_schedule(string) optionalThe schedule of the Maintenance Window in the form of a cron or rate expression.
Default value:
"cron(0 0 18 ? * WED *)"scan_maintenance_windows_targetsoptionalThe map of tags for targetting which EC2 instances will be scaned
Type:
list(object({
key : string
values : list(string)
}
)
)Default value:
[ ]scan_patch_groups(list(string)) optionalThe 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)
Default value:
[
"TOSCAN"
]scan_sns_notification_enabled(bool) optionalEnable/Disable the SNS notification for scans
Default value:
falseservice_role_arn(string) optionalThe 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 you
Default value:
nullsns_notification_role_arn(string) optionalAn Amazon Resource Name (ARN) for a Simple Notification Service (SNS) topic. Run Command pushes notifications about command status changes to this topic.
Default value:
""ssm_bucket_policy(string) optionalCustom bucket policy for the SSM log bucket
Default value:
nullssm_bucket_versioning_enable(string) optionalTo enable or disable S3 bucket versioning for the log bucket.
Default value:
truetask_install_priority(number) optionalThe 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 value:
1task_scan_priority(number) optionalThe 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 1
Default value:
1
Context Variables
The following variables are defined in the context.tf file of this module and part of the terraform-null-label pattern.
context.tf file of this module and part of the terraform-null-label pattern.additional_tag_map(map(string)) optionalAdditional key-value pairs to add to each map in
tags_as_list_of_maps. Not added totagsorid.
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.Required: No
Default value:
{ }attributes(list(string)) optionalID element. Additional attributes (e.g.
workersorcluster) to add toid,
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 thedelimiter
and treated as a single ID element.Required: No
Default value:
[ ]context(any) optionalSingle object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables asnullto use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional_tag_map, which are merged.Required: No
Default value:
{
"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
}delimiter(string) optionalDelimiter to be used between ID elements.
Defaults to-(hyphen). Set to""to use no delimiter at all.Required: No
Default value:
nulldescriptor_formats(any) optionalDescribe additional descriptors to be output in the
descriptorsoutput map.
Map of maps. Keys are names of descriptors. Values are maps of the form
\{<br/> format = string<br/> labels = list(string)<br/> \}
(Type isanyso the map values can later be enhanced to provide additional options.)
formatis a Terraform format string to be passed to theformat()function.
labelsis a list of labels, in order, to pass toformat()function.
Label values will be normalized before being passed toformat()so they will be
identical to how they appear inid.
Default is{}(descriptorsoutput will be empty).Required: No
Default value:
{ }enabled(bool) optionalSet to false to prevent the module from creating any resources
Required: NoDefault value:
nullenvironment(string) optionalID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'
Required: NoDefault value:
nullid_length_limit(number) optionalLimit
idto this many characters (minimum 6).
Set to0for unlimited length.
Set tonullfor keep the existing setting, which defaults to0.
Does not affectid_full.Required: No
Default value:
nulllabel_key_case(string) optionalControls the letter case of the
tagskeys (label names) for tags generated by this module.
Does not affect keys of tags passed in via thetagsinput.
Possible values:lower,title,upper.
Default value:title.Required: No
Default value:
nulllabel_order(list(string)) optionalThe 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.Required: No
Default value:
nulllabel_value_case(string) optionalControls 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 thetagsinput.
Possible values:lower,title,upperandnone(no transformation).
Set this totitleand setdelimiterto""to yield Pascal Case IDs.
Default value:lower.Required: No
Default value:
nulllabels_as_tags(set(string)) optionalSet of labels (ID elements) to include as tags in the
tagsoutput.
Default is to include all labels.
Tags with empty values will not be included in thetagsoutput.
Set to[]to suppress all generated tags.
Notes:
The value of thenametag, if included, will be theid, not thename.
Unlike othernull-labelinputs, the initial setting oflabels_as_tagscannot be
changed in later chained modules. Attempts to change it will be silently ignored.Required: No
Default value:
[
"default"
]name(string) optionalID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as atag.
The "name" tag is set to the fullidstring. There is no tag with the value of thenameinput.Required: No
Default value:
nullnamespace(string) optionalID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique
Required: NoDefault value:
nullregex_replace_chars(string) optionalTerraform 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.Required: No
Default value:
nullstage(string) optionalID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'
Required: NoDefault value:
nulltags(map(string)) optionalAdditional tags (e.g.
{'BusinessUnit': 'XYZ'}).
Neither the tag keys nor the tag values will be modified by this module.Required: No
Default value:
{ }tenant(string) optionalID element (Rarely used, not included by default). A customer identifier, indicating who this instance of a resource is for
Required: NoDefault value:
null
Outputs
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
Dependencies
Requirements
terraform, version:>= 1.3aws, version:>= 4.0
Providers
aws, version:>= 4.0
Modules
| Name | Version | Source | Description |
|---|---|---|---|
install_window_label | 0.25.0 | cloudposse/label/null | n/a |
scan_window_label | 0.25.0 | cloudposse/label/null | n/a |
ssm_patch_log_s3_bucket | 4.0.1 | cloudposse/s3-bucket/aws | n/a |
this | 0.25.0 | cloudposse/label/null | n/a |
Resources
The following resources are used by this module:
aws_ssm_maintenance_window.install_window(resource)aws_ssm_maintenance_window.scan_window(resource)aws_ssm_maintenance_window_target.target_install(resource)aws_ssm_maintenance_window_target.target_scan(resource)aws_ssm_maintenance_window_task.task_install_patches(resource)aws_ssm_maintenance_window_task.task_scan_patches(resource)aws_ssm_patch_baseline.baseline(resource)aws_ssm_patch_group.install_patchgroup(resource)aws_ssm_patch_group.scan_patchgroup(resource)
Data Sources
The following data sources are used by this module:
aws_caller_identity.current(data source)aws_iam_policy_document.bucket_policy(data source)aws_partition.current(data source)