Sam sync always updates infra on update lambda (hander.py) code
Description:
I have a simple api, defined with OpenAPI, that acts as a simple proxy to a lambda. The lambda code is in a seperate file, handler.py. When I start the sam sync command en then update the code in the handler.py, the infra always gets updated.
Steps to reproduce:
Source code is available in: https://github.com/ben-elsen/eventbroker-test
virtualenv --python=/usr/local/Cellar/[email protected]/3.8.6_1/bin/python3.8 venv
source venv/bin/activate
sam sync --watch --stack-name eventbroker-app-stack-dev -t sam.yml --profile assumeAdminINMSEventbridgeDev
Update the handler.py
Observed result:
See attachement Output sync after update code.txt
Expected result:
Not a sync of the whole infra but just the upload of the new code of the lambda
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS: MacOs Monterey 12.0.1
sam --version: 1.35.0- AWS region: eu-west-1
@mndeveci I think I stumbled upon the problem! When I start from the 'Hello World' sam app example, the sam sync command works like expected. When I then create a nested stack from the lambda, the sam command doesn't work anymore.
Why that behavior occurs is still a mystery to me..
Thanks for the steps @ben-elsen! As of now sam sync does not support nested stacks, we will be working to support that.
If you experience same problem with a template with no nested stacks please share the steps in this issue.
Hi @mndeveci, thanks for letting me know that!
Do you have any idea when it will become supported because every application that I make is based upon nested stacks.
Any update on this?
@ben-elsen The team is aware of the request. We do not have an update to share at this time. However, we do tag PRs with issues they solve. Once the team prioritizes and solves it, you will be able to see that link in the issue.
Ok thanks @jfuss !
+1 on wanting sam sync to work with nested stacks
@ben-elsen @bgeshk Just an update nested stack support is indeed in our to-do to get worked on! We will update you once the feature is out!
Thanks @qingchm , looking forward to it!
sounds good, thanks for the update! @qingchm
@ben-elsen & @bgeshk with the latest version of SAM CLI, we have partially support for nested stacks for sam sync (there are still some edge cases that we are addressing). We would really appreciate if you can you give it a try and give us feedback about your experience.
Thanks!
@mndeveci Thanks for the update! I will test it out the coming days and provide feedback!
exciting stuff, taking a look at this now. thanks! @mndeveci
@mndeveci here are my initial findings:
Running sam sync on an individual resource ✅
command: sam sync --stack-name <my_stack_name> -t <path_to_template_file> --profile <my_profile> --region <my_region> --code --resource-id <my_resource_id>
Running sam sync with the --watch flag ❌
command: sam sync --stack-name <my_stack_name> -t <path_to_template_file> --profile <my_profile> --region <my_region> --watch
What I saw happen was the cloud formation stack got updated just fine, and I saw the "Infra sync completed" message. Thus, I tried updating a file locally and got this error message:
Syncing Lambda Function <My_Lambda_Function>...
Code sync encountered an error.
Traceback (most recent call last):
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/samcli/lib/sync/sync_flow_executor.py", line 335, in _sync_flow_execute_wrapper
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/samcli/lib/sync/sync_flow.py", line 307, in execute
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/samcli/lib/sync/flows/zip_function_sync_flow.py", line 73, in set_up
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/samcli/lib/sync/flows/function_sync_flow.py", line 69, in set_up
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/samcli/lib/sync/sync_flow.py", line 84, in set_up
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/boto3/session.py", line 90, in __init__
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/boto3/session.py", line 131, in _setup_loader
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/botocore/session.py", line 769, in get_component
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/botocore/session.py", line 1079, in get_component
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/botocore/session.py", line 180, in <lambda>
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/botocore/session.py", line 303, in get_config_variable
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/botocore/configprovider.py", line 426, in get_config_variable
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/botocore/configprovider.py", line 628, in provide
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/botocore/configprovider.py", line 718, in provide
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/botocore/session.py", line 402, in get_scoped_config
botocore.exceptions.ProfileNotFound: The config profile (my_profile) could not be found
I know it is a valid profile, as it is used across most aws-cli commands, and it worked on the initial sam sync command. Any idea why the profile couldn't be found when I updated a local file & saved?
Thanks for the feedback @bgeshk let me give it a try and respond back to you!
Thanks for the feedback @bgeshk let me give it a try and respond back to you!
@mndeveci So, I ended up deleting my existing cloudformation stack and creating a new one - now I am able to hot reload the function, which is awesome. Have a couple questions:
- is it a hard requirement that you always have to MFA every time you save locally, or can there be some auth cache, similar to other sam-cli commands? I know an alternative is looking into using
aws-vault, but I do not currently use that - It also MFAs for trying to sync the lambda layer after the lambda function gets synced, and I am seeing this error
Code sync encountered an error.
Traceback (most recent call last):
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/samcli/lib/sync/sync_flow_executor.py", line 335, in _sync_flow_execute_wrapper
dependent_sync_flows = sync_flow.execute()
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/samcli/lib/sync/sync_flow.py", line 307, in execute
self.set_up()
File "/opt/homebrew/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/samcli/lib/sync/flows/auto_dependency_layer_sync_flow.py", line 72, in set_up
raise NoLayerVersionsFoundError(layer_name)
samcli.lib.sync.exceptions.NoLayerVersionsFoundError: <lambda_function_name>DepLayer doesn't have any versions in remote.
not a huge deal, since mainly just trying to sync my lambda code, but is there any way to ignore the layer sync or have this error not occur? For context, I have a global layer that's created through CloudFormation and attached to each lambda. The layer itself has a ContentUri property where I specify the path to the zip file that contains my dependencies (I think the more common use case is storing this in S3), but that is not the current implementation
@mndeveci My first go looks promising! The code changes are updated without the infra being updated, awesome!
I do however have the same error as @bgeshk but I don't understand why. The layer that I have attached globally is one of Thundra, do I get the error because it is located in another account and I don't have access to edit it?
Infra sync completed.
Syncing Lambda Function Cognito/LambdaTriggers/PostConfirmationHook/Lambda...
Building codeuri: /Users/benelsen/Engineering/Air/Microservice-User-App/cognito/lambda_triggers/post_confirmation/src runtime: python3.8 metadata: {} architecture: x86_64 functions: ['Cognito/LambdaTriggers/PostConfirmationHook/Lambda']
requirements.txt file not found. Continuing the build without dependencies.
Running PythonPipBuilder:CopySource
Finished syncing Lambda Function Cognito/LambdaTriggers/PostConfirmationHook/Lambda.
Syncing Alias live and Version of Cognito/LambdaTriggers/PostConfirmationHook/Lambda...
Syncing Layer CognitoLambdaTriggersPostConfirmationHookLambdaeeb1d34dDepLayer...
Code sync encountered an error.
Traceback (most recent call last):
File "/usr/local/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/samcli/lib/sync/sync_flow_executor.py", line 335, in _sync_flow_execute_wrapper
dependent_sync_flows = sync_flow.execute()
File "/usr/local/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/samcli/lib/sync/sync_flow.py", line 307, in execute
self.set_up()
File "/usr/local/Cellar/aws-sam-cli/1.52.0/libexec/lib/python3.8/site-packages/samcli/lib/sync/flows/auto_dependency_layer_sync_flow.py", line 72, in set_up
raise NoLayerVersionsFoundError(layer_name)
samcli.lib.sync.exceptions.NoLayerVersionsFoundError: microservice-usebb307aa1-CognitoLambdaTriggersPeeb1d34d-DepLayer doesn't have any versions in remote.
Finished syncing Alias live and Version of Cognito/LambdaTriggers/PostConfirmationHook/Lambda.
@bgeshk can you explain a little bit more about how you are setting up your credentials? Is it asking for MFA every time you need to make an API call?
Regarding to the <lambda_function_name>DepLayer doesn't have any versions in remote. error, if you are trying to sync a function which has no dependencies, this is a known error that is been fixed but not shipped yet. In the meantime, you can run sam sync with --no-dependency-layer flag for a workaround for now. Let us know if you still experience the same issue. Here is the related GH issue: https://github.com/aws/aws-sam-cli/issues/3945, please let me know if your case is different.
@ben-elsen that could be the same error that is reported here https://github.com/aws/aws-sam-cli/issues/3945. If you are trying to sync a function which has no dependencies, you might get this error. For a workaround, you can run sam sync with --no-dependency-layer flag for now. And please let us know if your case is different.
This message came a bit late but as you can see, our 1.53.0 release already supports nested stacks with sam sync! Please have a try and if you still have any questions or issues feel free to re-open the issue! Also for the issue that you mentioned above @ben-elsen I believe that it should have also been addressed, please have a try! Thanks for supporting sam accelerate!