Serverless AWS Lambda project running on deploy - node.js

I have built a simple Node.JS application that consists of a single API endpoint. Deployment works and I can all the API endpoint as expected. However the handler function also runs on deployment of the code, which is problematic for me as handler function posts a tweet to twitter, and I don't want it to tweet every time I deploy a code update. I haven't been able to find anyone online reporting a similar problem, but I'm sure this would not be expected functionality.
This is my serverless.yml file (I created originally from the GitLab Node.JS serverless template here https://gitlab.com/gitlab-org/project-templates/serverless-framework/):
service: my-project
provider:
name: aws
region: ${env:AWS_REGION}
runtime: nodejs10.x
plugins:
- serverless-offline
- serverless-jest-plugin
- serverless-stack-output # Allows us to output endpoint url to json file
functions:
post:
handler: main.main
events:
- http:
path: post
method: post
custom:
output:
handler: main.main
file: stack.json

I believe the problem in your case is the fact that the handler you have for serverless-stack-output plugin is the same as for your function. That plugin explicitly calls the handler (https://github.com/sbstjn/serverless-stack-output#handler) which executes your function. If you will drop use of the plugin or just configure a different handler for it the problem should disappear

Related

How do you create a new version of an API using serverless framework?

I have a simple API developed using serverless framework that is deployed in production. The serverless.yaml is similar to this one:
service: example
provider:
name: aws
plugins:
- serverless-plugin-scripts
- serverless-offline
functions:
package:
runtime: nodejs14.x
handler: build/index.handler
events:
- httpApi: "POST /test"
The API will change in the next version and I want to offer backward compatibility to my clients. I want to create a /v1/test route in API Gateway that will point to the new implementation of the function and I want /test to remain the same.
Is there a way to do this using serverless framework?
There are a few things you can do.
Create a new function entirely, with a new route. This option is simplest to implement, but your setup may not allow you to create a new function for some reason (CloudFormation stack limits, or other non-functional reasons).
functions:
# V0 package function
package:
runtime: nodejs14.x
handler: build/index.handler
events:
- httpApi: "POST /test"
# V1 package function
packageV1:
runtime: nodejs14.x
handler: build/index.handlerv1
events:
- httpApi: "POST v1/test"
Use the same function, but append a new path. Then inside your function code inspect the event payload to determine which path was called and use that to modify the response or functionality to adhere to either API spec.
package:
runtime: nodejs14.x
handler: build/index.handler # Both routes share the function, you'll need to modify logic to implement both v0/v1 spec
events:
- httpApi: "POST /test" # V0 route
- httpApi: "POST v1/test" # V1 route
Both of these are good for temporary migrations where you'll eventually deprecate the old API. If you need both in perpetuity, you could also migrate your v0 API into a new stack (or similarly create a new stack for the v1 API).
Lambda is priced per-invocation, not per-function. So with that in mind, I'd suggest creating a totally distinct function, that will make it easier to deprecate and delete when the time comes.

Moving existing lambda edge to Serverless Framework

I have a Lambda Edge attached to a CloudFront distribution. What I want to do is use Serverless Framework to publish the lambda (instead of manually uploading files and click on "Deploy to Lambda#Edge"). What I've tried to do, looking at the serverless documentation, is add this yml file to the project and run the deployment script
service: cloudfront-service
provider:
name: aws
runtime: nodejs10.x
functions:
cfLambda:
handler: index.handler
events:
- cloudFront:
eventType: origin-request
origin: <CloudFront-Origin-ID>
This deployed the Lambda but it didn't attached it to CloudFront (it hasn't been published and there is no versions or triggers related). So how can I do this, using an existing CloudFront distribution?
This plugin #silvermine/serverless-plugin-cloudfront-lambda-edge will not help if you want to use an existing cloud front distribution. It is only helpful if you are going to create a new one.
This issue has been already reported and as per the forum, this functionality they are not supporting.
Lambda#Edge with Serverless-Framework is quite easy. We use this plugin.
plugins:
- '#silvermine/serverless-plugin-cloudfront-lambda-edge'
Please go directly to the plugin author's website for complete examples: https://github.com/silvermine/serverless-plugin-cloudfront-lambda-edge
Base on your implementation you have a wrong indentation so I think it wont really attach it to your cloudfront. Having a wrong indetation will not create an events on your lambda function so intead of this
events:
- cloudFront:
eventType: origin-request
origin: <CloudFront-Origin-ID>
Do this:
events:
- cloudFront:
eventType: origin-request
origin: <CloudFront-Origin-ID>
I hope that this will solve your problem. Because I encounter this wrong indentation myself and wander why it is not being implemented properly.

Serverless Framework, Google Cloud Functions & Firestore Event Triggers

Is it possible to trigger a Google Cloud Function from a Firestore Event with the Serverless Framework?
I’m using Google Cloud Functions + Serverless Framework and I am trying to figure out if the Serverless Framework supports firestore-events.
I want to use Cloud Firestore triggers, but unsure if it’s support, and if it is, then how I correctly specify the event in the serverless.yml file?
An example of a function that should trigger event when any changes to a document happens. From here: https://firebase.google.com/docs/functions/firestore-events
Note: I am importing functions from firebase in a separate file and then importing it in my index.js.
exports.firestoreEvents = functions.firestore
.document(‘users/marie’).onWrite((change, context) => {
// ... Your code here
});
If supported, how do I configure it in serverless.yml?
firestoreEvents:
handler: firestoreEvents
events:
- event:
????
For triggering a function upon a Firestore document change you should write something like this in your serverless.yml file:
myFunction:
handler: myFunction
events:
- event:
eventType: providers/cloud.firestore/eventTypes/document.update
resource: projects/<project-id>/databases/(default)/documents/<path-to-document>
I have written an article on how to use Serverless Framework with Firebase triggers: https://medium.com/ponce-agtech/using-firebase-triggers-in-serverless-framework-ad99594b86fa
Hope it helps ;)

Why am I unable to set Amazon S3 as a trigger for my Serverless Lambda Function?

I am attempting to set a NodeJS Lambda function to be triggered when an image is uploaded to an Amazon S3 bucket. I have seen multiple tutorials and have the yml file set up as shown. Below is the YML config file:
functions:
image-read:
handler: handler.imageRead
events:
- s3:
bucket: <bucket-name-here>
event: s3:ObjectCreated:*
Is there something I am missing for the configuration? Is there something I need to do in an IAM role to set this up properly?
The YAML that you have here looks good but there may be some other problems.
Just to get you started:
are you deploying the function using the right credentials? (I've seen it many times that people are deploying in some other account etc. than they think - verify in the web console that it's there)
can you invoke the function in some other way? (from the serverless command line, using http trigger etc.)
do you see anything in the logs of that function? (add console.log statements to see if anything is being run)
do you see the trigger installed in the web console?
can you add trigger manually on the web console?
Try to add a simple function that would only print some logs when it is run and try to add a trigger for that function manually. If it works then try to do the same with the serverless command line but start with a simple function with just one log statement and if it works then go from there.
See also this post for more hints - S3 trigger is not registered after deployment:
https://forum.serverless.com/t/s3-trigger-is-not-registered-after-deployment/1858

Structure of a serverless application

I am new to serverless application. I followed the aws tutorial to build a simple nodejs serverless app with codestar and lambda.
However, imagine this node app does multiple things. In consequence, it has mutiple functions inside index.js, one for functionnality A, one for functionnality B, etc (for example).
Do I have to attach multiple lambda expressions, one for each functionality, to this codestar project?
Question: Do I have to attach multiple lambda expressions, one for each functionality, to this codestar project?
Answer: Yes
AWS CodeStar Project Details:
AWS Code star project contains below file structure(reference link):
README.md - this file
buildspec.yml - this file is used by AWS CodeBuild to package your service for deployment to AWS Lambda
app.js - this file contains the sample Node.js code for the web service
index.js - this file contains the AWS Lambda handler code
template.yml - this file contains the Serverless Application Model (SAM) used by AWS Cloudformation to deploy your service to AWS Lambda and Amazon API Gateway.
Assume you have the template.yml file like below:
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
- AWS::CodeStar
Resources:
HelloWorld:
Type: AWS::Serverless::Function
Properties:
Handler: index.first_handler
Runtime: nodejs4.3
Role:
Fn::ImportValue:
!Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
Events:
GetEvent:
Type: Api
Properties:
Path: /first
Method: get
HelloWorld2:
Type: AWS::Serverless::Function
Properties:
Handler: index.second_handler
Runtime: nodejs4.3
Role:
Fn::ImportValue:
!Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
Events:
GetEvent:
Type: Api
Properties:
Path: /second
Method: get
Notice that, in above tamplate.yml file specified "HelloWorld" and "HelloWorld2" configurations.
HelloWorld configuration contains "Handler" value as "index.first_handler" meaning that "index" is the filename of index.js and first_handler is the method in index.js file.
Likewise, HelloWorld2 configuration contains "Handler" value as "index.second_handler" meaning that "index" is the filename of index.js and second_handler is the method in index.js file.
Conclusion:
You can specify any number of lambda functions in your index.js (whatever.js) file. Only you need to specify the proper Handler to identify the app your lambda function.
Hope this is the answer to your question. Feel free to ask doubts, if you have!
you don't need multiple handler functions (index.js) and you cannot have multiple handler functions. If the different functionality is doing the logically separated job then you can add multiple JS files and write functions there but you should refer that to your handler function (index.js). Alternatively, you can write functionality in index.js itself but better idea and clean code is to separate logically different functionality to another file and refer it

Resources