aws / aws/aws-sam-cli

Bug: sam sync does not update layer version in dependent lambdas

Open
#4,077 9 comments 2 reactions 0 assignees View on GitHub
area/sync stage/bug-repro type/bug
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

### Description:

`sam sync` `--watch` and `--code` does not sync `Function Layer Reference` under certain conditions when Layer itself gets synced.

### Steps to reproduce:

Create serverless template yaml and put it in **\/cf/template.yaml**
```
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Sam sync

Globals:
Function:
Layers:
- !Ref LambdaLayer

Resources:
LambdaLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub my-layer-${AWS::StackName}
Description: Common Library
ContentUri: ../src/common/
CompatibleRuntimes:
- nodejs14.x
Metadata:
BuildMethod: makefile

LambdaFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub test-sam-sync-${AWS::StackName}
CodeUri: ../src/handlers
Handler: handler.handler
Runtime: nodejs14.x
MemorySize: 128
```
**\/src/common/Makefile**
```
build-LambdaLayer:
cp -r ./ "$(ARTIFACTS_DIR)"
```
**\/src/common/log.js**
```
module.exports = {
log: () => {
console.log('dummy');
},
};
```
**\/src/handlers/handle.js**
```
const { log } = require('/opt/log');

exports.handler = async (event, context) => {
log();
};
```

From the **rooDir** run sam sync:
```
sam sync \
-t ./cf/template.yaml \
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
--stack-name sam-sync-test \
--watch

```
Try to change code in the layer **\/src/common/log.js**

### Observed result:

Updated layer gets synced but Lambda's Layer Reference does not get updated, meaning Lambda still points to the old layer version

```
sam sync \
-t ./cf/template.yaml \
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
--stack-name sam-sync-test \
--s3-bucket sam-sync-test \
--s3-prefix sam-sync \
--watch
--debug

The SAM CLI will use the AWS Lambda, Amazon API Gateway, and AWS StepFunctions APIs to upload your code without
performing a CloudFormation deployment. This will cause drift in your CloudFormation stack.
**The sync command should only be used against a development stack**.
Confirm that you are synchronizing a development stack.

Enter Y to proceed with the command, or enter N to cancel:
[Y/n]: y
Queued infra sync. Wating for in progress code syncs to complete...
Starting infra sync.
Valid cache found, copying previously built resources for following layers (LambdaLayer)
Building codeuri: /home/max/study/aws/maksym.dukov/sam-data-processing/cf runtime: nodejs14.x metadata: {} architecture: x86_64 functions: LambdaFunction
package.json file not found. Continuing the build without dependencies.
Running NodejsNpmBuilder:CopySource

Build Succeeded

Successfully packaged artifacts and wrote output template to file /tmp/tmpm8fplfsb.
Execute the following command to deploy the packaged template
sam deploy --template-file /tmp/tmpm8fplfsb --stack-name

Deploying with following values
===============================
Stack name : sam-sync-test
Region : us-east-1
Disable rollback : False
Deployment s3 bucket : sam-sync-test
Capabilities : ["CAPABILITY_IAM", "CAPABILITY_NAMED_IAM", "CAPABILITY_AUTO_EXPAND"]
Parameter overrides : {}
Signing Profiles : null

Initiating deployment
=====================

2022-07-23 22:08:25 - Waiting for stack create/update to complete

CloudFormation events from stack operations (refresh every 0.5 seconds)
---------------------------------------------------------------------------------------------------------------------
ResourceStatus ResourceType LogicalResourceId ResourceStatusReason
---------------------------------------------------------------------------------------------------------------------
UPDATE_IN_PROGRESS AWS::CloudFormation::Stack sam-sync-test Transformation succeeded
UPDATE_IN_PROGRESS AWS::CloudFormation::Stack AwsSamAutoDependencyLayerNe -
stedStack
UPDATE_COMPLETE AWS::CloudFormation::Stack AwsSamAutoDependencyLayerNe -
stedStack
UPDATE_COMPLETE_CLEANUP_IN_ AWS::CloudFormation::Stack sam-sync-test -
PROGRESS
UPDATE_COMPLETE AWS::CloudFormation::Stack AwsSamAutoDependencyLayerNe -
stedStack
UPDATE_COMPLETE AWS::CloudFormation::Stack sam-sync-test -
---------------------------------------------------------------------------------------------------------------------

Stack update succeeded. Sync infra completed.

Infra sync completed.
Syncing Layer LambdaLayer...
Cache is invalid, running build and copying resources for following layers (LambdaLayer)
Building layer 'LambdaLayer'
Running CustomMakeBuilder:CopySource
Running CustomMakeBuilder:MakeBuild
Current Artifacts Directory : /home/max/study/aws/maksym.dukov/sam-data-processing/.aws-sam/auto-dependency-layer/LambdaLayer
cp -r ./ "/home/max/study/aws/maksym.dukov/sam-data-processing/.aws-sam/auto-dependency-layer/LambdaLayer"
Updated source_hash and manifest_hash field in build.toml for layer with UUID 432944d3-6c2a-4123-866e-6ef7c6acef96
Finished syncing Layer LambdaLayer.

```

### Expected result:

Lambda's Layer Reference gets updated

### Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
Workaround (1) to make it all work - specify `- !Ref LambdaLayer` in a **lambda** declaration section instead of the **Globals** section

Workaround (2) - move **template.yaml** into the root directory.

1. OS: ManjaroLinux 21.3.5
4. `sam --version`: SAM CLI, version 1.53.0
5. AWS region: us-east-1

`Add --debug flag to command you are running`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.