Created
April 2, 2019 19:22
-
-
Save sdymj84/940a983c86e2d2aa1e92401128789bff to your computer and use it in GitHub Desktop.
Serverless framework global secondary index example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
service: apt-api | |
# Use the serverless-webpack plugin to transpile ES6 | |
plugins: | |
- serverless-webpack | |
- serverless-offline | |
custom: | |
# Our stage is based on what is passed in when running serverless | |
# commands. Or fallsback to what we have set in the provider section. | |
stage: ${opt:stage, self:provider.stage} | |
# Set the table name here so we can use it while testing locally | |
residentsTable: ${self:custom.stage}-residents | |
apartsTable: ${self:custom.stage}-aparts | |
maintanancesTable: ${self:custom.stage}-maintanances | |
# Set our DynamoDB throughput for prod and all other non-prod stages. | |
tableThroughputs: | |
prod: 5 | |
default: 1 | |
tableThroughput: ${self:custom.tableThroughputs.${self:custom.stage}, self:custom.tableThroughputs.default} | |
# Load our webpack config | |
webpack: | |
webpackConfig: ./webpack.config.js | |
includeModules: true | |
# Load our secret environment variables based on the current stage. | |
# Fallback to default if it is not in prod. | |
environment: ${file(env.yml):${self:custom.stage}, file(env.yml):default} | |
provider: | |
name: aws | |
runtime: nodejs8.10 | |
stage: dev | |
region: us-east-2 | |
# These environment variables are made available to our functions | |
# under process.env. | |
environment: | |
residentsTable: ${self:custom.residentsTable} | |
apartsTable: ${self:custom.apartsTable} | |
maintanancesTable: ${self:custom.maintanancesTable} | |
stripeSecretKey: ${self:custom.environment.stripeSecretKey} | |
region: ${self:custom.environment.region} | |
userPoolClientId: ${self:custom.environment.userPoolClientId} | |
userPoolId: ${self:custom.environment.userPoolId} | |
identityPoolId: ${self:custom.environment.identityPoolId} | |
iamRoleStatements: | |
- Effect: Allow | |
Action: | |
- dynamodb:DescribeTable | |
- dynamodb:Query | |
- dynamodb:Scan | |
- dynamodb:GetItem | |
- dynamodb:PutItem | |
- dynamodb:UpdateItem | |
- dynamodb:DeleteItem | |
# Restrict our IAM role permissions to | |
# the specific table for the stage | |
Resource: | |
- "Fn::GetAtt": [ ResidentsTable, Arn ] | |
- "Fn::GetAtt": [ ApartsTable, Arn ] | |
- "Fn::GetAtt": [ MaintanancesTable, Arn ] | |
- "Fn::Join": ['/', ["Fn::GetAtt": [ ResidentsTable, Arn ], 'index', 'emailIndex']] | |
functions: | |
# Residents APIs | |
createResident: | |
handler: functions/residents/create.resident | |
events: | |
- http: | |
path: residents | |
method: post | |
cors: true | |
authorizer: aws_iam | |
getResident: | |
handler: functions/residents/get.resident | |
events: | |
- http: | |
path: residents/{id} | |
method: get | |
cors: true | |
authorizer: aws_iam | |
getResidentByEmail: | |
handler: functions/residents/get.residentByEmail | |
events: | |
- http: | |
path: residents/email/{email} | |
method: get | |
cors: true | |
authorizer: aws_iam | |
updateResident: | |
handler: functions/residents/update.resident | |
events: | |
- http: | |
path: residents/{id} | |
method: put | |
cors: true | |
authorizer: aws_iam | |
deleteResident: | |
handler: functions/residents/delete.resident | |
events: | |
- http: | |
path: residents/{id} | |
method: delete | |
cors: true | |
authorizer: aws_iam | |
billing: | |
handler: functions/billing.main | |
events: | |
- http: | |
path: billing | |
method: post | |
cors: true | |
authorizer: aws_iam | |
customMessage: | |
handler: functions/residents/customMessage.main | |
events: | |
- http: | |
path: customMessage | |
method: get | |
cors: true | |
authorizer: aws_iam | |
# Aparts APIs | |
createApart: | |
handler: functions/aparts/create.apart | |
events: | |
- http: | |
path: aparts | |
method: post | |
cors: true | |
authorizer: aws_iam | |
getApart: | |
handler: functions/aparts/get.apart | |
events: | |
- http: | |
path: aparts/{aid} | |
method: get | |
cors: true | |
authorizer: aws_iam | |
listAparts: | |
handler: functions/aparts/list.apart | |
events: | |
- http: | |
path: aparts/list/{aid} | |
method: get | |
cors: true | |
authorizer: aws_iam | |
listApartsWithFilter: | |
handler: functions/aparts/listWithFilter.apart | |
events: | |
- http: | |
path: aparts/list | |
method: get | |
cors: true | |
authorizer: aws_iam | |
updateApart: | |
handler: functions/aparts/update.apart | |
events: | |
- http: | |
path: aparts/{aid} | |
method: put | |
cors: true | |
authorizer: aws_iam | |
updateAnnouncement: | |
handler: functions/aparts/updateAnnouncement.apart | |
events: | |
- http: | |
path: aparts/{aid} | |
method: put | |
cors: true | |
authorizer: aws_iam | |
addResidentInApart: | |
handler: functions/aparts/update.addResident | |
events: | |
- http: | |
path: aparts/{aid}/add | |
method: put | |
cors: true | |
authorizer: aws_iam | |
removeResidentInApart: | |
handler: functions/aparts/update.removeResident | |
events: | |
- http: | |
path: aparts/{aid}/remove/{rid} | |
method: put | |
cors: true | |
authorizer: aws_iam | |
# Maintanances APIs | |
createRequest: | |
handler: functions/maintanances/create.request | |
events: | |
- http: | |
path: requests | |
method: post | |
cors: true | |
authorizer: aws_iam | |
# Create our resources with separate CloudFormation templates | |
resources: | |
# API Gateway Errors | |
- ${file(resources/api-gateway-errors.yml)} | |
# DynamoDB | |
- ${file(resources/dynamodb-table.yml)} | |
# S3 | |
- ${file(resources/s3-bucket.yml)} | |
# Cognito | |
- ${file(resources/cognito-user-pool.yml)} | |
- ${file(resources/cognito-identity-pool.yml)} | |
================================================================== | |
${file(resources/dynamodb-table.yml)} | |
================================================================== | |
Resources: | |
ResidentsTable: | |
Type: AWS::DynamoDB::Table | |
Properties: | |
TableName: ${self:custom.residentsTable} | |
AttributeDefinitions: | |
- AttributeName: residentId | |
AttributeType: S | |
- AttributeName: email | |
AttributeType: S | |
KeySchema: | |
- AttributeName: residentId | |
KeyType: HASH | |
# Set the capacity based on the stage | |
ProvisionedThroughput: | |
ReadCapacityUnits: ${self:custom.tableThroughput} | |
WriteCapacityUnits: ${self:custom.tableThroughput} | |
GlobalSecondaryIndexes: | |
- IndexName: emailIndex | |
KeySchema: | |
- AttributeName: email | |
KeyType: HASH | |
Projection: | |
ProjectionType: ALL | |
ProvisionedThroughput: | |
ReadCapacityUnits: ${self:custom.tableThroughput} | |
WriteCapacityUnits: ${self:custom.tableThroughput} | |
ApartsTable: | |
Type: AWS::DynamoDB::Table | |
Properties: | |
TableName: ${self:custom.apartsTable} | |
AttributeDefinitions: | |
- AttributeName: pk | |
AttributeType: S | |
- AttributeName: apartId | |
AttributeType: S | |
KeySchema: | |
- AttributeName: pk | |
KeyType: HASH | |
- AttributeName: apartId | |
KeyType: RANGE | |
# Set the capacity based on the stage | |
ProvisionedThroughput: | |
ReadCapacityUnits: ${self:custom.tableThroughput} | |
WriteCapacityUnits: ${self:custom.tableThroughput} | |
MaintanancesTable: | |
Type: AWS::DynamoDB::Table | |
Properties: | |
TableName: ${self:custom.maintanancesTable} | |
AttributeDefinitions: | |
- AttributeName: requestId | |
AttributeType: S | |
- AttributeName: apartId | |
AttributeType: S | |
KeySchema: | |
- AttributeName: requestId | |
KeyType: HASH | |
- AttributeName: apartId | |
KeyType: RANGE | |
# Set the capacity based on the stage | |
ProvisionedThroughput: | |
ReadCapacityUnits: ${self:custom.tableThroughput} | |
WriteCapacityUnits: ${self:custom.tableThroughput} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment