TypeError: can't subtract offset-naive and offset-aware datetimes during sam sync --watch
Description:
When running sam sync --watch with a nested stack containing an ECR-based Lambda function, the sync fails with a datetime comparison error in SAM's internal code.
SAM CLI Version: 1.149.0
OS: Linux 6.8.0-87-generic (Ubuntu)
Steps to Reproduce:
-
Create a SAM template with a nested stack (AWS::CloudFormation::Stack) that references a local template: RecruiticsMLStack: Type: AWS::CloudFormation::Stack Properties: TemplateURL: ./THEMLTemplate.yaml Parameters: EnvType: !Ref EnvType
-
The nested template contains a Lambda function using an ECR image: generateRecruiticsFlags: Type: AWS::Serverless::Function Properties: PackageType: Image ImageUri: !Sub "${AWS::AccountId}.dkr.ecr.${AWS::Region}.a mazonaws.com/my-repo:latest"
-
Run sam sync --watch: sam sync --watch --stack-name my-stack --region eu-west-1 --parameter-overrides EnvType=dev --template THETemplate.yaml
Expected Behavior:
Infra sync completes successfully.
Actual Behavior:
Build succeeds, but infra sync fails with:
Failed to sync infra. Code sync is paused until template/stack is fixed. Traceback (most recent call last): File "samcli/lib/sync/watch_manager.py", line 269, in _execute_infra_sync File "samcli/lib/sync/watch_manager.py", line 199, in _execute_infra_context File "samcli/lib/sync/infra_sync_executor.py", line 176, in execute_infra_sync TypeError: can't subtract offset-naive and offset-aware datetimes
Additional Context:
- The nested stack deploys successfully via sam deploy (non-watch mode)
- SAM shows this warning before the error: The resource AWS::Serverless::Function 'generateRecruiticsFlags' has specified ECR registry image for ImageUri. It will not be built and SAM CLI does not support invoking it locally.
- The error appears to be in SAM's datetime handling when comparing stack update times, not in the template itself
Workaround:
Using sam deploy instead of sam sync --watch works correctly (this is a terrible work-flow of course with poor feedback loop experience compared to sam sync --watch)
Thanks for reporting this!
I was able to reproduce the issue. The root cause is a datetime timezone mismatch in samcli/commands/sync/sync_context.py - when loading timestamps from .aws-sam/sync.toml, datetime.fromisoformat() returns timezone-naive datetimes if the ISO string lacks the +00:00 suffix (e.g., sync_time = "2025-12-03T22:24:14.920432" instead of sync_time = "2025-12-03T22:24:14.920432+00:00"), but the comparison code expects timezone-aware datetimes.
I've reproduced the issue by removing the +00:00 suffix from the file and update the code to trigger the synce and confrim the issue. The fix is to load all timestamps to be timezone-aware UTC, maintaining consistency with how SAM CLI writes them.
I'll have the fix out in a few moment.
In the meantime, make sure your .aws-sam/sync.toml has the +00:00 suffix