Skip to content

Latest commit

 

History

History
107 lines (73 loc) · 3.41 KB

README.md

File metadata and controls

107 lines (73 loc) · 3.41 KB

Serverless Framework Node HTTP API on AWS

This repository demonstrates how to make a simple HTTP API with Node.js running on AWS Lambda and API Gateway using the Serverless Framework.

This template does not include any kind of persistence (database). In this project we will create an AWS cloud infrastructure with API Gateway, DynamoDB, AWS Lambda and AWS CloudFormation using the Serverless framework for development based on Infrastructure as a Code.

Usage

Prerequisites:

  • have an AWS account and install Node.js on the machine.
  • Install the AWS CLI

Initial Setup

AWS Credentials

  • Create user: AWS Management Console -> IAM Dashboard -> Create New User -> "username" -> Permissions "Administrator Access" -> Programmatic Access -> Dowload Keys
  • In terminal: $ aws configure -> paste previously generated credentials

Configure the Serverless framework

$ npm i -g serverless

Deployment

$ serverless deploy

After deploying, you should see output similar to:

Deploying aws-node-http-api-project to stage dev (us-east-1)

✔ Service deployed to stack aws-node-http-api-project-dev (152s)

endpoint: GET - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/
functions:
  hello: aws-node-http-api-project-dev-hello (1.9 kB)

Note: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to http event docs.

Invocation

After successful deployment, you can call the created application via HTTP:

curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/

Which should result in response similar to the following (removed input content for brevity):

{
  "message": "Go Serverless v2.0! Your function executed successfully!",
  "input": {
    ...
  }
}

Local development

You can invoke your function locally by using the following command:

serverless invoke local --function hello

Which should result in response similar to the following:

{
  "statusCode": 200,
  "body": "{\n  \"message\": \"Go Serverless v3.0! Your function executed successfully!\",\n  \"input\": \"\"\n}"
}

Alternatively, it is also possible to emulate API Gateway and Lambda locally by using serverless-offline plugin. In order to do that, execute the following command:

serverless plugin install -n serverless-offline

It will add the serverless-offline plugin to devDependencies in package.json file as well as will add it to plugins in serverless.yml.

After installation, you can start local emulation with:

serverless offline

To learn more about the capabilities of serverless-offline, please refer to its GitHub repository.