I am having a tough time setting up the lambda layers with lambdas. I am using node 14.
My folder structure for lambda layer
layer/
nodejs/
node14/
node_modules/
hey.js
I have also tried having only the nodejs directory as below
layer/
nodejs/
hey.js
But in both cases I get cannot found module error in the lambda.
The paths I tried for accessing layers in lambda are as below
'/opt/nodejs/node14/node_modules/hey.js' (for first folder structure)
'/opt/nodejs/hey.js' (for folder structure with only the nodejs directory in the layer)
'hey.js' (trying to access the file directly)
But I had no luck. What am I doing wrong?
I am using AWS sam to deploy lambda and layers. I could see the layer getting attached to lambda on the console.
Here is my SAM template
layer1:
Type: AWS::Serverless::LayerVersion
Properties:
ContentUri: ./src/lambda-layers/layer1
CompatibleRuntimes:
- nodejs14.x
Metadata:
BuildMethod: nodejs14.x
lambda1:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: ./src/lambdas/lambda1
Handler: index.handler
Role: !GetAtt LambdaExecutionRole.Arn
Layers:
- !Ref layer1
Events:
AwsIoTMetadata:
Type: Api
Properties:
RestApiId: !Ref CrApi
Path: /user
Method: GET
How to access the layer in lambda?
Please Help. Thanks in advance
Related
I have a lambda with node and for local deployment I am using SAM CLI. This lambda requires some parameters in the SSM parameter store to be able to connect to the DB.
I configured the AWS_ACCES_KEY_ID and AWS_SECRET_ACCESS_KEY, as environment variables, in addition to the region. When executing the local lamda, I do not get any error, as it goes to aws, but it does not bring me anything. It is not a code issue, because if I deploy it already in aws it works without problem. I don't know if I need to do another configuration for it to work.
template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
ciencuadras-appraisal-request
Sample SAM Template for ciencuadras-appraisal-request
Parameters:
Stage:
Type: String
Default: dev
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Resources:
ApiDeployment:
Type: AWS::Serverless::Api
Properties:
StageName: !Ref Stage
RequestAppraisalFunction:
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: dist/
Handler: main.handler
Runtime: nodejs14.x
Environment:
Variables:
AWS_REGION: 'us-east-1'
Events:
RequestAppraisal:
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: /sendemail-new-appraisal
Method: post
RestApiId: !Ref ApiDeployment
Thanks
Currently there is no possibility to access Parameter Store variables from Sam Local as you can read up here.
Instead, you can use --env-vars option on SAM CLI to pass values to
the running function.
You can still use SSM in your template and pass --env-vars when invoking the function locally, for example:
template.yaml
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: dist/
Handler: main.handler
Runtime: nodejs14.x
Environment:
Variables:
API_KEY: !Sub '{{resolve:ssm:API_KEY:1}}'
env.json:
{
"Parameters": {
"API_KEY": "123"
}
}
pass env.json when invoking the function:
sam local invoke --env-vars env.json MyFunction
Note: If you are running API locally too, you can do:
sam local start-api --env-vars env.json
I created a lambda layer with serverless successfully but now I have to do the same with SAM CLI but i can't.
With serverless I only use two files:
serverless.yml
awswrangler-layer-0.0.23-py3.7.zip
serverless.yml content below:
service: MyService
provider:
name: aws
layers:
awswrangler:
package:
artifact: awswrangler-layer-0.0.23-py3.7.zip
How can I do the same with SAM CLI? Please give an example of the template.yaml
I have to unzip and this was the solution:
AwswranglerLayer :
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Join ['-', [!Ref Project, !Ref Environment, 'AwswranglerLayer']]
ContentUri: ../layers/awswrangler/
Description: "ETL and wrangling utility belt to handle data on AWS. Pandas, PySpark"
CompatibleRuntimes:
- python3.7
- python3.8
RetentionPolicy: Retain
AwswranglerLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub '${EnvironmentKey}-AwswranglerLayer'
CompatibleRuntimes:
- nodejs12.x
ContentUri: AwswranglerLayerPath/
I am using aws-serverless-express for deploying an express api on aws lambda. I followed the aws-serverless-express repository example (https://github.com/awslabs/aws-serverless-express/tree/master/examples/basic-starter) to deploy the api and it works, but now I don't know how to set up environment variables in express code then after the express deployment I can see and edit those environment variables on lambda console. I didn't find any documentation about this.
In the repository you mentioned, the cloudformation.yaml file has the function definition called YOUR_SERVERLESS_EXPRESS_LAMBDA_FUNCTION_NAME. you can define an attribute called Environment under that. see the example below.
YOUR_SERVERLESS_EXPRESS_LAMBDA_FUNCTION_NAME:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./
Handler: lambda.handler
MemorySize: 1024
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: nodejs8.10
Timeout: 30
Environment:
Variables:
SOME_VAR: value
Events:
ProxyApiRoot:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /
Method: ANY
ProxyApiGreedy:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /{proxy+}
Method: ANY
I am writing a serverless app by using SAM. I created a config folder to keep some table information and some other info. then I load it in my app.js.
when I deploy the app.js locally by using SAM deploy, I observe that the config folder will not include. would you mind advise me how to add config folder in final build folder in .aws-sam\build folder?
my Yaml file
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Sample SAM Template for test
Globals:
Function:
Timeout: 120
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs10.x
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
Also when I run the project in debug mode I am getting this error:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module '../config/config.js'"
}
I load the js file as below:
"use strict";
let response;
const AWS = require('aws-sdk');
const config = require('../config/config.js');
To include custom files that you need to re-use in multiple functions like the config file in your case. Then you can use lambda layers.
In your template.yml you would include a layer as follow:
ConfigLayer:
Type: "AWS::Serverless::LayerVersion"
Properties:
CompatibleRuntimes:
- nodejs10.x
ContentUri: ./config/
and then add it to your lambda function definition:
Type: AWS::Serverless::Function
Properties:
Handler: cmd/lambdas/hello-world/app.lambdaHandler
CodeUri: src/
Runtime: nodejs10.x
Layers:
- Ref: ConfigLayer
Events:
CatchAll:
Type: Api
Properties:
Path: /hello-world
Method: GET
The contents of the config/ directory will be available in the /opt/ path.
that means the full path to your config.js will be /opt/config.js and you can access it from any lambda that uses that layer.
I am new to serverless framework.
I was trying to deploy my code to lambda using serverless.
service:
name: store-consumer
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: ap-XXXXXX-1
functions:
lambda:
handler: index.handler
The content of the serverless.yml file is as given above.
But when I hit 'sls deploy' in terminal my code is zipped and uploaded to an s3 bucket. How do I deploy my code to the corresponding lambda using serverless?
I assume I 'll have to give some credentials for the lambda, but how do I do that in the .yml file?! What am I not getting correctly?
You can specify the Lambda function name explicitly using the name field. Example:
service:
name: store-consumer
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: ap-XXXXXX-1
functions:
lambda:
handler: index.handler
name: myfunc
With this config file your deployed Lambda function will have the name myfunc.
See line 129 in https://serverless.com/framework/docs/providers/aws/guide/serverless.yml/.
Using the name of an already existing Lambda function will not work, you still have to delete old the Lambda function beforehand.