Skip to content

Commit 0c99287

Browse files
committed
Updating to serverless-bundle
1 parent 6d5d788 commit 0c99287

File tree

12 files changed

+196
-36
lines changed

12 files changed

+196
-36
lines changed

create.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { success, failure } from "./libs/response-lib";
55
export async function main(event, context) {
66
const data = JSON.parse(event.body);
77
const params = {
8-
TableName: "notes",
8+
TableName: process.env.tableName,
99
Item: {
1010
userId: event.requestContext.identity.cognitoIdentityId,
1111
noteId: uuid.v1(),

delete.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { success, failure } from "./libs/response-lib";
33

44
export async function main(event, context) {
55
const params = {
6-
TableName: "notes",
6+
TableName: process.env.tableName,
77
// 'Key' defines the partition key and sort key of the item to be removed
88
// - 'userId': Identity Pool identity id of the authenticated user
99
// - 'noteId': path parameter

get.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { success, failure } from "./libs/response-lib";
33

44
export async function main(event, context) {
55
const params = {
6-
TableName: "notes",
6+
TableName: process.env.tableName,
77
// 'Key' defines the partition key and sort key of the item to be retrieved
88
// - 'userId': Identity Pool identity id of the authenticated user
99
// - 'noteId': path parameter

handler.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { success, failure } from "./libs/response-lib";
33

44
export async function main(event, context) {
55
const params = {
6-
TableName: "notes",
6+
TableName: process.env.tableName,
77
// 'KeyConditionExpression' defines the condition for the query
88
// - 'userId = :userId': only return items with matching 'userId'
99
// partition key
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
Resources:
2+
# The federated identity for our user pool to auth with
3+
CognitoIdentityPool:
4+
Type: AWS::Cognito::IdentityPool
5+
Properties:
6+
# Generate a name based on the stage
7+
IdentityPoolName: ${self:custom.stage}IdentityPool
8+
# Don't allow unathenticated users
9+
AllowUnauthenticatedIdentities: false
10+
# Link to our User Pool
11+
CognitoIdentityProviders:
12+
- ClientId:
13+
Ref: CognitoUserPoolClient
14+
ProviderName:
15+
Fn::GetAtt: [ "CognitoUserPool", "ProviderName" ]
16+
17+
# IAM roles
18+
CognitoIdentityPoolRoles:
19+
Type: AWS::Cognito::IdentityPoolRoleAttachment
20+
Properties:
21+
IdentityPoolId:
22+
Ref: CognitoIdentityPool
23+
Roles:
24+
authenticated:
25+
Fn::GetAtt: [CognitoAuthRole, Arn]
26+
27+
# IAM role used for authenticated users
28+
CognitoAuthRole:
29+
Type: AWS::IAM::Role
30+
Properties:
31+
Path: /
32+
AssumeRolePolicyDocument:
33+
Version: '2012-10-17'
34+
Statement:
35+
- Effect: 'Allow'
36+
Principal:
37+
Federated: 'cognito-identity.amazonaws.com'
38+
Action:
39+
- 'sts:AssumeRoleWithWebIdentity'
40+
Condition:
41+
StringEquals:
42+
'cognito-identity.amazonaws.com:aud':
43+
Ref: CognitoIdentityPool
44+
'ForAnyValue:StringLike':
45+
'cognito-identity.amazonaws.com:amr': authenticated
46+
Policies:
47+
- PolicyName: 'CognitoAuthorizedPolicy'
48+
PolicyDocument:
49+
Version: '2012-10-17'
50+
Statement:
51+
- Effect: 'Allow'
52+
Action:
53+
- 'mobileanalytics:PutEvents'
54+
- 'cognito-sync:*'
55+
- 'cognito-identity:*'
56+
Resource: '*'
57+
58+
# Allow users to invoke our API
59+
- Effect: 'Allow'
60+
Action:
61+
- 'execute-api:Invoke'
62+
Resource:
63+
Fn::Join:
64+
- ''
65+
-
66+
- 'arn:aws:execute-api:'
67+
- Ref: AWS::Region
68+
- ':'
69+
- Ref: AWS::AccountId
70+
- ':'
71+
- Ref: ApiGatewayRestApi
72+
- '/*'
73+
74+
# Allow users to upload attachments to their
75+
# folder inside our S3 bucket
76+
- Effect: 'Allow'
77+
Action:
78+
- 's3:*'
79+
Resource:
80+
- Fn::Join:
81+
- ''
82+
-
83+
- Fn::GetAtt: [AttachmentsBucket, Arn]
84+
- '/private/'
85+
- '$'
86+
- '{cognito-identity.amazonaws.com:sub}/*'
87+
88+
# Print out the Id of the Identity Pool that is created
89+
Outputs:
90+
IdentityPoolId:
91+
Value:
92+
Ref: CognitoIdentityPool

resources/cognito-user-pool.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Resources:
2+
CognitoUserPool:
3+
Type: AWS::Cognito::UserPool
4+
Properties:
5+
# Generate a name based on the stage
6+
UserPoolName: ${self:custom.stage}-user-pool
7+
# Set email as an alias
8+
UsernameAttributes:
9+
- email
10+
AutoVerifiedAttributes:
11+
- email
12+
13+
CognitoUserPoolClient:
14+
Type: AWS::Cognito::UserPoolClient
15+
Properties:
16+
# Generate an app client name based on the stage
17+
ClientName: ${self:custom.stage}-user-pool-client
18+
UserPoolId:
19+
Ref: CognitoUserPool
20+
ExplicitAuthFlows:
21+
- ADMIN_NO_SRP_AUTH
22+
GenerateSecret: false
23+
24+
# Print out the Id of the User Pool that is created
25+
Outputs:
26+
UserPoolId:
27+
Value:
28+
Ref: CognitoUserPool
29+
30+
UserPoolClientId:
31+
Value:
32+
Ref: CognitoUserPoolClient

resources/dynamodb-table.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Resources:
2+
NotesTable:
3+
Type: AWS::DynamoDB::Table
4+
Properties:
5+
TableName: ${self:custom.tableName}
6+
AttributeDefinitions:
7+
- AttributeName: userId
8+
AttributeType: S
9+
- AttributeName: noteId
10+
AttributeType: S
11+
KeySchema:
12+
- AttributeName: userId
13+
KeyType: HASH
14+
- AttributeName: noteId
15+
KeyType: RANGE
16+
# Set the capacity to auto-scale
17+
BillingMode: PAY_PER_REQUEST

resources/s3-bucket.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Resources:
2+
AttachmentsBucket:
3+
Type: AWS::S3::Bucket
4+
Properties:
5+
# Set the CORS policy
6+
CorsConfiguration:
7+
CorsRules:
8+
-
9+
AllowedOrigins:
10+
- '*'
11+
AllowedHeaders:
12+
- '*'
13+
AllowedMethods:
14+
- GET
15+
- PUT
16+
- POST
17+
- DELETE
18+
- HEAD
19+
MaxAge: 3000
20+
21+
# Print out the name of the bucket that is created
22+
Outputs:
23+
AttachmentsBucketName:
24+
Value:
25+
Ref: AttachmentsBucket

serverless.yml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
service: notes-app-api
1+
service: notes-app-2-api
22

33
# Create an optimized package for our functions
44
package:
@@ -8,14 +8,24 @@ plugins:
88
- serverless-bundle # Package our functions with Webpack
99
- serverless-offline
1010

11+
custom:
12+
# Our stage is based on what is passed in when running serverless
13+
# commands. Or fallsback to what we have set in the provider section.
14+
stage: ${opt:stage, self:provider.stage}
15+
# Set the table name here so we can use it while testing locally
16+
tableName: ${self:custom.stage}-notes
17+
1118
provider:
1219
name: aws
1320
runtime: nodejs8.10
14-
stage: prod
21+
stage: dev
1522
region: us-east-1
1623

17-
# 'iamRoleStatements' defines the permission policy for the Lambda function.
18-
# In this case Lambda functions are granted with permissions to access DynamoDB.
24+
# These environment variables are made available to our functions
25+
# under process.env.
26+
environment:
27+
tableName: ${self:custom.tableName}
28+
1929
iamRoleStatements:
2030
- Effect: Allow
2131
Action:
@@ -26,7 +36,10 @@ provider:
2636
- dynamodb:PutItem
2737
- dynamodb:UpdateItem
2838
- dynamodb:DeleteItem
29-
Resource: "arn:aws:dynamodb:us-east-1:*:*"
39+
# Restrict our IAM role permissions to
40+
# the specific table for the stage
41+
Resource:
42+
- "Fn::GetAtt": [ NotesTable, Arn ]
3043

3144
functions:
3245
# Defines an HTTP API endpoint that calls the main function in create.js
@@ -96,3 +109,10 @@ functions:
96109
resources:
97110
# API Gateway Errors
98111
- ${file(resources/api-gateway-errors.yml)}
112+
# DynamoDB
113+
- ${file(resources/dynamodb-table.yml)}
114+
# S3
115+
- ${file(resources/s3-bucket.yml)}
116+
# Cognito
117+
- ${file(resources/cognito-user-pool.yml)}
118+
- ${file(resources/cognito-identity-pool.yml)}

0 commit comments

Comments
 (0)