AWS Function
If you are using AWS as a service provider then all function that you are using on AWS platform is known as AWS lambda function.
Serverless
Configuration
All of the lambda functions that we have created can be found in serverless.yml file under functions property.
How to generate serverless file for our Application?
Getting started with Serverless Framework and AWS
Open your Terminal and choose a specific folder
Execute the following command
# Install the serverless cli
npm install -g serverless
# Or, update the serverless cli from a previous version
npm update -g serverless
After that severless-cli has been successfully installed on your system.we can create our project using following command
# Create and deploy a new service/project
serverless
Note: Before going for serverless framework make sure your system should have Node 6 or higher on Your machine, if not then first do that.
After that during the process, it will ask a few questions just follow them
- It will ask you log in on serverless or if you no account then create account
- Ask you to give permission for connecting with AWS
- Then after on AWS account, it will ask to your account user Iamrole assign it and then after it will generate Access key ID and Secret access key
- Your terminal will ask for that Access key ID and Secret access key just enter this information and after that, your project has been successfully created.
After that our Serverless has been successfully created now we have to follow steps for creating our Lambda function and deploy on AWS using serverless framework.
Configuration
service: serverlessex
# app and org for use with dashboard.serverless.com
app: server-app
org: dheerajkumar
# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
# frameworkVersion: "=X.X.X"
provider:
name: aws
runtime: nodejs10.x
# you can also these properties
# memorySize:512
# timeout:10
# versionfunctions:false
# tracing:true
functions:
hello:
handler: handler.hello
handler: property points file and module containing the code you want to run in your function.
During creating a lambda function it will be a common need that we have to maintain folder structure so we have to provide path of lambda function inside our serverless file.
This property is declared inside functions like
permissions: Every AWS lambda function needs permission to interact with other AWS infrastructure resources within your account.
These roles are set via an AWS IAMRole.
We can set permission policy statements within this role via the property.
provider.iamRoleStatements property.
Environmental Variables: We can add an environment variable configuration to a specific function in serverless.yml by adding an environment object in the function configuration like
functions:
hello:
handler: handler.hello
# Define function environment variables here
# environment:
# variable2: value2
Note: Using an environment variable in a function applies changes to that particular function. But, if we want to apply changes for all functions in our project then we have to need to declare environment variable in provider.
provider:
name: aws
runtime: nodejs10.x
# you can also these properties
# memorySize:512
# timeout:10
# versionfunctions:false
# tracing:true
# you can define service wide environment variables here
# environment:
# variable1: value1
If an environment variable with the same key is defined in the provider level as well as function level then function level key will overwrite the provider level.