Reputation: 73
I recently explored serverless and I would like to create a "kind of simple" backend for my app. It should like CRUD connected to DynamoDB like it is nicely shown here. And then later converted into logging users with Cognito. Link1 and link2 (
During my implementation I have got into a few issue and would like to ask you for help because I don't like using something I only copied and don't exactly know how it is working:
Using callbacks vs returns in lambda functions Please can you describe the difference between these two? I have found some resources on this, however, they won't clear it for me. On Amazon Docs and here Stack overflow.
Why I don't need to set CORS (headers) when initializing lambdas with serverless as opposed to lambda functions created in the amazon console (like here)?
In serverless frameworks what is the difference between functions.events.http
and functions.events.httpapi
?
Serverless.yml Is there any guide to YAML? I understand that Amazon writing user guides and templates like AWS::IAM::Role or for Dynamo Tables and all others. And also that resources make a cloudformation but what is the purpose for example for Lambda functions listing there when I create/code them? To reference them? Add them privileges?
Also how the referencing works there? Do I only specify resources that should be created or can I also reference already existing ones via ARN? If so how?
Do the policies are only set globally like documentation showing or the only other option is to use plugin serverless-iam-roles-per-function
? The same question could be applied to roles and policies in resources.
I am just overwhelmed with the documentation and all the different guides and sometimes can't find the right answers. I appreciate any answers to any of my questions! I like serverless functions and see huge potential in it and therefore want to get more knowledge about it and start using it more :)
Cheers and thanks in advance Lukas!
Upvotes: 1
Views: 469
Reputation: 5747
There's a lot here, but I'll take a shot at addressing these questions at a high level. If you have follow-up questions or want a more in-depth answer, I'd suggest posting a different StackOverflow question that focuses on the specifics. You are more likely to get more community participation if your question is focused and concise.
Using callbacks vs returns in lambda functions Please can you describe the difference between these two? I have found some resources on this, however, they won't clear it for me. On Amazon Docs and here Stack overflow.
The difference boils down to asynchronous vs synchronous handlers. I don't think I can explain the difference any better than the resources that you've linked. However, I will say that it comes down to personal preference.
You can implement your lambdas with either approach, it's up to your personal preference. Do you like managing callbacks or do you prefer to use async/await?
Why I don't need to set CORS (headers) when initializing lambdas with serverless as opposed to lambda functions created in the amazon console (like here)?
The serverless-stack demo puts the CORS response neatly into a helper method. It looks like you're using it here. When you call this method in your lambdas, you are setting the CORS headers.
In serverless frameworks what is the difference between functions.events.http and functions.events.httpapi?
functions.events.http
refers to API Gateway's REST API offering (APIG v1). functions.events.httpapi
refers to API Gateways HTTP API offering (APIG v2).
You can read about the differences between REST API and HTTP API offerings here.
Serverless.yml Is there any guide to YAML? I understand that Amazon writing user guides and templates like AWS::IAM::Role or for Dynamo Tables and all others. And also that resources make a cloudformation but what is the purpose for example for Lambda functions listing there when I create/code them? To reference them? Add them privileges?
The Serverless Framework docs have a guide to the serverless.yml options when you set the provider to aws
. The serverless.yml file is an abstraction over Cloudformation. The idea is that serverless.yml is easier to write than verbose Cloudformation. You'd define functions in your serverless.yml to create/configure your lambdas in AWS. For example, here's a snippet from your serverless.yml
functions:
login:
handler: login.login
name: my-login-function
memorySize: 128
description: Function that log user.
events:
- httpApi:
path: /login
method: post
This creates a lambda in AWS with the code you have written in the login.js file. It also sets the memory size and sets up an API Gateway HTTP API endpoint behind /login. You can check out the Cloudformation template that is created from this serverless.yml file in your .serverless
directory. It's....a lot.
Do I only specify resources that should be created or can I also reference already existing ones via ARN? If so how?
The serverless docs give an example of how to point to existing API endpoints.
apiGateway: # Optional API Gateway global config
restApiResources: # List of existing resources that were created in the REST API. This is required or the stack will be conflicted
'/users': xxxxxxxxxx
'/users/create': xxxxxxxxxx
Do the policies are only set globally like documentation showing or the only other option is to use plugin serverless-iam-roles-per-function? The same question could be applied to roles and policies in resources.
I'm not sure I understand the question. In general, I'd suggest spending some time reading What is IAM? from the serverless-stack website. It does a fantastic job of covered IAM Users/Roles/Policies/Groups/etc.
Upvotes: 1