Coder Social home page Coder Social logo

larrybotha / eggheadio-develop-a-serverless-backend-using-node-on-aws-lambda Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 9 KB

Notes and annotations for https://egghead.io/courses/develop-a-serverless-backend-using-node-js-on-aws-lambda

License: The Unlicense

JavaScript 100.00%

eggheadio-develop-a-serverless-backend-using-node-on-aws-lambda's Introduction

Eggheadio - Develop a Serverless Backend using Node.js on AWS Lambda

Notes and annotations for https://egghead.io/courses/develop-a-serverless-backend-using-node-js-on-aws-lambda

Create a User and setup the AWS CLI to interact with AWS

  1. Create an IAM user with programmatic access, and AdministratorAccess policy (for ease of testing / evaluation - not recommended for production)
  2. Install awscli: $ brew install awscli
  3. Store the user's key on your system by:
    1. Running $ aws configure
    2. Entering Access Key ID for new user
    3. Entering Secret access key for new user
    4. Used defaults for remaining options

Setup the Serverless Framework

  1. Install serverless framework: $ npm i -g serverless
  2. sls is an alias for the serverless command
  3. $ sls will show all commands available through the serverless framework
  4. You can use sls create -h to see how to bootstrap projects

Deploy a Node.js function to AWS Lambda using the Serverless Framework

  1. create serverless.yml

  2. add service property with name of your application

  3. add provider block with name of provider and runtime for your service

  4. add functions block to define your functions

  5. in functions block add a name for a function, and in that block add a handler property with handler.[my-export-name] for the function that Lambda will execture for that function block

  6. create a js file with a named export of the same name as the handler defined in the above step.

  7. lambdas take 3 parameters

    1. event - request data
    2. context - aws-specific data
    3. callback - a function that should be invoked with either an error response for the first argument, or a success response for the second argument
  8. deploy the service using sls deploy

  9. test the service works by running sls invoke --function [functionName]

  10. console.log statements can be evaluated by running the invoke command with the --log flag:

    $ sls invoke --function myFunc --log
  11. one can view past logs for a function, too:

    $ sls logs --function myFunc
  12. instead of calling the callback in the function, we can instead return a promise

  13. to redeploy a change to a specific function faster, run:

    $ sls deploy --function myFunc
  14. with Node 8 we can leverage async / await, so we can define a function as async instead of explicitly returning a promise

Attach a HTTP endpoint to an AWS Lambda function using the Serverless Framework

  1. we can configure HTTP endpoints for lambdas by defining an HTTP path and method in serverless.yml on the events property.

  2. to create a GET request at the path / we can do the following:

    functions:
      myFunction:
        handler: [file-name].[function-name]
        events:
          - http
            path: /
            method: GET
  3. lambdas can only be invoked by an AWS SDK

  4. AWS's API Gateway is such an SDK, and the serverless framework configures one under the hood

  5. when using events for lambdas, responses must be structured with a statusCode and body

  6. one has to rerun deployment when serverless.yml is updated so that the stack is updated accordingly, and because we've added an http event, the API Gateway service needs to be configured

  7. once the deployment succeeds, we will receive an endpoint where a request can be made using curl etc.

Deploy a DynamoDB table to AWS using the Serverless Framework

To store data, we need a database

  1. to add resources to your application, such as DynamoDB, add a resources block in the root Serverless.yml

  2. nest a Resources block inside that block. Cloud formation also supports keywords in addition to Resources, so the nesting is required

  3. the minimum configuration for a table includes:

    1. table name
    2. attributes for primary and secondary instances
    3. key schema with mandatory properties for the primary key of the table
    4. throughput capacity for read and write access
  4. run $ sls deploy to have the serverless framework configure the application

  5. you can see the instance in your AWS console

Deploy an AWS Lambda function to store data in DynamoDB using the Serverless Framework

By default, lambdas are not allowed to interact with databases. In order to allow access, we need to create an IAM role.

The serverless framework allows for configuration of IAM roles instead of manually creating them.

To create an IAM role for lambdas:

  1. add an iamRoleStatements property to the provider property with the following details:

    provider:
      ...
      iamRoleStatements:
        - Effect: Allow
          # we need to allow our functions to put items onto the dynamo db table
          Action:
            - dynamodb:PutItem
          # arn:partition:service:region:account-id:resourcetype/resource
          Resource: "arn:aws:dynamodb:es-east-1:12398712398:table/todos"
  2. the Resource field has the following format:

    "arn:aws:dynamodb:us-east-1:12398712398:table/todos"
     [1] [2]   [3]      [4]         [5]      [6]   [7]
    
     1 - amazon resource name
     2 - partition name
     3 - service
     4 - region
     5 - account id
     6 - resource type
     7 - resource
    

    Further details can be found here: https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html#genref-arns

  3. It's common to see configs with * for the resource - security is improved by providing explicit access to resources, rather than wildcarding them

  4. we can leverage modules from the aws-sdk package directly from our lambda files without having to install anything

  5. to create a DynamoDB client:

    const AWS = require('aws-sdk')
    const dynamoDbClient = AWS.DynamoDB.DocumentClient();
  6. to write to a DynamoDB table:

    await dynamoDbClient.put({
      TableName: [tablename],
      Item: {...itemProps}
    }).promise()

eggheadio-develop-a-serverless-backend-using-node-on-aws-lambda's People

Contributors

larrybotha avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.