We are having projects created with aws codestar. these were working fine. but from today we are facing following issue:
Unable to upload artifact None referenced by CodeUri parameter of GetCompanyRecords resource.
zip does not support timestamps before 1980
Now when i removed aws-sdk module again it works fine. but when i add it again build fails. i am pretty much worried about this. Here is my lambda function.
GetCompanyRecords:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs6.10
Role:
Fn::ImportValue:
!Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
Timeout: 10
Events:
PostEvent:
Type: Api
Properties:
Path: /getCompanyRecords
Method: post
thanks in advance
At the moment following patch fixed my issue:
I added following lines to buildspec.yml after 'npm install'
-ls $CODEBUILD_SRC_DIR
-find $CODEBUILD_SRC_DIR/node_modules -mtime +10950 -exec touch {} ;
As i was having issue just by adding aws-sdk so i want aws to fix to this issue. I am really disappointed aws-sdk is not working with aws..
You have forgotten to initialize the codebase with git:). It means It's trying to create a zip from git's head but failing
rm -rf .git
git init
git add .
git commit -am 'First commit'
Related
I'm trying to deploy a basic serverless application that contains two Rust lambda functions. I'm using SAM to deploy the application.
The issue is how to get SAM to pick up the correct "bootstrap" file. Because both functions are built in the same CodeUri path, SAM does not execute both the Make commands. Instead, it just copies the output of Function1 to Function2 (this seems like a design flaw in SAM?). Thus, both lambdas currently get deployed with the same code.
My build directory is
myapp/
- src/
- bin/
- function1.rs (note: function1 & 2 depend on lib.rs)
- function2.rs
- lib.rs
- Cargo.toml
- Makefile
- template.yaml
The template.yaml file:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Handler: bootstrap.is.the.handler
Runtime: provided.al2
Architectures:
- x86_64
Resources:
Function1:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Function2:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
The Makefile is:
build-Function1:
cargo lambda build
cp ./target/lambda/function1/bootstrap $(ARTIFACTS_DIR)
build-Function2: # This never gets run!
cargo lambda build
cp ./target/lambda/function2/bootstrap $(ARTIFACTS_DIR)
Commands to build/deploy
sam build
sam deploy
I'm open to other build structures. I've also tried structuring the project using rust workspaces. But, because SAM copies the build source to a separate directory, I cannot find a way to add module dependencies.
After much struggle, I have come up with a hacky solution, that I'm sure cannot be the recommended way.
Use rust workspaces, so the folder structure is:
root/
common/
lib.rs
Cargo.toml
function1/
main.rs
Cargo.toml
Makefile
function2/
main.rs
Cargo.toml
Makefile
Cargo.toml
template.yaml
root/Cargo.toml:
[workspace]
members = [
"common"
"function1",
"function2",
]
Set template.yaml file to use different codeURIs:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Handler: bootstrap.is.the.handler
Runtime: provided.al2
Architectures:
- x86_64
Resources:
Function1:
Type: AWS::Serverless::Function
Properties:
CodeUri: function1
Function2:
Type: AWS::Serverless::Function
Properties:
CodeUri: function2
(The hack step) In each Makefile, cp back into the source dir so it builds with all the source. (This has added benefit of sharing the build cache between targets)
build-Function1:
cd $(PWD); cargo lambda build --release
cp $(PWD)/target/lambda/function1/bootstrap $(ARTIFACTS_DIR)
This solution is compatible with cargo lambda watch and sam build/sam deploy commands.
Binary size is large since all lambdas duplicate the base library. I'm uncertain if this is avoidable with Rust.
I'm yet to trial it with deployment from CI servers.
I have serverless.yaml script that use to work before - next after updating to newer version of SLS (2.72.0) I start getting warning:
Cannot resolve serverless.yaml: Variables resolution errored with:
- Cannot resolve variable at "custom.S3_BUCKET_NAME": Value not found at "self" source
my custom section looks like this:
custom:
S3_BUCKET_NAME: ${self:service}-data-${opt:stage, self:provider.stage}
s3Sync:
- bucketName: ${self:custom.S3_BUCKET_NAME}-website
localDir: ./dist
deleteRemoved: true
how I can fix this warning?
There is a slight change in variables resolution and in your case, the best way to resolve it would be to use the following syntax:
custom:
S3_BUCKET_NAME: ${self:service}-data-${sls:stage}
s3Sync:
- bucketName: ${self:custom.S3_BUCKET_NAME}-website
localDir: ./dist
deleteRemoved: true
for resolving the stage. Alternatively, you can use old syntax, but provide explicit fallback value for stage:
custom:
S3_BUCKET_NAME: ${self:service}-data-${opt:stage, self:provider.stage, 'dev'}
s3Sync:
- bucketName: ${self:custom.S3_BUCKET_NAME}-website
localDir: ./dist
deleteRemoved: true
I would recommend going with sls:stage version.
Changing the way you are writing the stage from:
self:provider.stage
To:
${sls:stage}
Should do the work!
You can find the updated documentation in: https://www.serverless.com/framework/docs/providers/aws/guide/variables or running serverless print for a more detailed response of the problem.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
The project I am currently working on creates a lambda layer which contains a file called app.py, within this file is a function named lambda_handler which is interest to be used as Handler for whatever lambda function includes the layer. The sam template I use to do this looks as follow:
Resources:
LamLayer:
Type: AWS::Serverless::LayerVersion
LayerName: !Join
- ''
- - 'LamLayer'
- - !Ref AWS::StackName
Properties:
ContentUri: ./lam_layer
CompatibleRuntimes:
- python3.8
Metadata:
BuildMethod: python3.8
LamFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./lam_function
Runtime: python3.8
Handler: app.lambda_handler
Layers:
- !Ref LamLayer
Timeout: 60
AutoPublishAlias: live
Now although the Handler: app.lambda_handler is not present in the lambda function itself, it is present in the included layer.
Now after creating this setup I tested it by calling sam build; sam deploy and it successfully deployed and worked. When I called the LamFunction it successfully found the Handler and ran it.
The problem arises when I push my changes to the CodePipeline we have setup. The build and deploy succeeded but when I now call the LamFunction it throws the following error:
Unable to import module 'app': No module named 'app'
After debugging this for a while I seem to have narrowed down the problem to the difference in the way I was building vs. how the pipeline is building the project.
I called: sam build; sam deploy
Whereas the pipeline calls: sam build; sam package --s3-bucket codepipeline-eu-central-1-XXXXXXXXXX --output-template-file packaged-template.yml and then uses the standard pipeline deploy stage to deploy from the S3 bucket.
But although I think I know that this difference is causing the problem I am not sure what the underlying reason is and what I need to change to fix it ?
---- EDIT ----
Here is the buildspec.yml in case this is the culprit:
version: 0.2
phases:
install:
runtime-versions:
python: 3.8
build:
commands:
- sam build
- sam package --s3-bucket codepipeline-eu-central-1-XXXXXXXXXX --output-template-file packaged-template.yml
artifacts:
files:
- packaged-template.yml
In the end I managed to trace the issue back to the CodeBuild image used in the pipeline. Due to an oversight during the creation of the pipeline I used a managed image which used the CodeBuild standard 1 which does not support the building of nested stacks/templates. Since the stack mentioned above was being build as nested stack of a larger template it was not build and so with cause the error with the layer.
After changing to the CodeBuild standard 3 the stack build and packaged as expected.
I have tried to upload my application using servless/lambda function AWS, but i got this issue:
An error occurred: AppLambdaFunction - Unzipped size must be smaller than 262144000 bytes (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException; Request ID: 8ea0d887-5743-4db1-96cd-6c5efa57b081).
What is the best way to resolve it?
Look my dependencies:
"dependencies": {
"ethereumjs-tx": "^1.3.7",
"aws-sdk": "^2.4.52",
"body-parser": "^1.18.3",
"compression": "^1.7.4",
"consign": "^0.1.6",
"cors": "^2.8.5",
"express": "^4.16.4",
"helmet": "^3.16.0",
"moment": "^2.24.0",
"openzeppelin-solidity": "^2.3.0",
"serverless": "^1.48.2",
"serverless-http": "^1.9.1",
"serverless-offline": "^4.9.4",
"truffle": "^5.1.9",
"truffle-hdwallet-provider": "^1.0.17",
"web3": "^1.2.5-rc.0"
},
Serverless.yml:
provider:
name: aws
runtime: nodejs8.10
stage: v1
region: us-east-1
timeout: 30
memorySize: 512
package:
excludeDevDependencies: true
exclude:
- .git/**
- .vscode/**
- venv/**
functions:
app:
handler: handler.run
events:
- http:
path: /
method: ANY
cors: true
- http:
path: /{proxy+}
method: ANY
cors: true
plugins:
- serverless-offline
Use the directive exclude at your serverless.yml file. In case of Python, I've been used it as follows:
package:
exclude:
- node_modules/**
- venv/**
The build process will exclude them from the build before sending to AWS.
Tip I got in this issue at Github. The documentation for this directive is detailed here.
You can use module bundlers to package the code.
Using module bundlers such as webpack
You can consider using plugins like serverless-webpack. The serverless-webpack plugin is using webpack to build the project and it will only include the bare minimum files required to run your application. It will not include the entire node_modules directory. so that your deployment package will be smaller.
a note about using of Lambda layers
Like others mentioned, you can use the layers and move some of the libraries and code to the layer. Layers are mainly used to share code between functions. The unzipped deployed package including layers cannot exceed 250MB.
hope this helps.
References:
https://github.com/serverless-heaven/serverless-webpack
https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path
I've had success resolving this error message using the serverless-esbuild plugin, and configuring it as follows in serverless.yml:
service: service_name
frameworkVersion: '3'
provider:
name: aws
runtime: nodejs12.x
plugins:
- serverless-esbuild
custom:
esbuild:
bundle: true
minify: false
sourcemap: true
exclude: 'aws-sdk'
target: node14
define:
'require.resolve': undefined
platform: node
concurrency: 10
You can load larger packages into AWS Lambda indirectly using s3:
Load your package into a bucket/key on S3
In the Lambda console choose Function Code -> Code Entry Type -> Upload a file from S3
See my answer here
You can deploy a Lambda function using a Docker image and it bypasses this problem, allowing a function with its dependencies to be as large as 10 gb.
Adding exclude under package is deprecated. We can use pattern to remove node_modules.
Example to remove files in the serverless.yml
...remaining props
package:
patterns:
- '!.git/**'
- '!test/**'
- '!e2e/**'
- '!src/**'
- '!node_modules/**'
Deprecation for exclude and
Pattern
Recently i faced the same issue, my total package size was more than 40mbs and it was also including the venv (python virtual environment) folder that resides in the project directory. I excluded it and build size got reduced to 16 mbs. and the project was deployed successfully. I added the following in serverless.yaml
package:
patterns:
- '!node_modules/**'
- '!venv/**'
- '!apienv/**'
- '!__pycache__/**'
I have the following project tree
Where nodejs folder is a lambda layer defined in the following serverless.yaml
service: aws-nodejs # NOTE: update this with your service name
provider:
name: aws
runtime: nodejs8.10
stage: dev
plugins:
- serverless-offline
layers:
layer1:
path: nodejs # required, path to layer contents on disk
name: ${self:provider.stage}-layerName # optional, Deployed Lambda layer name
functions:
hello:
handler: handler.hello
layers:
- {Ref: Layer1LambdaLayer}
events:
- http:
path: /dev
method: get
The layer1 only contains UUID package.
So when I try to run the lambda locally using serverless offline plugin, it says can't find module UUID.
But when I deploy the code to AWS, it run like a charm.
Any way we can get lambda layers running locally for testing purpose? and for speeding up the development?
Or is there any way where I can dynamically set the node_module path to point to the layer folder during the development and once I need to push to production, it change the path to the proper one
Ok after many trials, I figure out a working solution
I added a npm run command which export a temporary node_module path to the list of paths
"scripts": {
"offline": "export NODE_PATH=\"${PWD}/nodejs/node_modules\" && serverless offline"
},
So, node can lookup for the node modules inside the sub folders
I got around this by running serverless-offline in a container and copying my layers into the /opt/ directory with gulp. I set a gulp watch to monitor any layer changes and to copy them to the /opt/ directory.
I use layers in serverless offline via installing a layer from local file system as a dev dependency.
npm i <local_path_to_my_layer_package> --save-dev
BTW this issue was fixed in sls 1.49.0.
Just run:
sudo npm i serverless
Then you should specify package include in serverless.yml's layer section
service: aws-nodejs # NOTE: update this with your service name
provider:
name: aws
runtime: nodejs8.10
stage: dev
plugins:
- serverless-offline
layers:
layer1:
path: nodejs # required, path to layer contents on disk
package:
include:
- node_modules/**
name: ${self:provider.stage}-layerName # optional, Deployed Lambda layer name
functions:
hello:
handler: handler.hello
layers:
- {Ref: Layer1LambdaLayer}
events:
- http:
path: /dev
method: get
Tested on nodejs10.x runtime