@@ -175,33 +175,31 @@ export function hasSageMakerEnvVars(): boolean {
175175/**
176176 * Checks if the current environment is running on Amazon Linux 2.
177177 *
178- * This function detects if we're actually running on AL2 , not just if the host is AL2 .
179- * In containerized environments, we check the container's OS, not the host's .
178+ * This function detects the container/runtime OS , not the host OS .
179+ * In containerized environments, we check the container's OS identity .
180180 *
181181 * Detection Process (in order):
182182 * 1. Returns false for web environments (browser-based)
183- * 2. Returns false for SageMaker environments (even if host is AL2)
183+ * 2. Returns false for SageMaker environments (even if container is AL2)
184184 * 3. Checks `/etc/os-release` with fallback to `/usr/lib/os-release`
185- * - Standard Linux OS identification files
186- * - Explicitly checks for and rejects Amazon Linux 2023
185+ * - Standard Linux OS identification files per freedesktop.org spec
187186 * - Looks for `ID="amzn"` and `VERSION_ID="2"` for AL2
188- * 4. Falls back to kernel version check as last resort
189- * - Checks for `.amzn2.` or `.amzn2int.` in kernel release
190- * - Only used if file-based detection fails or confirms AL2
187+ * - This correctly identifies AL2 containers regardless of host OS
191188 *
192189 * This approach ensures correct detection in:
193190 * - Containerized environments (detects container OS, not host)
191+ * - AL2 containers on any host OS (Ubuntu, AL2023, etc.)
194192 * - Web/browser environments (returns false)
195- * - Amazon Linux 2023 systems (properly distinguished from AL2)
196193 * - SageMaker environments (returns false)
197194 *
195+ * Note: We intentionally do NOT check kernel version as it reflects the host OS,
196+ * not the container OS. AL2 containers should be treated as AL2 environments
197+ * regardless of whether they run on AL2, Ubuntu, or other host kernels.
198+ *
198199 * References:
199200 * - https://docs.aws.amazon.com/linux/al2/ug/ident-amazon-linux-specific.html
200201 * - https://docs.aws.amazon.com/linux/al2/ug/ident-os-release.html
201- *
202- * Example kernel versions:
203- * - `5.10.220-188.869.amzn2int.x86_64` (internal AL2)
204- * - `5.10.236-227.928.amzn2.x86_64` (Cloud Dev Machine)
202+ * - https://www.freedesktop.org/software/systemd/man/latest/os-release.html
205203 */
206204export function isAmazonLinux2 ( ) {
207205 // Skip AL2 detection for web environments
@@ -211,7 +209,7 @@ export function isAmazonLinux2() {
211209 }
212210
213211 // First check if we're in a SageMaker environment, which should not be treated as AL2
214- // even if the underlying host is AL2
212+ // even if the underlying container is AL2
215213 if ( hasSageMakerEnvVars ( ) ) {
216214 return false
217215 }
@@ -221,54 +219,36 @@ export function isAmazonLinux2() {
221219 return false
222220 }
223221
224- // For containerized environments, check the actual container OS
225- // not the host kernel version
222+ // Check the container/runtime OS identity via os-release files
223+ // This correctly identifies AL2 containers regardless of host OS
226224 try {
227225 const fs = require ( 'fs' )
228- // Check /etc/os-release with fallback to /usr/lib/os-release as per https://docs.aws.amazon.com/linux/al2/ug/ident-os-release.html
226+ // Check /etc/os-release with fallback to /usr/lib/os-release as per freedesktop.org spec
229227 const osReleasePaths = [ '/etc/os-release' , '/usr/lib/os-release' ]
228+
230229 for ( const osReleasePath of osReleasePaths ) {
231230 if ( fs . existsSync ( osReleasePath ) ) {
232231 try {
233232 const osReleaseContent = fs . readFileSync ( osReleasePath , 'utf8' )
234233 const osRelease = parseOsRelease ( osReleaseContent )
235234
236- // Check if this is Amazon Linux 2023 (not AL2)
237- if ( osRelease . VERSION_ID === '2023' || osRelease . PLATFORM_ID === 'platform:al2023' ) {
238- // This is Amazon Linux 2023, not AL2
239- return false
240- }
241-
242- // Check if this is actually Amazon Linux 2
243- // Must be specifically version 2, not 2023 or other versions
244- const isAL2 = osRelease . ID === 'amzn' && osRelease . VERSION_ID === '2'
245-
246- // If we found os-release file, trust its content over kernel version
247- if ( ! isAL2 ) {
248- // Explicitly not AL2 based on os-release
249- return false
250- }
251- // If it is AL2 according to os-release, continue to kernel check for confirmation
252- break // Found and processed os-release, no need to check fallback
235+ // Check if this is Amazon Linux 2
236+ // We trust os-release as the authoritative source for container OS identity
237+ return osRelease . VERSION_ID === '2' && osRelease . ID === 'amzn'
253238 } catch ( e ) {
254- // Continue to next path or fallback check
255- getLogger ( ) . error ( `Parsing os-release file failed with error : ${ e } ` )
239+ // Continue to next path if parsing fails
240+ getLogger ( ) . error ( `Parsing os-release file ${ osReleasePath } failed : ${ e } ` )
256241 }
257242 }
258243 }
259244 } catch ( e ) {
260- // If we can't read the files, fall back to the os.release() check
261- // This might happen in some restricted environments
262- getLogger ( ) . error ( `Checking the current environment failed with error: ${ e } ` )
245+ // If we can't read the files, we cannot determine AL2 status
246+ getLogger ( ) . error ( `Checking os-release files failed: ${ e } ` )
263247 }
264248
265- // Check kernel version as a fallback or confirmation
266- // This should only be trusted if we couldn't determine from files above
267- // or if files confirmed it's AL2
268- const kernelRelease = os . release ( )
269- const hasAL2Kernel = kernelRelease . includes ( '.amzn2int.' ) || kernelRelease . includes ( '.amzn2.' )
270-
271- return hasAL2Kernel
249+ // If no os-release files found or all failed to parse, assume not AL2
250+ // We do NOT fall back to kernel version as it reflects host OS, not container OS
251+ return false
272252}
273253
274254/**
0 commit comments