aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

Sam sync always updates infra on update lambda (hander.py) code

Open ben-elsen opened this issue 5 years ago • 20 comments

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)

  1. OS: MacOs Monterey 12.0.1
  2. sam --version: 1.35.0
  3. AWS region: eu-west-1

Output sync after update code.txt

ben-elsen avatar Nov 09 '21 17:11 ben-elsen

@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..

ben-elsen avatar Nov 12 '21 16:11 ben-elsen

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.

mndeveci avatar Nov 16 '21 05:11 mndeveci

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.

ben-elsen avatar Nov 16 '21 06:11 ben-elsen

Any update on this?

ben-elsen avatar Nov 23 '21 08:11 ben-elsen

@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.

jfuss avatar Nov 23 '21 20:11 jfuss

Ok thanks @jfuss !

ben-elsen avatar Nov 25 '21 10:11 ben-elsen

+1 on wanting sam sync to work with nested stacks

bgeshk avatar Dec 21 '21 22:12 bgeshk

@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!

qingchm avatar Jan 28 '22 09:01 qingchm

Thanks @qingchm , looking forward to it!

ben-elsen avatar Jan 28 '22 09:01 ben-elsen

sounds good, thanks for the update! @qingchm

bgeshk avatar Jan 31 '22 14:01 bgeshk

@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 avatar Jun 10 '22 17:06 mndeveci

@mndeveci Thanks for the update! I will test it out the coming days and provide feedback!

ben-elsen avatar Jun 11 '22 15:06 ben-elsen

exciting stuff, taking a look at this now. thanks! @mndeveci

bgeshk avatar Jun 14 '22 21:06 bgeshk

@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?

bgeshk avatar Jun 15 '22 00:06 bgeshk

Thanks for the feedback @bgeshk let me give it a try and respond back to you!

mndeveci avatar Jun 15 '22 00:06 mndeveci

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:

  1. 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
  2. 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

bgeshk avatar Jun 15 '22 17:06 bgeshk

@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.

ben-elsen avatar Jun 18 '22 13:06 ben-elsen

@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.

mndeveci avatar Jun 22 '22 17:06 mndeveci

@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.

mndeveci avatar Jun 22 '22 17:06 mndeveci

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!

qingchm avatar Jul 21 '22 18:07 qingchm