Patch Manager is a capability of AWS Systems Manager. It applies and automates the patching process of managed nodes for both security related and other types of updates, which makes it a useful tool for mutable infrastructure model. It’s capable of patching operating systems as well as applications. With the use of patch baseline
, Patch Manager includes rules for auto-approving patches and for creating a list of approved or rejected patches. Patches can be installed individually or to a large groups of managed instances using tags.
To schedule patching, Patch Manager runs tasks of Maintenance Window
, which is another capability of Systems Manager. Although using Patch Manager is not the only patching option, it is one of the most straightforward and practical approaches. This patching model tutorial is to provide a proof of concept for patching a single managed Amazon Linux 2 instance. The tutorial’s goal is to equip cloud teams the know-how they need to start using the Patch Manager. As a result, the tutorial’s expertise and knowledge are to be applied to a fleet of managed instances or on-premises servers.
Note: Patch Manager doesn’t support upgrading major versions of operating systems.
Patch Manager provides predefined baselines for each supported operating systems. The service uses the native package manager to drive the installation of patches approved by the patch baseline. For Amazon Linux 2, the predefined baseline is to approve all operating systems patches classified as Security
and have a severity level of Critical
or Important
. The patches are auto-approved 7 days after release. Moreover, all patches classified Bugfix
are also auto-approved 7 days after release. The predefined patch baseline are not customizable. However, it’s feasible to create a custom patch baseline to control patch classifications, approval/rejection and auto-approve days after release.
Below is the guidelines for the packages selected for update:
Security option | Equivalent Yum Command |
---|---|
Pre-defined default patch baselines provided by Amazon (non-security updates option is not selected) | sudo yum update-minimal --sec-severity=critical,important --bugfix -y |
User Custom patch baselines (non-security updates option is selected) | sudo yum update --security --bugfix -y |
Please, refer to AWS documentation for further information on how security patches are selected.
The plan is to utilize Patch Manager to develop a patching model for a single managed Amazon Linux 2 EC2 instance by using tags. Based on a scheduled maintenance window, an SSM Agent, installed on the EC2 instance, receives command issued to commence a patching process. The agent validates the instance’s Patch Group
tag-value and queries Patch Manager for an associated patch baseline. Once Patch Manager confirms the patch baseline for the Patch Group tag-value, it notifies the SSM Agent to retrieve the patch baseline snapshot. Finally, the SSM Agent begins scanning and installing patches based on the rules defined in the patch baseline snapshot provided by Patch Manager.
TheAWS-RunPatchBaselineWithHooks
SSM document will be used to orchestrate multi-step installing patches. It offers three optional hooks which allows running SSM documents at three points during the patching cycle (pre-install, post-patch and post-reboot). We will create three simple SSM documents, as a proof of concept, to be used during the patching cycle. To read more about AWS-RunPatchBaselineWithHooks SSM document , please refer to AWS blog.
Kernel Live Patching
is another feature available for Amazon Linux 2. It allows applying patches without the need for an immediate reboot or any disruption to running applications. We will not be using this feature for our patching model but I would like to recommend considering it. For more information about Kernel Live Patching, please refer to AWS documentation. Please, bear in mind in case there is a need to run patching outside of the maintenance window for a fleet of instances, it’s recommend by AWS, as best practice, to provide a Snapshot-ID. It ensures consistency among the targeted instances. Please, refer to AWS documentation for more information. In this patching model, we have allocated a Patch Group
tag per instance; therefore, no snapshot id is needed for patching outside the maintenance window.
Note: it’s highly recommended to test the patching model in a development environment prior to deploying to production. Also, for multi-account and multi-region patching, please refer to AW blog for more information.
This patching model should work as a proof of concept. A similar concept to this architecture can be applied to fleets of managed EC2 instances and on-premises servers.
Note: the lambda functions to Slack channel and to PagerDuty are not included in this tutorial.
Select View predefined patch baselines and click on Create patch baseline. The, fill out the requirements as following:
Under Patch baseline details:
Under Approval rule for operating systems:
Under Patch exceptions:
system-release.*
as shown below.Note: the purpose of this block is to reject patches to new Amazon Linux releases beyond the Patch Manager supported operating systems.
Under Patch sources: we will not add other source. Please, refer to AWS documentation on how to define alternative patch source repository
Under Manage tags: optionally define tags for the patch baseline
Navigate to AWS Systems Manager console. Under Change Management, select Maintenance Windows.
Click on Create maintenance window and fill out the requirements as following:
Under Provide maintenance window details:
Under Schedule:
Note: for testing, you can use Rate schedule to the run the patching job every hour or so. Also, refer to AWS documentation for more information about cron and rate expressions for Systems Manager.
Note: the Central Daylight Time (CDT) is -5 hours from GMT;therefore, the maintenance window is in affect starting from the 6th of July at 8:00pm CDT.
Under Manage tags: optionally define tags for the maintenance window
Finally, click on Create maintenance window to complete the process.
Key | Value |
---|---|
Patch Group | WebServer-Prd |
AWS-RunPatchBaselineWithHooks
and then select it. Leave Document version at the default selection Default Version at runtime and leave the Task priority at default value of 1.Note: AWS-RunPatchBaselineWithHooks is a wrapper document for AWS-RunPatchBaseline document. It divides the patching into two events, before reboot and after reboot for a total of three hooks to support custom functionality. Refer to About the AWS-RunPatchBaselineWithHooks SSM document for more information.
Note: if the IAM service role is not created yet, refer to Create IAM Service Role for Maintenance Window section.
Under Output options,
Note: to capture the complete terminal output logs, configure an S3 bucket because only the last 2500 characters of a command output are displayed in the console.
patching/webserver/prd
patching/webserver/prd
Note: we will have to give the required permissions to the EC instance IAM profile role to put objects to S3 and put logs to CloudWatch.
For the SNS notifications section
Under the Parameters section:
Note: for details on how to create documents, refer to How to Create SSM Document section. If you don’t want to use any of the three hooks, then leave it at default value, which is AWS-Noop.
Key | Value |
---|---|
Patch Group | WebServer-Prd |
We will create three simple documents as a proof of concept. Please, refer to AWS documentation for more details about creating SSM documents.
---
schemaVersion: '2.2'
description: Pre-patch Web Server Document
parameters: {}
mainSteps:
- action: aws:runShellScript
name: configureServer
inputs:
runCommand:
- sudo systemctl status httpd
Post-install-WebServer-Document
and Post-reboot-WebServer-Document
. The YAML contents are below.---
schemaVersion: '2.2'
description: Post-install Web Server Document
parameters: {}
mainSteps:
- action: aws:runShellScript
name: configureServer
inputs:
runCommand:
- sudo systemctl stop httpd
---
schemaVersion: '2.2'
description: Post-reboot Web Server Document
parameters: {}
mainSteps:
- action: aws:runShellScript
name: configureServer
inputs:
runCommand:
- sudo systemctl start httpd
- sudo systemctl status httpd
As Systems Manager needs permissions to run maintenance window tasks, below are the steps to create the required IAM role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssm:SendCommand",
"ssm:CancelCommand",
"ssm:ListCommands",
"ssm:ListCommandInvocations",
"ssm:GetCommandInvocation",
"ssm:ListTagsForResource",
"ssm:GetParameters"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"resource-groups:ListGroups",
"resource-groups:ListGroupResources"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"tag:GetResources"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"sns:Publish"
],
"Resource": [
"arn:aws:sns:*:*:*"
]
},
{
"Effect": "Allow",
"Action": "iam:PassRole",
"Resource": "*",
"Condition": {
"StringEquals": {
"iam:PassedToService": [
"ssm.amazonaws.com"
]
}
}
}
]
}
The EC2 instance profile requires necessary permissions to write data to an S3 bucket and to put log events to CloudWatch. We will create inline policies to assign the required permissions to the EC2 instance profile.
From the IAM console, navigate to Roles and search for your EC2 instance profile role. Then, click on the Role name.
Under *Permissions tab:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:PutObject",
"Resource": "*"
}
]
}
S3-Put-Instance-Profile
Repeat the process from bullet 2 to create an inline policy to allow the EC2 instance to create log stream and to put log events to CloudWatch. Use the below inline policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:CreateLogGroup"
],
"Resource": "*"
}
]
}
Patch Manager is an effective and powerful tool. It assists cloud teams in managing security risks and addressing vulnerabilities by automating the patching process. By the end of the tutorial, we have learned how to schedule patches for a single managed Amazon Linux 2 instance using Patch Manager and its features. Now that we have this knowledge, we can use it to create a patching model that is more intricate for a fleet of managed instances or on-premises servers.
I hope this tutorial helps you learn more about Patch Manager and how to use it for your patching models.
Omar A Omar