├── src └── put_item_function.js └── README.md /src/put_item_function.js: -------------------------------------------------------------------------------- 1 | var AWS = require('aws-sdk'); 2 | const dynamodb = new AWS.DynamoDB.DocumentClient(); 3 | 4 | exports.handler = async (event) => { 5 | 6 | let responseBody = "" 7 | let statusCode = 0 8 | 9 | let {id, price} = JSON.parse(event.body); 10 | 11 | const params = { 12 | TableName : 'Items', 13 | /* Item properties will depend on your application concerns */ 14 | Item: { 15 | id: id, 16 | price: price 17 | } 18 | } 19 | 20 | try { 21 | 22 | await dynamodb.put(params).promise(); 23 | statusCode = 200; 24 | responseBody = JSON.stringify('Item inserido com sucesso!'); 25 | 26 | } catch (err) { 27 | 28 | statusCode = 200; 29 | responseBody = JSON.stringify(err); 30 | 31 | } 32 | 33 | const response = { 34 | statusCode: statusCode, 35 | body: responseBody, 36 | }; 37 | 38 | return response; 39 | }; 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Roteiro para o desenvolvimento da atividade prática do DIO Live Coding do dia 17/11/2021 2 | 3 | ## Serviços AWS utilizados 4 | 5 | - Amazon Cognito 6 | - Amazon DynamoDB 7 | - Amazon API Gateway 8 | - AWS Lambda 9 | 10 | ## Etapas do desenvolvimento 11 | 12 | ### Criando uma API REST no Amazon API Gateway 13 | 14 | - API Gateway Dashboard -> Create API -> REST API -> Build 15 | - Protocol - REST -> Create new API -> API name [dio_live_api] -> Endpoint Type - Regional -> Create API 16 | - Resources -> Actions -> Create Resource -> Resource Name [Items] -> Create Resource 17 | 18 | ### No Amazon DynamoDB 19 | 20 | - DynamoDB Dashboard -> Tables -> Create table -> Table name [Items] -> Partition key [id] -> Create table 21 | 22 | ### No AWS Lambda 23 | 24 | #### Função para inserir item 25 | 26 | - Lambda Dashboard -> Create function -> Name [put_item_function] -> Create function 27 | - Inserir código da função ```put_item_function.js``` disponível na pasta ```/src``` -> Deploy 28 | - Configuration -> Execution role -> Abrir a Role no console do IAM 29 | - IAM -> Roles -> Role criada no passo anterior -> Permissions -> Add inline policy 30 | - Service - DynamoDB -> Manual actions -> add actions -> putItem 31 | - Resources -> Add arn -> Selecionar o arn da tabela criada no DynamoDB -> Add 32 | - Review policy -> Name [lambda_dynamodb_putItem_policy] -> Create policy 33 | 34 | ### Integrando o API Gateway com o Lambda backend 35 | 36 | - API Gateway Dashboard -> Selecionar a API criada -> Resources -> Selecionar o resource criado -> Action -> Create method - POST 37 | - Integration type -> Lambda function -> Use Lambda Proxy Integration -> Lambda function -> Selecionar a função Lambda criada -> Save 38 | - Actions -> Deploy API -> Deployment Stage -> New Stage [dev] -> Deploy 39 | 40 | ### No POSTMAN 41 | 42 | - Add Request -> Method POST -> Copiar o endpoint gerado no API Gateway 43 | - Body -> Raw -> JSON -> Adicionar o seguinte body 44 | ``` 45 | { 46 | "id": "003", 47 | "price": 600 48 | } 49 | ``` 50 | - Send 51 | 52 | ### No Amazon Cognito 53 | 54 | - Cognito Dashboard -> Manage User Pools -> Create a User Pool -> Pool name [TestPool] 55 | - How do you want your end users to sign in? - Email address or phone number -> Next Step 56 | - What password strength do you want to require? 57 | - Do you want to enable Multi-Factor Authentication (MFA)? Off -> Next Step 58 | - Do you want to customize your email verification messages? -> Verification type - Link -> Next Step 59 | - Which app clients will have access to this user pool? -> App client name [TestClient] -> Create App Client -> Next Step 60 | - Create Pool 61 | 62 | - App integration -> App client settings -> Enabled Identity Providers - Cognito User Pool 63 | - Callback URL(s) [https://example.com/logout] 64 | - OAuth 2.0 -> Allowed OAuth Flows - Authorization code grant -Implicit grant 65 | - Allowed OAuth Scopes - email - openid 66 | - Save Changes 67 | 68 | - Domain name -> Domain prefix [diolive] -> Save 69 | 70 | ### Criando um autorizador do Amazon Cognito para uma API REST no Amazon API Gateway 71 | 72 | - API Gateway Dashboard -> Selecionar a API criada -> Authorizers -> Create New Authorizer 73 | - Name [CognitoAuth] -> Type - Cognito -> Cognito User Pool [pool criada anteriormente] -> Token Source [Authorization] 74 | 75 | - Resources -> selecionar o resource criado -> selecionar o método criado -> Method Request -> Authorization - Selecionar o autorizador criado 76 | 77 | ### No POSTMAN 78 | 79 | - Add request -> Authorization 80 | - Type - OAuth 2.0 81 | - Callback URL [https://example.com/logout] 82 | - Auth URL [https://diolive.auth.sa-east-1.amazoncognito.com/login] 83 | - Client ID - obter o Client ID do Cognito em App clients 84 | - Scope [email - openid] 85 | - Client Authentication [Send client credentials in body] 86 | - Get New Acces Token 87 | - Copiar o token gerado 88 | 89 | - Selecionar a request para inserir item criada -> Authorization -> Type - Bearer Token -> Inserir o token copiado 90 | - Send 91 | --------------------------------------------------------------------------------