How to know the custom domain my lambda is bound to? - node.js

I have a lambda function which has its api-gateway URL. Also, I have a custom domain URL associated with it in CloudFront (https://abc.def.com/ghi). I can access my lambda using the custom domain.
Now as a requirement I am supposed to return the custom domain URL from the lambda function as part of the JSON response.
So my response body from the lambda should look like:
{
"response": "hi i am coming from lambda",
"myurl": "https://abc.def.com/ghi"
}
How do I catch hold of the URL inside lambda function?
PS: I am using node 8.10 environment.

First, I would challenge the requirement. IMHO it does not make sense to return the domain url to the client as part of the response, since it already knows it (otherwise it would not be able to make the request in the first place).
That said, one way of solving your problem is to pass the custom domain url as a Lambda environment variable. If you have setup your application using AWS SAM / CloudFormation you pass the value as part of the Environment property, e.g. when using SAM:
ExampleLambda:
Type: AWS::Serverless::Function
Properties:
Runtime: nodejs8.10
CodeUri: .
Handler: index.handler
Environment:
Variables:
CUSTOM_DOMAIN_URL: !Sub CustomDomainUrl
Then CUSTOM_DOMAIN_URL will be available as an environmental variable in your Lambda runtime:
const CUSTOM_DOMAIN_URL = process.env.CUSTOM_DOMAIN_URL;
And then you you just add it to your response object.

Related

What do i put in the "handler" prop of aws-cdk's gateway.LambdaRestApi?

I'm building out one of my first gateway api's and have been reading the code and documentation here.
For an apigateway which use made using the LambdaRestApi function, my understanding was that i define the endpoints and the lambda attached to the endpoints.
If that's the case, what do i put as the functions handler function? I don't have any plans for there to be a base route for it so do i have to just have a blank lambda here? Or am i going in the wrong direction with my thinking?
LambdaRestApi is just a utility construct based on RestApi with defaultIntegration to a Lambda function.
You can use a plain RestApi instead if you don't need the default proxy integration to a Lambda handler configured.

Best practice to call lambda to lambda with multi tenancy?

In my Serverless web-app (nodeJS) which supports multi tenancy, I have the following architecture:
Layer of controller - each controller is a lambda function (separated repository)
Layer of service - each service is a lambda function (another separated repository) which also calling to Dynamo DB.
Currently the controller is calling the service lambda using http (development purposes only) and we want to make it better using aws-sdk with lambda.invoke() or Step functions.
In case that we will use lambda.invoke(), there is a need to have a stable ARN per each lambda function and use it over other lambda's.
My question is, how can I have an ARN per each tenant+lambda and how can I maintain it?
In other case which we will use step functions, I wanted to know if its suitable for this kind of architecture ?
You don't need to maintain the lambda ARN if you know where to look for the current one. You can use an export from a CloudFormation deck or ssm parameter or DynamoDB or anything really. Even for the step function you can redeploy with using the CloudFormation output exports and it will point to the correct ARNs.

Serverless AWS Lambda project running on deploy

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

Use Path Parameters in serverless framework

functions:
update:
handler: todos/update.update
events:
- http:
path: /update/{title}
method: put
How do I have to define the path param in order to pass it to my AWS Lambda function using serverless framework?
The language I want to use for lambda is Python3.
This is correct, you can access the parameter you defined in the serverless.yml from event['pathParameters']['title'] inside the lambda.
hope this helps

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