avatar
Building serverless Node.js with AWS Lambda AWS

> To manipulate AWS services from AWS Command Line Interface, we need to setup accessKeyId and secretAccessKey. Thus, follow up the step by step to create IAM user and provide permissions for that user in your AWS account. Open command prompt(CMD) and run amplify configure.

PS C:\Users\admin\Documents\aws> amplify configure
Follow these steps to set up access to your AWS account:

Sign in to your AWS administrator account:
https://console.aws.amazon.com/
Press Enter to continue

Specify the AWS Region
? region:  ap-southeast-1
Specify the username of the new IAM user:
? user name:  FlagtickUser

> Trigger to open UI in AWS console and to create User step by step. Choose Next permissions, add AdministratorAccess. That is IAM User and you can access link here to get more information.

? user name:  FlagtickUser
Complete the user creation using the AWS console
https://console.aws.amazon.com/iam/home?region=ap-southeast-1#/users$new?step=final&accessKey&userNames=FlagtickUser&permissionType=policies&policies=
arn:aws:iam::aws:policy%2FAdministratorAccess-Amplify
Press Enter to continue

Enter the access key of the newly created user:
? accessKeyId:  ********************
? secretAccessKey:  ****************************************
This would update/create the AWS Profile in your local machine
? Profile Name:  Flagtick
Successfully set up the new user.

> Use access key ID and secret access key that you observe in IAM user for configuration in AWS CLI. Go to link https://flagtick.com/snippet/aws/aws-configure-environment-variable

aws configure
AWS Access Key ID [None]: AKIASULOI7ICH4XXWKV7
AWS Secret Access Key [None]: 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
Default region name [None]: ap-southeast-1
Default output format [None]: json

> Move to aws folder and run the following command to generate Node.js application.

C:\Users\admin\Documents\aws>
serverless create --template aws-nodejs --path flagtick-service --name Flagtick

> Getting started with Serverless framework by performing the following command to download Serverless cli into your local machine. That is perfect way to build up application by AWS Lambda.

serverless -v
Framework Core: 3.33.0
Plugin: 6.2.3
SDK: 4.3.2

Node: --path is path of the project, --name was defined as Service name stored in serverless.yml. Running code . to open Visual Studio Code.

> Update file serverless.yml as below:

# Welcome to Serverless!
service: Flagtick

frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs14.x
  memorySize: 512
  timeout: 10
  versionFunctions: false
  stage: dev 
  region: ap-southeast-1
  tracing:
    lambda: true

functions:
  hello:
    handler: handler.index

> Run the serverless deploy in Command Prompt (CMD) as below

serverless deploy

Deploying Flagtick to stage dev (ap-southeast-1)

✔ Service deployed to stack Flagtick-dev (33s)

functions:
  hello: Flagtick-dev-hello (392 B)

Need a better logging experience than CloudWatch? Try our Dev Mode in console: run "serverless --console"

> Go to AWS dashboard and check on region ap-southeast-1.

> We can check on AWS CLI by running aws lambda get-function --function-name Flagtick-dev-hello --region ap-southeast-1 as below:

{
    "Configuration": {
        "FunctionName": "Flagtick-dev-hello",
        "FunctionArn": "arn:aws:lambda:ap-southeast-1:181156772356:function:Flagtick-dev-hello",
        "Runtime": "nodejs14.x",
        "Role": "arn:aws:iam::181156772356:role/Flagtick-dev-ap-southeast-1-lambdaRole",
        "Handler": "handler.index",
        "CodeSize": 392,
        "Description": "",
        "Timeout": 10,
        "MemorySize": 512,
        "LastModified": "2022-12-17T01:00:28.000+0000",
        "CodeSha256": "NAneVe9zq/Dqh6W0temaNmdjWR8wAHEpX3h019uRkG0=",
        "Version": "$LATEST",
        "TracingConfig": {
            "Mode": "Active"
        },
        "RevisionId": "a76930cb-2db5-4179-af1e-5e30f42b1fb8",
        "State": "Active",
        "LastUpdateStatus": "Successful",
        "PackageType": "Zip",
        "Architectures": [
            "x86_64"
        ],
        "EphemeralStorage": {
            "Size": 512
        }
    },
    "Code": {
        "RepositoryType": "S3",
        "Location": "https://awslambda-ap-se-1-tasks.s3.ap-southeast-1.amazonaws.com=600&X-Amz-Credential=ASIAUJQ4O7LPYBOMTV4N%2F20221217%2Fap-southeast-1%2Fs3%2Faws4_request&X-Amz-Signature=8482152096a6e2c3bc8ec08e541b457386878c784d6ced98d9e272ec9d225bce"
    },
    "Tags": {
        "aws:cloudformation:stack-name": "Flagtick-dev",
        "aws:cloudformation:stack-id": "arn:aws:cloudformation:ap-southeast-1:181156772356:stack
/Flagtick-dev/a0fd3ce0-7da5-11ed-ac91-0aea1951315c",
        "STAGE": "dev",
        "aws:cloudformation:logical-id": "HelloLambdaFunction"
    }
}

> Now, we will analyze some configuration in provider. Let move each configuration by one.

versionFunctions - If true, the Versions will generate upon run serverless deploy command.

timeout - In configuration, it shown exactly timeout from 0 min to n sec. Thus, n is value of timeout in provider.

> Add custom name and description in your lambda function.

functions:
  hello:
    handler: handler.index
    name: ${sls:stage}-flagtick
    description: Demo function to get list of stages

Note: After change serverless.yml, you should run serverless deploy to get new update on AWS dashboard.

> Here is serverless.yml reference, you want to get more information relates serverless.yml and press link here and github.

Note: If you change Flagtick to flagtick (uppercase to lowercase), the IAM role have already existed and hence you need to add a new one.

# Welcome to Serverless!

service: Flagtick

frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs14.x
  memorySize: 512
  timeout: 5
  versionFunctions: false
  region: ap-southeast-1
  tracing:
    lambda: true

functions:
  getFlagtick:
    handler: handler.index
    name: getFlagtick
    description: Demo function to get list of stages
    runtime: nodejs14.x 
    memorySize: 512 
    timeout: 10 
    provisionedConcurrency: 3 
    reservedConcurrency: 5 
    tracing: PassThrough 


24
get detail of lambda function Input format of a Lambda function for proxy integration query list of user and filter cognito in lambda function create Custom Query in Lambda using SecretsManager add Layers to Lambda function
You need to login to do this manipulation!