{"id":215,"date":"2019-01-21T06:49:00","date_gmt":"2019-01-21T06:49:00","guid":{"rendered":""},"modified":"2026-01-08T12:23:54","modified_gmt":"2026-01-08T06:53:54","slug":"log-parsing-of-windows-servers-on-instance-termination","status":"publish","type":"post","link":"https:\/\/opstree.com\/blog\/2019\/01\/21\/log-parsing-of-windows-servers-on-instance-termination\/","title":{"rendered":"Log Parsing of Windows Servers on Instance Termination"},"content":{"rendered":"<h2 data-start=\"711\" data-end=\"726\">Introduction<\/h2>\n<p data-start=\"728\" data-end=\"1067\">Logs play a critical role in any application or system. They provide deep visibility into what the application is doing, how requests are processed, and what caused an error. Depending on how logging is configured, logs may contain transaction history, timestamps, request details, and even financial information such as debits or credits.<\/p>\n<p data-start=\"1069\" data-end=\"1380\">In enterprise environments, applications usually run across multiple hosts. Managing logs across hundreds of servers can quickly become complex. Debugging issues by manually searching log files on multiple instances is time consuming and inefficient. This is why centralizing logs is considered a best practice.<\/p>\n<p data-start=\"1382\" data-end=\"1645\">Recently, I encountered a common challenge in AWS environments where application logs need to be retained from instances running behind an Auto Scaling Group. This blog explains a practical solution to ensure logs are preserved even when instances are terminated.<\/p>\n<h2 data-start=\"1652\" data-end=\"1671\">Problem Scenario<\/h2>\n<p data-start=\"1673\" data-end=\"1758\">Assume your application writes logs to the following directory on a Windows instance.<\/p>\n<div class=\"contain-inline-size rounded-2xl corner-superellipse\/1.1 relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre!\"><span class=\"hljs-built_in\">C<\/span><span class=\"hljs-operator\">:<\/span>\\<span class=\"hljs-variable\">Source<\/span>\\<span class=\"hljs-built_in\">Application<\/span>\\<span class=\"hljs-variable\">web<\/span>\\<span class=\"hljs-variable\">logs<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p data-start=\"1800\" data-end=\"1979\">Traffic to the application is variable. At low traffic, two EC2 instances may be sufficient. During peak traffic, the Auto Scaling Group may scale out to twenty or more instances.<\/p>\n<p data-start=\"1981\" data-end=\"2246\">When traffic increases, new EC2 instances are launched and logs are generated normally. However, when traffic drops, Auto Scaling triggers scale-down events and terminates instances. When an instance is terminated, all logs stored locally on that instance are lost.<\/p>\n<p data-start=\"2248\" data-end=\"2306\">This makes post-incident debugging and auditing difficult.<\/p>\n<h2 data-start=\"2313\" data-end=\"2333\">Solution Overview<\/h2>\n<p data-start=\"2335\" data-end=\"2428\">The goal is to synchronize logs from terminating EC2 instances before they are fully removed.<\/p>\n<p data-start=\"2430\" data-end=\"2666\">This solution uses AWS services to trigger a PowerShell script through AWS Systems Manager at instance termination time. The script archives logs and uploads them to an S3 bucket with identifying information such as IP address and date.<\/p>\n<p data-start=\"2668\" data-end=\"2716\">To achieve this, two prerequisites are required.<\/p>\n<ol data-start=\"2718\" data-end=\"2852\">\n<li data-start=\"2718\" data-end=\"2785\">\n<p data-start=\"2721\" data-end=\"2785\">Systems Manager must be able to communicate with EC2 instances<\/p>\n<\/li>\n<li data-start=\"2786\" data-end=\"2852\">\n<p data-start=\"2789\" data-end=\"2852\">EC2 instances must have permission to write logs to Amazon S3<\/p>\n<\/li>\n<\/ol>\n<h2 data-start=\"2859\" data-end=\"2878\">Environment Used<\/h2>\n<p data-start=\"2880\" data-end=\"2923\">For this setup, the following AMI was used.<\/p>\n<div class=\"contain-inline-size rounded-2xl corner-superellipse\/1.1 relative bg-token-sidebar-surface-primary\">\n<div class=\"sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl\/main:top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs\">\u00a0<\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre!\"><span class=\"hljs-string\">Microsoft<\/span> <span class=\"hljs-string\">Windows<\/span> <span class=\"hljs-string\">Server<\/span> <span class=\"hljs-number\">2012 <\/span><span class=\"hljs-string\">R2<\/span> <span class=\"hljs-string\">Base<\/span><br \/>\n<span class=\"hljs-attr\">AMI ID:<\/span> <span class=\"hljs-string\">ami-0f7af6e605e2d2db5<\/span><br \/>\n<\/code><\/div>\n<div dir=\"ltr\"><img decoding=\"async\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/01\/cfa3a-blank2bdiagram.jpeg?w=300\" \/><\/div>\n<\/div>\n<h2 data-start=\"3007\" data-end=\"3058\">Step 1 Configuring Systems Manager Access on EC2<\/h2>\n<p data-start=\"3060\" data-end=\"3190\">SSM Agent is installed by default on Windows Server 2016 and on Windows Server 2003 to 2012 R2 AMIs published after November 2016.<\/p>\n<p data-start=\"3192\" data-end=\"3280\">For older Windows AMIs, EC2Config must be upgraded and SSM Agent installed alongside it.<\/p>\n<p data-start=\"3282\" data-end=\"3445\">The following PowerShell script upgrades EC2Config, installs SSM Agent, and installs AWS CLI.<br data-start=\"3375\" data-end=\"3378\" \/>Use this script only for instructional and controlled environments.<\/p>\n<h3 data-start=\"3447\" data-end=\"3499\">PowerShell Script to Install Required Components<\/h3>\n<div class=\"contain-inline-size rounded-2xl corner-superellipse\/1.1 relative bg-token-sidebar-surface-primary\">\n<div class=\"sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl\/main:top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs\">\u00a0<\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre!\"><span class=\"hljs-comment\"># Create temporary directory if not present<\/span><br \/>\n<span class=\"hljs-keyword\">if<\/span> (!(<span class=\"hljs-built_in\">Test-Path<\/span> <span class=\"hljs-literal\">-Path<\/span> C:\\Tmp)) {<br \/>\n    <span class=\"hljs-built_in\">New-Item<\/span> <span class=\"hljs-literal\">-ItemType<\/span> Directory <span class=\"hljs-literal\">-Path<\/span> C:\\Tmp<br \/>\n}<\/p>\n<p><span class=\"hljs-built_in\">Set-Location<\/span> C:\\Tmp<\/p>\n<p><span class=\"hljs-comment\"># Download installers<\/span><br \/>\n<span class=\"hljs-built_in\">Invoke-WebRequest<\/span> <span class=\"hljs-string\">\"https:\/\/s3.ap-south-1.amazonaws.com\/asg-termination-logs\/Ec2Install.exe\"<\/span> <span class=\"hljs-literal\">-OutFile<\/span> Ec2Config.exe<br \/>\n<span class=\"hljs-built_in\">Invoke-WebRequest<\/span> <span class=\"hljs-string\">\"https:\/\/s3.amazonaws.com\/ec2-downloads-windows\/SSMAgent\/latest\/windows_amd64\/AmazonSSMAgentSetup.exe\"<\/span> <span class=\"hljs-literal\">-OutFile<\/span> ssmagent.exe<br \/>\n<span class=\"hljs-built_in\">Invoke-WebRequest<\/span> <span class=\"hljs-string\">\"https:\/\/s3.amazonaws.com\/aws-cli\/AWSCLISetup.exe\"<\/span> <span class=\"hljs-literal\">-OutFile<\/span> awscli.exe<\/p>\n<p><span class=\"hljs-comment\"># Install EC2Config<\/span><br \/>\n<span class=\"hljs-built_in\">Start-Process<\/span> C:\\Tmp\\Ec2Config.exe <span class=\"hljs-literal\">-ArgumentList<\/span> <span class=\"hljs-string\">\"\/Ec \/S \/v\/qn\"<\/span> <span class=\"hljs-literal\">-Wait<\/span><br \/>\n<span class=\"hljs-built_in\">Start-Sleep<\/span> <span class=\"hljs-literal\">-Seconds<\/span> <span class=\"hljs-number\">20<\/span><\/p>\n<p><span class=\"hljs-comment\"># Install AWS CLI<\/span><br \/>\n<span class=\"hljs-built_in\">Start-Process<\/span> C:\\Tmp\\awscli.exe <span class=\"hljs-literal\">-ArgumentList<\/span> <span class=\"hljs-string\">\"\/Ec \/S \/v\/qn\"<\/span> <span class=\"hljs-literal\">-Wait<\/span><br \/>\n<span class=\"hljs-built_in\">Start-Sleep<\/span> <span class=\"hljs-literal\">-Seconds<\/span> <span class=\"hljs-number\">20<\/span><\/p>\n<p><span class=\"hljs-comment\"># Install SSM Agent<\/span><br \/>\n<span class=\"hljs-built_in\">Start-Process<\/span> C:\\Tmp\\ssmagent.exe <span class=\"hljs-literal\">-ArgumentList<\/span> <span class=\"hljs-string\">\"\/Ec \/S \/v\/qn\"<\/span> <span class=\"hljs-literal\">-Wait<\/span><br \/>\n<span class=\"hljs-built_in\">Start-Sleep<\/span> <span class=\"hljs-literal\">-Seconds<\/span> <span class=\"hljs-number\">10<\/span><\/p>\n<p><span class=\"hljs-built_in\">Restart-Service<\/span> AmazonSSMAgent<\/p>\n<p><span class=\"hljs-built_in\">Remove-Item<\/span> C:\\Tmp <span class=\"hljs-literal\">-Recurse<\/span> <span class=\"hljs-literal\">-Force<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<h2 data-start=\"4438\" data-end=\"4469\">IAM Role for Systems Manager<\/h2>\n<p data-start=\"4471\" data-end=\"4561\">The EC2 instance must have an IAM role that allows it to communicate with Systems Manager.<\/p>\n<p data-start=\"4563\" data-end=\"4620\">Attach the following managed policy to the instance role.<\/p>\n<div class=\"contain-inline-size rounded-2xl corner-superellipse\/1.1 relative bg-token-sidebar-surface-primary\">\n<div class=\"sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl\/main:top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs\">\u00a0<\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre!\"><span class=\"hljs-attribute\">AmazonEC2RoleforSSM<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p data-start=\"4651\" data-end=\"4726\">Once attached, the role should appear under the instance IAM configuration.<\/p>\n<p data-start=\"4651\" data-end=\"4726\"><img decoding=\"async\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/01\/index.png\" alt=\"index\" \/><\/p>\n<h2 data-start=\"4733\" data-end=\"4775\">Step 2 Allowing EC2 to Write Logs to S3<\/h2>\n<p data-start=\"4777\" data-end=\"4837\">The EC2 instance also needs permission to upload logs to S3.<\/p>\n<p data-start=\"4839\" data-end=\"4888\">Attach the following policy to the same IAM role.<\/p>\n<div class=\"contain-inline-size rounded-2xl corner-superellipse\/1.1 relative bg-token-sidebar-surface-primary\">\n<div class=\"sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl\/main:top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs\">\u00a0<\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre!\"><span class=\"hljs-attribute\">AmazonS3FullAccess<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p data-start=\"4918\" data-end=\"5010\">In production environments, it is recommended to scope this permission to a specific bucket.<\/p>\n<p data-start=\"4918\" data-end=\"5010\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-809\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/01\/index-1.png\" alt=\"index\" width=\"510\" height=\"169\" \/><\/p>\n<h2 data-start=\"5017\" data-end=\"5065\">PowerShell Script for Log Archival and Upload<\/h2>\n<p data-start=\"5067\" data-end=\"5108\">Save the following script as shown below.<\/p>\n<div class=\"contain-inline-size rounded-2xl corner-superellipse\/1.1 relative bg-token-sidebar-surface-primary\">\n<div class=\"sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl\/main:top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs\">\u00a0<\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre!\"><span class=\"hljs-section\">C:\\Scripts\\termination.ps1<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p data-start=\"5146\" data-end=\"5189\">This script performs the following actions.<\/p>\n<ul data-start=\"5191\" data-end=\"5297\">\n<li data-start=\"5191\" data-end=\"5227\">\n<p data-start=\"5193\" data-end=\"5227\">Creates a date-stamped directory<\/p>\n<\/li>\n<li data-start=\"5228\" data-end=\"5257\">\n<p data-start=\"5230\" data-end=\"5257\">Archives application logs<\/p>\n<\/li>\n<li data-start=\"5258\" data-end=\"5297\">\n<p data-start=\"5260\" data-end=\"5297\">Uploads the archive to an S3 bucket<\/p>\n<\/li>\n<\/ul>\n<h3 data-start=\"5299\" data-end=\"5329\">Log Synchronization Script<\/h3>\n<div class=\"contain-inline-size rounded-2xl corner-superellipse\/1.1 relative bg-token-sidebar-surface-primary\">\n<div class=\"sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl\/main:top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs\">\u00a0<\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre!\"><span class=\"hljs-variable\">$Date<\/span> = <span class=\"hljs-built_in\">Get-Date<\/span> <span class=\"hljs-literal\">-Format<\/span> yyyy<span class=\"hljs-literal\">-MM-dd<\/span><br \/>\n<span class=\"hljs-variable\">$InstanceName<\/span> = <span class=\"hljs-string\">\"TerminationEc2\"<\/span><br \/>\n<span class=\"hljs-variable\">$LocalIP<\/span> = <span class=\"hljs-built_in\">Invoke-RestMethod<\/span> <span class=\"hljs-literal\">-Uri<\/span> <span class=\"hljs-string\">\"http:\/\/169.254.169.254\/latest\/meta-data\/local-ipv4\"<\/span><\/p>\n<p><span class=\"hljs-variable\">$WorkDir<\/span> = <span class=\"hljs-string\">\"C:\\Users\\Administrator\\workdir\\<span class=\"hljs-variable\">$InstanceName<\/span><\/span>-<span class=\"hljs-variable\">$LocalIP<\/span>-<span class=\"hljs-variable\">$Date<\/span>\\<span class=\"hljs-variable\">$Date<\/span>\"<\/p>\n<p><span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-built_in\">Test-Path<\/span> <span class=\"hljs-variable\">$WorkDir<\/span>) {<br \/>\n    <span class=\"hljs-built_in\">Remove-Item<\/span> <span class=\"hljs-variable\">$WorkDir<\/span> <span class=\"hljs-literal\">-Recurse<\/span> <span class=\"hljs-literal\">-Force<\/span><br \/>\n}<\/p>\n<p><span class=\"hljs-built_in\">New-Item<\/span> <span class=\"hljs-literal\">-ItemType<\/span> Directory <span class=\"hljs-literal\">-Path<\/span> <span class=\"hljs-variable\">$WorkDir<\/span><\/p>\n<p><span class=\"hljs-variable\">$SourcePathWeb<\/span> = <span class=\"hljs-string\">\"C:\\Source\\Application\\web\\logs\"<\/span><br \/>\n<span class=\"hljs-variable\">$DestFileWeb<\/span> = <span class=\"hljs-string\">\"<span class=\"hljs-variable\">$WorkDir<\/span><\/span>\\logs.zip\"<\/p>\n<p><span class=\"hljs-built_in\">Add-Type<\/span> <span class=\"hljs-literal\">-AssemblyName<\/span> <span class=\"hljs-string\">\"System.IO.Compression.FileSystem\"<\/span><br \/>\n[<span class=\"hljs-type\">System.IO.Compression.ZipFile<\/span>]::CreateFromDirectory(<span class=\"hljs-variable\">$SourcePathWeb<\/span>, <span class=\"hljs-variable\">$DestFileWeb<\/span>)<\/p>\n<p>&amp; <span class=\"hljs-string\">\"C:\\Program Files\\Amazon\\AWSCLI\\bin\\aws.cmd\"<\/span> s3 <span class=\"hljs-built_in\">cp<\/span> `<br \/>\n<span class=\"hljs-string\">\"C:\\Users\\Administrator\\workdir\"<\/span> `<br \/>\n<span class=\"hljs-string\">\"s3:\/\/terminationec2\"<\/span> `<br \/>\n<span class=\"hljs-literal\">--recursive<\/span> `<br \/>\n<span class=\"hljs-literal\">--region<\/span> us<span class=\"hljs-literal\">-east-1<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p data-start=\"6067\" data-end=\"6164\">Once executed manually, the script should complete successfully and upload logs to the S3 bucket.<\/p>\n<p data-start=\"6067\" data-end=\"6164\"><span style=\"font-size: 1rem;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-811\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/01\/index-3.png\" alt=\"index\" width=\"847\" height=\"225\" \/><\/span><\/p>\n<p data-start=\"6067\" data-end=\"6164\"><span style=\"font-size: 1rem;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-812\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/01\/index-4.png\" alt=\"index\" width=\"847\" height=\"225\" \/><\/span><\/p>\n<h2 data-start=\"6171\" data-end=\"6214\">Running the Script Using Systems Manager<\/h2>\n<p data-start=\"6216\" data-end=\"6293\">To automate execution, run this script using <strong data-start=\"6261\" data-end=\"6292\">Systems Manager Run Command<\/strong>.<\/p>\n<p data-start=\"6295\" data-end=\"6346\">Select the target instance and choose the document.<\/p>\n<div class=\"contain-inline-size rounded-2xl corner-superellipse\/1.1 relative bg-token-sidebar-surface-primary\">\n<div class=\"sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl\/main:top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs\">\u00a0<\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre!\"><span class=\"hljs-attribute\">AWS-RunPowerShellScript<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p data-start=\"6381\" data-end=\"6405\">Configure the following.<\/p>\n<div class=\"contain-inline-size rounded-2xl corner-superellipse\/1.1 relative bg-token-sidebar-surface-primary\">\n<div class=\"sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl\/main:top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs\">\u00a0<\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre!\"><span class=\"hljs-attr\">Commands:<\/span> <span class=\"hljs-string\">.\\termination.ps1<\/span><br \/>\n<span class=\"hljs-attr\">Working Directory:<\/span> <span class=\"hljs-string\">C:\\Scripts<\/span><br \/>\n<span class=\"hljs-attr\">Execution Timeout:<\/span> <span class=\"hljs-number\">3600<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<h2 data-start=\"6503\" data-end=\"6536\">Auto Scaling Group Preparation<\/h2>\n<p data-start=\"6538\" data-end=\"6622\">Ensure the AMI used by the Auto Scaling Group includes all the above configurations.<\/p>\n<p data-start=\"6624\" data-end=\"6724\">Create an AMI from a configured EC2 instance and update the launch configuration or launch template.<\/p>\n<p data-start=\"6726\" data-end=\"6777\">For this tutorial, the Auto Scaling Group is named.<\/p>\n<div class=\"contain-inline-size rounded-2xl corner-superellipse\/1.1 relative bg-token-sidebar-surface-primary\">\n<div class=\"sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl\/main:top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs\">\u00a0<\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre!\"><span class=\"hljs-attribute\">group_kaien<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<h2 data-start=\"6805\" data-end=\"6841\">Configuring CloudWatch Event Rule<\/h2>\n<p data-start=\"6843\" data-end=\"6916\">Create a CloudWatch Event rule to trigger when an instance is terminated.<\/p>\n<h3 data-start=\"6918\" data-end=\"6935\">Event Pattern<\/h3>\n<div class=\"contain-inline-size rounded-2xl corner-superellipse\/1.1 relative bg-token-sidebar-surface-primary\">\n<div class=\"sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl\/main:top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs\">\u00a0<\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre!\">{<br \/>\n  <span class=\"hljs-string\">\"source\"<\/span>: [<span class=\"hljs-string\">\"aws.autoscaling\"<\/span>],<br \/>\n  <span class=\"hljs-string\">\"detail-type\"<\/span>: [<br \/>\n    <span class=\"hljs-string\">\"EC2 Instance Terminate Successful\"<\/span>,<br \/>\n    <span class=\"hljs-string\">\"EC2 Instance-terminate Lifecycle Action\"<\/span><br \/>\n  ],<br \/>\n  <span class=\"hljs-string\">\"detail\"<\/span>: {<br \/>\n    <span class=\"hljs-string\">\"AutoScalingGroupName\"<\/span>: [<span class=\"hljs-string\">\"group_kaien\"<\/span>]<br \/>\n  }<br \/>\n}<\/code><\/div>\n<\/div>\n<div dir=\"ltr\">\u00a0<\/div>\n<div dir=\"ltr\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-819\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/01\/index-11.png\" alt=\"index\" width=\"539\" height=\"433\" \/><\/div>\n<h2 data-start=\"7161\" data-end=\"7190\">Event Target Configuration<\/h2>\n<p data-start=\"7192\" data-end=\"7238\">Set the target as Systems Manager Run Command.<\/p>\n<div class=\"contain-inline-size rounded-2xl corner-superellipse\/1.1 relative bg-token-sidebar-surface-primary\">\n<div class=\"sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl\/main:top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs\">\u00a0<\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre!\"><span class=\"hljs-attr\">Document:<\/span> <span class=\"hljs-string\">AWS-RunPowerShellScript<\/span><br \/>\n<span class=\"hljs-attr\">Target:<\/span> <span class=\"hljs-string\">Instance<\/span> <span class=\"hljs-string\">ID<\/span><br \/>\n<span class=\"hljs-attr\">Command:<\/span> <span class=\"hljs-string\">.\\termination.ps1<\/span><br \/>\n<span class=\"hljs-attr\">Working Directory:<\/span> <span class=\"hljs-string\">C:\\Scripts<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p data-start=\"7360\" data-end=\"7485\">This ensures that whenever an instance is terminated, the PowerShell script runs and synchronizes logs to S3 before shutdown.<\/p>\n<p data-start=\"7360\" data-end=\"7485\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-820\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/01\/index-12.png\" alt=\"index\" width=\"526\" height=\"521\" \/><\/p>\n<h2 data-start=\"7492\" data-end=\"7505\">Validation<\/h2>\n<p data-start=\"7507\" data-end=\"7582\">Trigger scale-out and scale-down events by adjusting Auto Scaling policies.<\/p>\n<p data-start=\"7584\" data-end=\"7694\">When instances are terminated, logs should appear in the S3 bucket with correct date and instance identifiers.<\/p>\n<p data-start=\"7701\" data-end=\"7714\"><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-821\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/01\/index-13.png\" alt=\"index\" width=\"572\" height=\"444\" \/><\/strong><\/p>\n<h2 data-start=\"7701\" data-end=\"7714\">Conclusion<\/h2>\n<p data-start=\"7716\" data-end=\"7952\">This setup ensures that application logs are safely preserved even when EC2 instances are terminated by an Auto Scaling Group. Logs are archived with proper timestamps and instance information, making debugging and auditing much easier.<\/p>\n<p data-start=\"7954\" data-end=\"8057\">With this approach, log retention is automated, reliable, and scalable for enterprise AWS environments.<\/p>\n<p data-start=\"8059\" data-end=\"8114\">Stay tuned for more practical infrastructure solutions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Logs play a critical role in any application or system. They provide deep visibility into what the application is doing, how requests are processed, and what caused an error. Depending on how logging is configured, logs may contain transaction history, timestamps, request details, and even financial information such as debits or credits. In enterprise &hellip; <a href=\"https:\/\/opstree.com\/blog\/2019\/01\/21\/log-parsing-of-windows-servers-on-instance-termination\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Log Parsing of Windows Servers on Instance Termination&#8221;<\/span><\/a><\/p>\n","protected":false},"author":171775670,"featured_media":29900,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[44070,676319254,768739308,768739298,882627,178495],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/11\/DevSecOps-1.jpg","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pfDBOm-3t","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/215"}],"collection":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/users\/171775670"}],"replies":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":15,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":30298,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/215\/revisions\/30298"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media\/29900"}],"wp:attachment":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}