Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serverless aws-nodejs starter pack with User CURD operations and cogn… #581

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ serverless install -u https://github.com/serverless/examples/tree/master/folder-
| [Graphql Api And Serverless](https://github.com/serverless/examples/tree/master/aws-node-graphql-and-rds) | nodeJS |
| [Aws Node Graphql Api With Dynamodb](https://github.com/serverless/examples/tree/master/aws-node-graphql-api-with-dynamodb) <br/> A single-module GraphQL endpoint with query and mutation functionality. | nodeJS |
| [Aws Lambda And Heroku Postgres](https://github.com/serverless/examples/tree/master/aws-node-heroku-postgres) <br/> Shows how to connect AWS Lambda to Heroku Postgres. Uses an api:release Heroku webhook and the Heroku API to handle automatic Heroku Postgres credential rotation. | nodeJS |
| [Initial Setup](https://github.com/serverless/examples/tree/master/aws-node-initial-setup-with-usersCRUDapi+cognito-setup+dummy_dynamoDB+S3) | nodeJS |
| [Aws Iot Event](https://github.com/serverless/examples/tree/master/aws-node-iot-event) <br/> Example on how to setup a AWS IoT Rule to send events to a Lambda function | nodeJS |
| [Aws Lambda And Mongodb Atlas](https://github.com/serverless/examples/tree/master/aws-node-mongodb-atlas) <br/> Shows how to connect AWS Lambda to MongoDB Atlas. | nodeJS |
| [Dropbox](https://github.com/serverless/examples/tree/master/aws-node-oauth-dropbox-api) <br/> dropbox integration | nodeJS |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Serverless AWS-NodeJs intial setup

serverless aws-nodejs initial setup with nodejs, you will get pre-made user functions for CRUD operation with cognito authorizer, Webpack configs, basic cognito setup, one dummy s3 bucket and function to upoad images on s3 and db clients for s3 and dynamodb with status codes.

- **User create, update, get, deltete, deactivate functions in DynamoDB table and to delete cognito user**
- **Cognito authorizer**
- **One Demo S3 bucket**
- **Lambda to upload images to S3**
- **DB and S3 clients**
- **Cognito client**
- **Status codes**
- **Code formatter**
- **Webpack configs**
- **development and production stages**
---

### Requirements

- [Install the Serverless Framework](https://serverless.com/framework/docs/providers/aws/guide/installation/)
- [Configure your AWS CLI](https://serverless.com/framework/docs/providers/aws/guide/credentials/)

### Installation

To create a new Serverless project.

``` bash
$ serverless install --url https://github.com/GM1957/serverless-aws-nodejs-initial-setup
```

Enter the new directory

``` bash
$ cd serverless-aws-nodejs-initial-setup
```

Install the Node.js packages

``` bash
$ npm install
```

### Usage

To run a function on your local

``` bash
$ serverless invoke local -f Users -p ./pathToYour_input.json --stage dev
```

To simulate API Gateway locally using [serverless-offline](https://github.com/dherault/serverless-offline)

``` bash
$ serverless offline --stage dev
```

Deploy your project in development stage

``` bash
$ serverless deploy --stage dev
```
Deploy your project in production stage

``` bash
$ serverless deploy --stage prod
```

Deploy a single function in development

``` bash
$ serverless deploy FunctionName --stage dev
```

#### Running code formatCheck

Run your check using [now there is only the code format checker test]

``` bash
$ npm run formatCheck
```
#### Running code formatter

format all the codes with pritter formatter

``` bash
$ npm run format
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Federated: "cognito-identity.amazonaws.com"
Action:
- "sts:AssumeRoleWithWebIdentity"
Condition:
StringEquals:
"cognito-identity.amazonaws.com:aud": !Ref CognitoIdentityPool
"ForAnyValue:StringLike":
"cognito-identity.amazonaws.com:amr": authenticated
Policies:
- PolicyName: "CognitoAuthorizedPolicy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "mobileanalytics:PutEvents"
- "cognito-sync:*"
- "cognito-identity:*"
Resource: "*"
- Effect: "Allow"
Action:
- "lambda:InvokeFunction"
Resource: "*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Type: AWS::Cognito::IdentityPool
Properties:
IdentityPoolName: ${self:provider.stage}-${self:provider.stage}
AllowUnauthenticatedIdentities: true
CognitoIdentityProviders:
- ClientId:
Ref: CognitoUserPoolClient
ProviderName:
Fn::GetAtt: [CognitoUserPool, ProviderName]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Federated: "cognito-identity.amazonaws.com"
Action:
- "sts:AssumeRoleWithWebIdentity"
Condition:
StringEquals:
"cognito-identity.amazonaws.com:aud": !Ref CognitoIdentityPool
"ForAnyValue:StringLike":
"cognito-identity.amazonaws.com:amr": unauthenticated
Policies:
- PolicyName: "CognitoUnauthorizedPolicy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "mobileanalytics:PutEvents"
- "cognito-sync:*"
Resource: "*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Type: "AWS::Cognito::UserPool"
Properties:
MfaConfiguration: OFF
UserPoolName: ${self:custom.userPoolName}
AccountRecoverySetting:
RecoveryMechanisms:
- Name: verified_email
Priority: 1
AutoVerifiedAttributes:
- email
VerificationMessageTemplate:
DefaultEmailOption: CONFIRM_WITH_LINK
EmailVerificationSubject: 'Your verification link'
EmailVerificationMessage: 'Please click the link below to verify your email address. {####}'
UsernameAttributes:
- email
Policies:
PasswordPolicy:
MinimumLength: 6
RequireLowercase: True
RequireNumbers: True
RequireSymbols: False
RequireUppercase: True
Schema:
- Name: name
AttributeDataType: String
Mutable: true
Required: true
- Name: email
AttributeDataType: String
Mutable: true
Required: true
- Name: picture
AttributeDataType: String
Mutable: true
Required: false
- Name: profile
AttributeDataType: String
Mutable: true
Required: false
- Name: zoneinfo
AttributeDataType: String
Mutable: true
Required: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Type: "AWS::Cognito::UserPoolClient"
Properties:
ClientName: ${self:custom.userPoolClientName}
GenerateSecret: False
SupportedIdentityProviders:
- "COGNITO"
AllowedOAuthFlowsUserPoolClient: true
AllowedOAuthFlows:
- "code"
CallbackURLs:
- ${file(./Resources/StageVariables.yml):CallbackURLs.${self:provider.stage}}
LogoutURLs:
- ${file(./Resources/StageVariables.yml):LogoutURLs.${self:provider.stage}}
DefaultRedirectURI: ${file(./Resources/StageVariables.yml):DefaultRedirectURI.${self:provider.stage}}
AllowedOAuthScopes:
- "phone"
- "email"
- "openid"
- "profile"
- "aws.cognito.signin.user.admin"
ReadAttributes:
- "email"
- "email_verified"
- "name"
- "picture"
- "profile"
- "zoneinfo"
WriteAttributes:
- "name"
- "email"
- "picture"
- "profile"
- "zoneinfo"
UserPoolId:
Ref: CognitoUserPool
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Type: AWS::Cognito::UserPoolDomain
Properties:
Domain: ${self:provider.stage}-${self:service}
UserPoolId:
Ref: CognitoUserPool
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Type: AWS::DynamoDB::Table
Properties:
TableName: UsersTable
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
BillingMode: PAY_PER_REQUEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Type: "AWS::Cognito::IdentityPoolRoleAttachment"
Properties:
IdentityPoolId: !Ref CognitoIdentityPool
Roles:
authenticated: !GetAtt CognitoAuthorizedRole.Arn
unauthenticated: !GetAtt CognitoUnAuthorizedRole.Arn
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
handler: S3Upload/index.main
package:
individually: true
artifact: "./dist/S3Upload.zip"
name: S3Upload-${self:provider.stage}
events:
- http:
path: /s3-upload
method: POST
cors: true
response: ${file(./Resources/LambdaResponses.yml)}
Loading