Skip to content

EC2 Image Builder fails on Windows Server 2022 Core with update-windows component: Defender update KB2267602 fails with ResultCode: '[4-Failed]'

0

I'm building a custom Windows Server 2022 Core AMI using EC2 Image Builder, and I'm using the AWS-managed component update-windows to apply Windows updates during the image build process.

The build fails during the patching step with the following error: Installing 'Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.433.130.0) - Current Channel (Broad)' EXCEPTION: Failed to install an update. Execution failed with ResultCode: '[4-Failed]'

The build does not run in a custom VPC—it's using the default infrastructure provided by EC2 Image Builder, with internet access enabled.

What is the recommended approach to prevent update-windows from failing in this case?

asked 9 months ago464 views
1 Answer
0

This is a common issue with Windows Server Core updates, particularly with Microsoft Defender updates. Here are the recommended approaches to resolve this:

  1. First Approach - Update Configuration:
# Create a component that runs before update-windows
$script = @'
# Stop Windows Update and Defender services
Stop-Service -Name wuauserv -Force
Stop-Service -Name WinDefend -Force

# Wait for services to stop completely
Start-Sleep -Seconds 10

# Start services again
Start-Service wuauserv
Start-Service WinDefend

# Wait for services to start
Start-Sleep -Seconds 30

# Clear Windows Update cache
Remove-Item "$env:SystemRoot\SoftwareDistribution\*" -Recurse -Force
'@
  1. Second Approach - Exclude Defender Updates: Create a custom component that configures Windows Update to exclude Defender updates:
# Configure Windows Update to exclude Defender updates
$script = @'
$updateSession = New-Object -ComObject Microsoft.Update.Session
$updateSearcher = $updateSession.CreateUpdateSearcher()
$searchResult = $updateSearcher.Search("IsInstalled=0")

foreach ($update in $searchResult.Updates) {
    if ($update.Title -like "*Defender*") {
        $update.AutoSelectOnWebSites = $false
    }
}
'@
  1. Third Approach - Use AWS-UpdateWindowsAmi Component: Instead of using update-windows, you could use the AWS-UpdateWindowsAmi component, which has better handling for Defender updates:
- name: UpdateWindows
  action: ExecutePowerShell
  inputs:
    commands:
      - |
        Install-WindowsFeature -Name UpdateServices-Services
        Start-Sleep -Seconds 30
        Install-WindowsUpdate -AcceptAll -IgnoreReboot

Key Implementation Notes:

  1. Component Ordering:

    • Ensure your custom component runs BEFORE the update-windows component
    • Set the appropriate dependencies in your recipe
  2. Error Handling: Add error handling to your custom component:

try {
    # Your update script here
    exit 0
} catch {
    Write-Error $_.Exception.Message
    exit 1
}
  1. Infrastructure Configuration:
infrastructureConfiguration:
  instanceType: t3.medium  # Recommend minimum t3.medium
  terminateInstanceOnFailure: true
  subnetId: subnet----# Optional: specify custom subnet
  1. Timeouts:
    • Consider increasing the timeout settings in your Image Builder configuration
    • Default timeout might not be sufficient for all updates

Best Practices:

  1. Monitor Build Logs:

    • Enable enhanced logging in EC2 Image Builder
    • Monitor CloudWatch Logs for detailed troubleshooting
  2. Testing:

    • Test the build process in a development environment first
    • Validate updates are applying correctly using:
Get-HotFix | Sort-Object InstalledOn -Descending
  1. Version Control:
    • Maintain version control for your components and recipes
    • Document any specific update exclusions or configurations

If these solutions don't resolve the issue, you might want to:

  • Review Windows Update logs at C:\Windows\Logs\WindowsUpdate
  • Check if there are any network connectivity issues
  • Verify there's sufficient disk space (at least 20GB free recommended)
  • Consider using a larger instance type during the build process
AWS
EXPERT
answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.