Comments (4)
Thanks for creating the issue, and preparing example repo for re-producing the problem.
Due to the nature of esbuild
there is no way to offload some dependencies into a shared AWS::Lambda::LayerVersion
. esbuild
itself bundles everything into single package and that package is deployed as lambda function. For that reason, if I remove the SharedLambdaLayer
and its reference in Lambda function, I can still build and invoke the function itself, even though the layer is removed from the template.
This is my updated template.yaml file;
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
lambda-layer-sync-ts
Sample SAM Template for lambda-layer-sync-ts
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs18.x
Architectures:
- x86_64
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get
Metadata: # Manage esbuild properties
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: "es2020"
Sourcemap: true
EntryPoints:
- app.ts
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
And I can get the sam local invoke
working without any issues;
❯ sam local invoke
Invoking app.lambdaHandler (nodejs18.x)
Local image is out of date and will be updated to the latest runtime. To skip this, pass in the parameter --skip-pull-image
Building image...........................................................................................................................
Using local image: public.ecr.aws/lambda/nodejs:18-rapid-x86_64.
Mounting /private/tmp/lambda-layer-sync-ts/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated, inside runtime container
START RequestId: 37febcd6-c1cf-4838-988d-825531bfb193 Version: $LATEST
END RequestId: 34cf177a-17b1-48e4-b526-af5ac85a44a8
REPORT RequestId: 34cf177a-17b1-48e4-b526-af5ac85a44a8 Init Duration: 0.03 ms Duration: 150.54 ms Billed Duration: 151 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"statusCode": 200, "body": "{\"message\":\"4 2\"}"}
However, when you remove the Layer, it might still not work since the layer contents and function contents are different folderes. sam sync
process listens to file changes in the folder which is reffered by CodeUri
property of the function. In order to get this work, you need to move your function and layer into a folder, and update your function definition to point to higher level folder.
- Inside
template.yaml
,CodeUri
points to/foo
- Your lambda function code is in
/foo/bar
- Your layer code is in
/foo/baz
Please let us know if you have other questions or issues.
from aws-sam-cli.
I'm not sure I understand. In the sample I shared I am in-fact offloading shared dependencies into a layer - which is working as expected. The bug is that during sam sync
, the layer is updated, which I can both manually inspect, and the logs state that the lambda functions which depend on the layers are updated. If I manually inspect the Lambda function that depends on the layer in the console, I can see that the layer it references has indeed been incremented, but the imported layer in the function itself is still the previous version, until a change to the lambda function code triggers a re-deployment and new lambda runtime.
In addition, if I'm not misstaken the change you suggest would mean that every function is re-built every time a change happens in one function. With your suggestion, my file structure would be
functions/
function1/
app.ts
function2/
app.ts
shared/
lib/
utils.ts
In my template, if I set
HelloWorldFunction1:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/
Handler: function1.app.lambdaHandler
...
HelloWorldFunction2:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/
Handler: function2.app.lambdaHandler
SharedLambdaLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: shared-layer
Description: Layer containing shared code
ContentUri: functions/lib/
Wouldn't that trigger all Lambdas to update if I update the code for one function?
from aws-sam-cli.
I just verified that is the case. I have 7 lambda function (and using the structure I mentioned in the previous message), and with the change you suggested each Lambda function is re-built during a sync
whenever there's a change in one single Lambda function. I understand that all need to be re-built when the Layer code changes, but updating the code of one Lambda function then re-builds and deploys all Lambda functions.
This is not desirable and greatly impacts the development experience. The sync now takes 7x longer, since I have 7 Lambda functions.
from aws-sam-cli.
Would appreciate some guidance here. Given the feedback from the CLI when using sync
, stating it is in fact updating functions that are referencing the layer, when the layer has been updated, this is a bug. The solution you suggested is not viable in multi-lambda projects.
from aws-sam-cli.
Related Issues (20)
- Bug: AWS::Serverless::Function ImageUri with function (ex: using !If) fails with type error HOT 3
- Bug: Template not rendered correctly HOT 4
- Why does aws-sam-cli not support debug mode on os runtime (provided.al2 / provided.al2023)? HOT 4
- Bug: sam local start-api fails to initialise lambda function containers within podman/toolbox container HOT 4
- Feature request: support Finch HOT 2
- Bug: sam local invoke - SSLError HOT 2
- start-api debug port failure HOT 1
- start-api debug port fails HOT 5
- Bug: `sam local invoke` takes client environment variable value over defined value in template.yaml HOT 3
- Sam build with project references in adjacent directory (.NET8 NativeAOT) HOT 4
- Bug: Docker 4.33 + Provided.al2 Image error HOT 9
- Bug: SAM Deployment Issue: Bootstrap File Not Found Despite Successful Build and Upload HOT 1
- Bug: sam pipeline init - ResourceNotFoundException HOT 1
- Bug: Official example for CDK is not working HOT 1
- Bug: Runtime.ImportModuleError for Typescript Lambda function defined using CDK HOT 2
- Feature request: Run Node tests as part of sam build HOT 1
- Bug: sam build --container-env-var is not working HOT 1
- Thumbprint of the AWS SAM Windows MSI installer does not match the documentation HOT 1
- Bug: ResourceDetailedStatus never returns as part of a cloudformation event HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aws-sam-cli.