Giter VIP home page Giter VIP logo

dio-live-serverless's Introduction

API Node.js com Serverless Framework em ambiente AWS

Este repositório contém o código fonte do Live Coding da DIO no dia 29/07/2021. Neste projeto vamos criar uma infraestrutra em nuvem AWS com API Gateway, DynamoDB, AWS Lambda e AWS CloudFormation utilizando o framework Serverless para o desenvolvimento baseada em Infraestrutura as a Code.

Etapas

Pré requisitos:

Setup Inicial

Credenciais AWS

  • Criar usuário: AWS Management Console -> IAM Dashboard -> Create New User -> <nome do usuário> -> Permissions "Administrator Access" -> Programmatic Access -> Dowload Keys
  • No terminal: $ aws configure -> colar as credenciais geradas anteriormente

Configurar o framework Serverless

$ npm i -g serverless

Desenvolvimento do projeto

$ serverless
Login/Register: No
Update: No
Type: Node.js REST API
Name: dio-live
$ cd dio-live
$ code .
  • No arquivo serverless.yml adicionar a região region: us-east-1 dentro do escopo de provider:
  • Salvar e realizar o deploy $ serverless deploy -v

Estruturar o código

  • Criar o diretório "src" e mover o arquivo "handler.js" para dentro dele
  • Renomear o arquivo "handler.js" para "hello.js"
  • Atualizar o código
const hello = async (event) => {
/////
module.exports = {
    handler:hello
}
  • Atualizar o arquivo "serverless.yml "
handler: src/hello.handler

$ serverless deploy -v

DynamoDB

Atualizar o arquivo serverless.yml

resources:
  Resources:
    ItemTable:
      Type: AWS::DynamoDB::Table
      Properties:
          TableName: ItemTable
          BillingMode: PAY_PER_REQUEST
          AttributeDefinitions:
            - AttributeName: id
              AttributeType: S
          KeySchema:
            - AttributeName: id
              KeyType: HASH

Desenvolver funções lambda

- Pasta /src do repositório
- Obter arn da tabela no DynamoDB AWS Console -> DynamoDB -> Overview -> Amazon Resource Name (ARN)
- Atualizar arquivo serverless.yml com o código a seguir, abaixo do ```region:```
  iam:
    role:
        statements:
          - Effect: Allow
            Action:
              - dynamodb:PutItem
              - dynamodb:UpdateItem
              - dynamodb:GetItem
              - dynamodb:Scan
            Resource:
              - arn:aws:dynamodb:us-east-1:167880115321:table/ItemTable
  • Instalar dependências

npm init npm i uuid aws-sdk

  • Atualizar lista de funções no arquivo serverless.yml
functions:
hello:
  handler: src/hello.handler
  events:
    - http:
        path: /
        method: get
insertItem:
  handler: src/insertItem.handler
  events:
    - http:
        path: /item
        method: post
fetchItems:
  handler: src/fetchItems.handler
  events:
    - http:
        path: /items
        method: get
fetchItem:
  handler: src/fetchItem.handler
  events:
    - http:
        path: /items/{id}
        method: get
updateItem:
  handler: src/updateItem.handler
  events:
    - http:
        path: /items/{id}
        method: put

dio-live-serverless's People

Contributors

cassianobrexbit avatar

Watchers

 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.