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:

Variables

Required Variables

Optional Variables

approved_patches (list(string)) optional

A list of explicitly approved patches for the baseline


Default value: [ ]

approved_patches_compliance_level (string) optional

Defines 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)) optional

The 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) optional

The 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: null

cloudwatch_log_output_enabled (bool) optional

Enables Systems Manager to send command output to CloudWatch Logs.


Default value: false

install_maintenance_window_cutoff (number) optional

The number of hours before the end of the Maintenance Window that Systems Manager stops scheduling new tasks for execution


Default value: 1

install_maintenance_window_duration (number) optional

The duration of the maintenence windows (hours)


Default value: 3

install_maintenance_window_schedule (string) optional

The schedule of the Maintenance Window in the form of a cron or rate expression


Default value: "cron(0 0 21 ? * WED *)"

install_maintenance_windows_targets optional

The 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)) optional

The 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) optional

Enable/disable the SNS notification for install patches


Default value: false

max_concurrency (number) optional

The maximum number of targets this task can be run for in parallel


Default value: 20

max_errors (number) optional

The maximum number of errors allowed before this task stops being scheduled


Default value: 50

notification_arn (string) optional

An 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)) optional

The different events for which you can receive notifications. Valid values: All, InProgress, Success, TimedOut, Cancelled, and Failed


Default value:

[
"All"
]
notification_type (string) optional

When 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) optional

Defines 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_rules optional

A 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.



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) optional

When 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)) optional

A list of rejected patches


Default value: [ ]

s3_bucket_prefix_install_logs (string) optional

The Amazon S3 bucket subfolder for install logs


Default value: "install"

s3_bucket_prefix_scan_logs (string) optional

The Amazon S3 bucket subfolder for scan logs


Default value: "scanning"

s3_log_output_enabled (bool) optional

To enable or disable s3 bucket output for the runCommand logs


Default value: true

scan_maintenance_window_cutoff (number) optional

The number of hours before the end of the Maintenance Window that Systems Manager stops scheduling new tasks for execution


Default value: 1

scan_maintenance_window_duration (number) optional

The duration of the maintenence windows (hours)


Default value: 3

scan_maintenance_window_schedule (string) optional

The schedule of the Maintenance Window in the form of a cron or rate expression.


Default value: "cron(0 0 18 ? * WED *)"

scan_maintenance_windows_targets optional

The 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)) optional

The 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) optional

Enable/Disable the SNS notification for scans


Default value: false

service_role_arn (string) optional

The 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: null

sns_notification_role_arn (string) optional

An 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) optional

Custom bucket policy for the SSM log bucket


Default value: null

ssm_bucket_versioning_enable (string) optional

To enable or disable S3 bucket versioning for the log bucket.


Default value: true

task_install_priority (number) optional

The 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: 1

task_scan_priority (number) optional

The 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.

additional_tag_map (map(string)) optional

Additional 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.


Required: No

Default value: { }

attributes (list(string)) optional

ID 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.


Required: No

Default value: [ ]

context (any) optional

Single 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.


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) optional

Delimiter to be used between ID elements.
Defaults to - (hyphen). Set to "" to use no delimiter at all.


Required: No

Default value: null

descriptor_formats (any) optional

Describe 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).


Required: No

Default value: { }

enabled (bool) optional

Set to false to prevent the module from creating any resources
Required: No

Default value: null

environment (string) optional

ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'
Required: No

Default value: null

id_length_limit (number) optional

Limit 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.


Required: No

Default value: null

label_key_case (string) optional

Controls 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.


Required: No

Default value: null

label_order (list(string)) optional

The 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: null

label_value_case (string) optional

Controls 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.


Required: No

Default value: null

labels_as_tags (set(string)) optional

Set 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.


Required: No

Default value:

[
"default"
]
name (string) optional

ID 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.


Required: No

Default value: null

namespace (string) optional

ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique
Required: No

Default value: null

regex_replace_chars (string) optional

Terraform 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: null

stage (string) optional

ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'
Required: No

Default value: null

tags (map(string)) optional

Additional 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) optional

ID element (Rarely used, not included by default). A customer identifier, indicating who this instance of a resource is for
Required: No

Default value: null

Outputs

install_maintenance_window_id

SSM Patch Manager install maintenance window ID

install_maintenance_window_target_id

SSM Patch Manager install maintenance window target ID

install_maintenance_window_task_id

SSM Patch Manager install maintenance windows task ID

install_patch_group_id

SSM Patch Manager install patch group ID

patch_baseline_arn

SSM Patch Manager patch baseline ARN

scan_maintenance_window_target_id

SSM Patch Manager scan maintenance window target ID

scan_maintenance_window_task_id

SSM Patch Manager scan maintenance windows task ID

scan_patch_group_id

SSM Patch Manager scan patch group ID

ssm_patch_log_s3_bucket_arn

SSM Patch Manager s3 log bucket ARN

ssm_patch_log_s3_bucket_id

SSM Patch Manager s3 log bucket ID

Dependencies

Requirements

  • terraform, version: >= 1.3
  • aws, version: >= 4.0

Providers

  • aws, version: >= 4.0

Modules

NameVersionSourceDescription
install_window_label0.25.0cloudposse/label/nulln/a
scan_window_label0.25.0cloudposse/label/nulln/a
ssm_patch_log_s3_bucket4.0.1cloudposse/s3-bucket/awsn/a
this0.25.0cloudposse/label/nulln/a

Resources

The following resources are used by this module:

Data Sources

The following data sources are used by this module: