When Serverless WarmUp Plugin invoke my lambda , than my lambda gives error when i manually invoke it workes fine - node.js

**serverless. yml **.
service: LambdaColdStartRnD
configValidationMode: error
provider:
name: aws
runtime: nodejs14.x
memorySize: 512
timeout: 30
stage: development
region: ap-south-1
lambdaHashingVersion: 20201221
iamRoleStatements:
- Effect: 'Allow'
Action:
- 'lambda:InvokeFunction'
Resource: '*'
plugins:
- serverless-webpack
- serverless-plugin-warmup
functions:
api:
handler: lambda.handler
events:
- http: ANY /
- http: 'ANY /{proxy+}'
package:
individually: true
patterns:
- '!node_modules/**'
custom:
warmup:
RNDwarmer:
enabled: true
role: IamRoleLambdaExecution
events:
- schedule: 'cron(0/2 * ? * * *)'
concurrency: 5
prewarm: true
webpack:
webpackConfig: 'webpack.config.js' # Name of webpack configuration file
includeModules: false # Node modules configuration for packaging
packager: 'npm' # Packager that will be used to package your external modules
excludeFiles: src/**/*.test.js # Provide a glob for files to ignore.
I have defined a custom warmup which creates 5 containers and it is itializing the function with 5 conatiners but cant invoke function below is screenshot of xray traces and logs.
.

Related

Generate communication between lambdas - AWS SAM - Step Functions

I am learning to use AWS SAM and it occurred to me that when receiving a payload a lambda has the responsibility of verifying that what is expected is correct. If it is, another lambda will be called to tokenize its data and once that process is finished, call two more lambdas to save the results obtained.
The structure of this project that I am trying to do is as follows:
and my template.yaml file is organized as follows:
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
lambda-data-dictionary-register
Sample SAM Template for step-function
Resources:
StockTradingStateMachine:
Type: AWS::Serverless::StateMachine # More info about State Machine Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html
Properties:
DefinitionUri: statemachine/data-dictionary.asl.json
DefinitionSubstitutions:
DataDictionaryFunctionArn: !GetAtt DataDictionaryFunction.Arn
TokenizeFunctionArn: !GetAtt TokenizeFunction.Arn
MongoDBFunctionArn: !GetAtt MongoDBFunction.Arn
RedisFunctionArn: !GetAtt RedisFunction.Arn
Events:
HourlyTradingSchedule:
Type: Schedule # More info about Schedule Event Source: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-statemachine-schedule.html
Properties:
Description: Schedule to run the stock trading state machine every hour
Enabled: False # This schedule is disabled by default to avoid incurring charges.
Schedule: "rate(1 hour)"
Policies: # Find out more about SAM policy templates: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-templates.html
- LambdaInvokePolicy:
FunctionName: !Ref DataDictionaryFunction
- LambdaInvokePolicy:
FunctionName: !Ref TokenizeFunction
- LambdaInvokePolicy:
FunctionName: !Ref MongoDBFunction
- LambdaInvokePolicy:
FunctionName: !Ref RedisFunction
DataDictionaryFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
Properties:
CodeUri: functions/data-dictionary-register/
Handler: app.lambdaHandler
Runtime: nodejs16.x
Architectures:
- x86_64
Events:
Api:
Type: Api
Properties:
Path: /api/data-dictionary-register
Method: GET
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: false
Target: 'es2020'
Sourcemap: true
UseNpmCi: true
TokenizeFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
Properties:
CodeUri: functions/tokenize-data/
Handler: app.lambdaHandler
Runtime: nodejs16.x
Architectures:
- x86_64
Events:
Api:
Type: Api
Properties:
Path: /api/tokenize-data
Method: GET
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: false
Target: 'es2020'
Sourcemap: true
UseNpmCi: true
MongoDBFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/lambda-mongo-db/
Handler: app.lambdaHandler
Runtime: nodejs16.x
Architectures:
- x86_64
Events:
Api:
Type: Api
Properties:
Path: /api/lambda-mongo-db
Method: GET
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: false
Target: 'es2020'
Sourcemap: true
UseNpmCi: true
RedisFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/lambda-redis/
Handler: app.lambdaHandler
Runtime: nodejs16.x
Architectures:
- x86_64
Events:
Api:
Type: Api
Properties:
Path: /api/lambda-redis
Method: GET
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: false
Target: 'es2020'
Sourcemap: true
UseNpmCi: true
With all of the above, I want to know how I can make the data-dictionary-register lambda, when processing an incoming data and the result is successful, pass a JSON to the tokenize-data lambda, and this in turn sends it to the other two (lambda-mongo-db and lambda-redis). I want to emphasize that I am working in my local environment and the ideal now is to do everything there.
Ultimately, my question is: how do I make the end of one successful process the start of another?
Additionally, I indicate that my test files at the moment are found with this structure in their corresponding app.ts:
At the time I tried with Axios pasting for example to http://127.0.0.1:8080/api/tokenize-data from http://127.0.0.1:8080/api/data-dictionary-register but it always gives an error and reading me I ran into step functions...

Not able to attach EventBridge Schedule to Lambda Function in AWS SAM

I'm unable to attach an EventBridge Schedule to a lambda function in AWS SAM.
Here is the section of my SAM template that intends to attach a schedule to the Lambda function:
TestLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: TestLambda
CodeUri: test_directory/
Handler: test_file.lambda_handler
Runtime: python3.8
Role: !Ref LambdaApiRole
Events:
TestLambdaCron:
Type: Schedule
Properties:
Schedule: rate(1 day)
This is the error I get while deploying the template:
Embedded stack <stack ARN> was not successfully updated. Currently in UPDATE_ROLLBACK_IN_PROGRESS with reason: The following resource(s) failed to create: [TestLambdaTestLambdaCron].
A solution would be much appreciated, thanks in advance!
PS: LambdaApiRole is defined in the template and can be referenced in the function (working fine), it's when TestLambdaCron is added when the issues occur
The AWS SAM CLI version I am working on is: SAM CLI, version 1.37.0
Here is simple template that I have it and it works.
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
Sample SAM Template for demo
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Resources:
TestFunctionLambda:
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: app/
Handler: app.lambda_handler
Runtime: python3.9
Architectures:
- x86_64
Role: arn:aws:iam::631249183025:role/mycstomrole
Events:
CWSchedule:
Type: Schedule
Properties:
Schedule: "rate(1 minute)"
Name: TestSchedule
Description: test schedule
Enabled: true

Code Pipeline not able to create Layers in Lambda using sam template

I have below sam template
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Specification template describing lambda 1 function.
Resources:
awstransformedcurlambdafn:
Type: 'AWS::Serverless::Function'
Properties:
FunctionName: lambda1
Handler: index.handler
Runtime: nodejs12.x
CodeUri: .
Description: ''
MemorySize: 256
Timeout: 300
Role: 'arn:aws:iam::11111111:role/Role1'
Tracing: Active
transformedcurNodeModulesLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: layer-lambda1
Description: layers with log4js library
ContentUri: ./layers/nodejs/
CompatibleRuntimes:
- nodejs12.x
LicenseInfo: 'Available under the MIT-0 license.'
RetentionPolicy: Retain
Now in my layers folder consist all the node js packages
Folder structure.
layers ----> nodejs ---> package.json & node_modules --> log4j ....
Now After run code pipeline it got success but saw that there no layers created but sam template deployed successfully even my new codes as well.
I see based on the code above, Layers option is not set in the function resource
Resources:
awstransformedcurlambdafn:
Type: 'AWS::Serverless::Function'
Properties:
FunctionName: lambda1
Handler: index.handler
Runtime: nodejs12.x
CodeUri: .
Description: ''
MemorySize: 256
Timeout: 300
Role: 'arn:aws:iam::11111111:role/Role1'
Tracing: Active
Layers:
- !Ref transformedcurNodeModulesLayer
transformedcurNodeModulesLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: layer-lambda1
Description: layers with log4js library
ContentUri: ./layers/
CompatibleRuntimes:
- nodejs12.x
LicenseInfo: 'Available under the MIT-0 license.'
RetentionPolicy: Retain
Can you please try adding above property in your "awstransformedcurlambdafn" function resource?

serverless step function state machine not created on sls deploy

I am trying to setup a step function in my serverless application but when I deploy the application to aws the state machine is not created for the step functions. Here is my lambda file. I have no idea what I am doing wrong on this but there must be something in my setup that is causing the state machine creation to fail.
service: help-please
provider:
name: aws
versionFunctions: false
runtime: nodejs12.x
vpc:
securityGroupIds:
- sg
subnetIds:
- subnet
- subnet
stage: dev
region: us-west-2
iamRoleStatements:
- Effect: 'Allow'
Action:
- 'xray:PutTraceSegments'
- 'xray:PutTelemetryRecords'
- 'sns:*'
- 'states:*'
Resource: '*'
functions:
upsertNewCustomerRecord:
handler: .build/handler.upsertNewCustomerRecord
iamRoleStatements:
- Effect: 'Allow'
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- logs:DescribeLogGroups
- ec2:CreateNetworkInterface
- ec2:DescribeNetworkInterfaces
- ec2:DeleteNetworkInterface
- cognito-idp:AdminInitiateAuth
- ccognito-idp:DescribeUserPool
- cognito-idp:DescribeUserPoolClient
- cognito-idp:ListUserPoolClients
- cognito-idp:ListUserPools
- 'xray:PutTraceSegments'
- 'xray:PutTelemetryRecords'
Resource: '*'
sendNewCustomerEmail:
handler: .build/handler.sendNewCustomerEmail
iamRoleStatements:
- Effect: 'Allow'
Action:
- logs:DescribeLogGroups
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- ec2:CreateNetworkInterface
- ec2:DescribeNetworkInterfaces
- ec2:DeleteNetworkInterface
- cognito-idp:AdminInitiateAuth
- ccognito-idp:DescribeUserPool
- cognito-idp:DescribeUserPoolClient
- cognito-idp:ListUserPoolClients
- cognito-idp:ListUserPools
- 'xray:PutTraceSegments'
- 'xray:PutTelemetryRecords'
Resource: '*'
upsertCognitoUser:
handler: .build/handler.upsertCognitoUser
iamRoleStatements:
- Effect: 'Allow'
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- logs:DescribeLogGroups
- ec2:CreateNetworkInterface
- ec2:DescribeNetworkInterfaces
- ec2:DeleteNetworkInterface
- cognito-idp:AdminInitiateAuth
- ccognito-idp:DescribeUserPool
- cognito-idp:DescribeUserPoolClient
- cognito-idp:ListUserPoolClients
- cognito-idp:ListUserPools
- 'xray:PutTraceSegments'
- 'xray:PutTelemetryRecords'
Resource: '*'
stepFunctions:
stateMachines:
signupstepfunc:
definition:
Comment: 'Sign up step function'
StartAt: UpsertNewCustomerRecord
States:
UpsertNewCustomerRecord:
Type: Task
Resource: 'arn'
Next: SendNewCustomerEmail
SendNewCustomerEmail:
Type: Task
Resource: 'arn'
Next: UpsertCognitoUser
UpsertCognitoUser:
Type: Task
Resource: 'arn'
End: true
plugins:
- serverless-plugin-typescript
- serverless-offline
- serverless-iam-roles-per-function
- serverless-plugin-tracing
- serverless-step-functions
- serverless-pseudo-parameters
- serverless-prune-plugin
Going by the yaml file provided, I think the issue might be the indentation on your function definition:
functions:
upsertNewCustomerRecord:
handler: .build/handler.upsertNewCustomerRecord
iamRoleStatements:
Must be replaced by this:
functions:
upsertNewCustomerRecord:
handler: .build/handler.upsertNewCustomerRecord
iamRoleStatements:
Can you make this change and try once?
Well unfortunately I figured out that I fat fingered a back slash in my buildspec file and that's what caused this to occur be wary of doing this because when deploying using serverless your build won't fail you'll have to dig into every file just to figure out what's going on. Just something to keep in mind.
In my case, i simply forgot to put End: true at the last stage of the step functions. Maybe this will help.

Environment Variables with Serverless and AWS Lambda

I am learning serverless framework and I'm making a simple login system.
Here is my serverless.yml file
service: lms-auth
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: ap-south-1
environment:
MONGODB_URI: $(file(../env.yml):MONOGDB_URI)
JWT_SECRET: $(file(../env.yml):JWT_SECRET)
functions:
register:
handler: handler.register
events:
- http:
path: auth/register/
method: post
cors: true
login:
handler: handler.login
events:
- http:
path: auth/login/
method: post
cors: true
plugins:
- serverless-offline
As you can see, I have two environment variables and both of them are referencing to a different file in the same root folder.
Here is that env.yml file
MONOGDB_URI: <MY_MONGO_DB_URI>
JWT_SECRET: LmS_JWt_secREt_auth_PasSWoRds
When I do sls deploy, I see that both the variables are logging as null. The environment variables aren't sent to lambda.
How can I fix this?
Also, currently I'm using this method and adding the env.yml to .gitignore and saving the values. Is there any other efficient way of hiding sensitive data?
I would do something like this to help you out with the syntax
service: lms-auth
custom: ${file(env.yml)}
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: ap-south-1
environment:
MONGODB_URI: ${self:custom.mongodb_uri}
JWT_SECRET: ${self:custom.jwt_secret}
functions:
register:
handler: handler.register
events:
- http:
path: auth/register/
method: post
cors: true
login:
handler: handler.login
events:
- http:
path: auth/login/
method: post
cors: true
plugins:
- serverless-offline
Then in your env.yml you can do
mongodb_uri: MY_MONGO_DB_URI
jwt_secret: LmS_JWt_secREt_auth_PasSWoRds
Enviroment variables
1. Add useDotenv:true to your .yml file 2.Add your variables like this -> ${env:VARIABLE_NAME}3.Create a file called .env.dev and write the variables (You can add .env.prod but you have to change the stage inside your .yml file ) Example :
service: lms-auth
useDotenv: true
provider:
name: aws
runtime: nodejs12.x
stage: dev
region: us-east-1
environment:
MONGODB_URI: ${env:MONOGDB_URI}
JWT_SECRET: ${env:JWT_SECRET}
functions:
register:
handler: handler.register
events:
- http:
path: auth/register/
method: post
cors: true
login:
handler: handler.login
events:
- http:
path: auth/login/
method: post
cors: true
plugins:
- serverless-offline
.env.dev
MONOGDB_URI = The URI Value
JWT_SECRET = The JWT Scret Value
I ended up solving it. I had set up my Dynamo DB in AWS us-west region. reinitialized in US-East-2, and reset the region under 'provider' within the .yml file.

Resources