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
Related
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
The app is a nodejs app deployed to AWS Lambda using Serverless. I have the production environment variables stored in .env-prod.json
serverless.yml:
custom:
stage: ${opt:stage, self:provider.stage}
service: my-backend
provider:
name: aws
runtime: nodejs14.x
stage: prod
region: us-east-1
memorySize: 128
functions:
app:
handler: index.handler
environment: ${file(./.env-${self:custom.stage}.json)}
events:
- http:
path: /
method: ANY
cors: true
- http:
path: /{proxy+}
method: ANY
cors: true
.env-prod.json:
{
"ENVIRONMENT": "prod",
"TEST1": "abc",
"TEST2": "abc2"
}
For the first serverless deploy I had only TEST1 var present and this deployed successfully. Now, after I added TEST2 var, then run serverless deploy, it does not deploy the new variable or any change to a variable, only code and code changes. In order to change or add a new var, I have to go to the AWS console UI and do it there.
Is there some special way to re-deploy the variables? I have tried the force option which had no effect.
I fixed this issue changing ${self:custom.stage} to ${opt:stage, self:provider.stage, 'dev'}
I hope it will work for your case. Tks
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 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 have to 2 service. I want to deploy it and get the same aws host.
1. This is first configuration serverless.yml
service: test
provider:
name: aws
runtime: nodejs8.10
stage: prod
region: ap-southeast-1
plugins:
- serverless-offline
functions:
bilangangenap:
handler: index.handler
events:
- http:
path: test/satu
method: post
timeout: 120
This is second configuration serverless.yml
service: test
provider:
name: aws
runtime: nodejs8.10
stage: prod
region: ap-southeast-1
plugins:
- serverless-offline
functions:
bilanganganjil:
handler: index.handler
events:
- http:
path: test/dua
method: post
timeout: 120
When I deploy first service, I got end point :
https://abcdefgh.execute-api.ap-southeast-1.amazonaws.com/prod/test/satu
And when I deploy second service, I got end point :
https://abcdefgh.execute-api.ap-southeast-1.amazonaws.com/prod/test/dua
But when I deploy second service, the first service will be deleted.
I want to use same host like : https://abcdefgh.execute-api.ap-southeast-1.amazonaws.com with different end point.