Este proyecto contiene el código para crear una aplicación serverless para poder desplegar la infraestructura en AWS con el framework SAM (Serverless Application Model). Incluye los siguientes archivos:
- functions/handelr.js - Incluye las funciones necesarias para el CRUD (Create, Read, Update, Delete).
- template.yaml - A template that defines the application's AWS resources.
- SAM CLI - Install the SAM CLI
- Node.js - Instalar Node.js
Esta aplicación usar diferentes servicios de AWS para con seguir desplegar una API REST.
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
Cors:
AllowMethods: "'POST, GET, PUT, DELETE, OPTIONS'"
AllowHeaders: "'Content-Type'"
AllowOrigin: "'*'"
SaveItemFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: handler.saveItem
Runtime: nodejs16.x
CodeUri: ./functions
Description: Function to store task in the DynamoDB table.
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref DynamoDBTable
Environment:
Variables:
TABLE_DYNAMODB: !Ref DynamoDBTable
Los eventos que se usan para ejecutar las funciones Lambda se asocian a la API Gateway a los diferentes métodos.
Events:
Api:
Type: Api
Properties:
Path: /tasks
Method: POST
RestApiId:
!Ref ApiGatewayApi
Las políticas necesarias para poder acceder a los datos en DynamoDB desde las funciones Lambda.
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref DynamoDBTable
DynamoDBTable:
Type: AWS::Serverless::SimpleTable
Para desplegar la aplicación en AWS, se debe de ejecutar lo siguiente:
sam build
sam deploy --guided