Cannot deploy Node.js app inside AWS Lambda using Serverless Framework - node.js

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.

Related

Deployment of simple node js application to AWS Lambda Serverless Failing with GeneralServiceException

I'm trying to deploy a simple NodeJS application to aws lambda serverless using aws cli. In the deployment the process, the application throws GeneralService Exception with the following error message.
Deploying aws-demo to stage dev (us-east-1)
Warning: Not authorized to perform: lambda:GetFunction for at least one of the lambda functions. Deployment will not be skipped even if service files did not change.
× Stack aws-demo-dev failed to deploy (155s)
Environment: win32, node 14.15.5, framework 3.26.0, plugin 6.2.2, SDK 4.3.2
Credentials: Local, "default" profile
Docs: docs.serverless.com
Support: forum.serverless.com
Bugs: github.com/serverless/serverless/issues
Error:
CREATE_FAILED: HelloLambdaFunction (AWS::Lambda::Function)
Resource handler returned message: "null (Service: Lambda, Status Code: 403, Request ID: 0f60ffeb-add5-4571-856f-72a390ce5be9)" (RequestToken: beefe9c6-32d2-5d7f-f483-d97a6e76b73c, HandlerErrorCode: GeneralServiceException)
Below is the serverless.yml file configuration of the application:
service: aws-demo
frameworkVersion: '3'
provider:
name: aws
runtime: nodejs12.x
memorySize: 512
stage: dev
timeout: 15
region: us-east-1
functions:
hello:
handler: handler.hello
events:
- http: ANY /{proxy+}
- http: ANY /
Upgrade your AWS user credentials!
Overwrite if its already done, using overwrite command.

Serverless deployed environment variables do not update

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

Lambda functions does not appear on AWS Console when I deploy from VSCode with Serverless Deploy

My problem is that if I write the Lambda function in VSCode I cannot deploy it to AWS console.
I have an AWS account and provided credentials to use in VSCode. Just testing the deployment of simple Lambda function to AWS Console with serverless deploy command. So far no success. It creates the bucket on S3 and put zip code there.
ConsoleTest function was created manually in AWS Lambda Console.
My serverless.yml looks like this:
service: myservice
provider:
name: aws
runtime: nodejs12.x
functions:
hello:
handler: handler.hello
events:
- http:
path: users/create
method: get
Result in terminal (I get correctly JSON response)
I was following the official guide: https://serverless.com/framework/docs/providers/aws/guide/deploying/
Any help, please?
Found a solution.
Problem was that it created the wrong region. I also installed AWS Cli and specified region into a config file, but added region property to the provider. Not sure which one helped, because basically they do the same function.
When I put
service: myservice
provider:
name: aws
runtime: nodejs12.x
stage: development
region: eu-central-1
Everything started to work correctly and deploy to my AWS console.
Change the zone as shown in image.

Warned - no cfnRole set and unnecessary files was created after deploy

No cfnRole warned and unnecessary files was created after deploy
Serverless: Safeguards Processing...
Serverless: Safeguards Results:
Summary --------------------------------------------------
passed - no-unsafe-wildcard-iam-permissions
passed - framework-version
warned - require-cfn-role
passed - allowed-runtimes
passed - no-secret-env-vars
passed - allowed-regions
passed - allowed-stages
Details --------------------------------------------------
1) Warned - no cfnRole set
details: http://slss.io/sg-require-cfn-role
Require the cfnRole option, which specifies a particular role for CloudFormation to assume while deploying.
I had been go to the site that write in details.
details: http://slss.io/sg-require-cfn-role
Anyway, I don't know how to fix it.
s_hello.py & s_hello2.py always generated after deploy.
This is my serverless.yaml file
service: myapp
app: sample-app
org: xxx
provider:
name: aws
runtime: python3.7
stage: dev
region: us-east-1
package:
individually: true
functions:
hello:
handler: src/handler/handler.hello
hello2:
handler: src/handler2/handler2.hello2
It's always happen although follow this site .
My Lambda-function will create "s_xxx.py (Where xxx is handler.py file.
I solved this issue creating a cfn-role in AWS IAM following these steps:
Roles -> Create Role -> AWS Service -> Select Cloud Formation from the list
Next: Permisions
You need to choose all the policies you need to deploy your lambda function (S3FullAccess, SQSFullAccess, LambdaFullAccess...)
There's one that it's mandatory AWSConfigRole who allows to serverless framework to get this role.
After setting the role, you need to copy its arn and create behind provider level cfnRole like this:
provider:
name: aws
runtime: python3.7
stage: ${opt:stage, 'dev'}
profile: ${self:custom.deploy-profile.${opt:stage, 'dev'}}
region: us-west-2
environment:
TEMP: "/tmp"
cfnRole: arn:aws:iam::xxxxxxxxxx:role/cfn-Role
That's work for me, I hope to help!

lambda deployed through serverless with plugin serverless-plugin-existing-s3 doesn't trigger on S3 upload event

I am deploying serverless lambda environment and using serverless-plugin-existing-s3 plugin, all is fine but S3 event don't trigger lambda when i upload files.
Example of code :
service: test-storage
package:
individually: true
plugins:
- serverless-plugin-existing-s3
- serverless-plugin-include-dependencies
provider:
name: aws
runtime: nodejs8.10
iamRoleStatements:
- Effect: "Allow"
Action:
- "s3:PutBucketNotification"
Resource:
Fn::Join:
- ""
- - "arn:aws:s3:::TESTBUCKET"
functions:
onPimImportTrigger:
handler: testFunc/testFunc.handler
name: testFunc
description: Detect file(s) uploaded to Bucket-S3, and handle lambda
events:
- existingS3:
bucket: S3_BUCKET_NAME
events:
- s3:ObjectCreated:*
rules:
- prefix: TEST/IN
- suffix: .txt
I don't understand, i followed package documentation.
Thanks for help.
Just run the command after deploying the code
serverless s3deploy --stage yourstage

Resources