Skip to content

Commit a1ee07e

Browse files
committed
Setting up Serverless infrastructure
1 parent 4d1afe0 commit a1ee07e

File tree

5 files changed

+192
-4
lines changed

5 files changed

+192
-4
lines changed

resources/cognito-identity-pool.yml

+92
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 unauthenticated 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

+32
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

+17
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

+25
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

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
service: serverless-react-notes-app-api
1+
service: serverless-react-notes-app-2-api
22

33
package:
44
individually: true
@@ -8,13 +8,23 @@ plugins:
88
- serverless-offline
99
- serverless-dotenv-plugin
1010

11+
custom:
12+
# Our stage is based on what is passed in when running serverless
13+
# commands (opt: stage). Or fallback to what we have set in the
14+
# provider section (self:provider.stage).
15+
stage: ${opt:stage, self:provider.stage}
16+
# Set the table name here so we can use it while testing locally
17+
tableName: ${self:custom.stage}-notes
18+
1119
provider:
1220
name: aws
1321
runtime: nodejs10.x
14-
stage: prod
22+
stage: dev
1523
region: eu-central-1
24+
# These environment variables are made available to our functions
25+
# under process.env.
1626
environment:
17-
tableName: notes
27+
tableName: ${self:custom.tableName}
1828
stripeSecretKey: ${env:STRIPE_SECRET_KEY}
1929
iamRoleStatements:
2030
- Effect: Allow
@@ -26,7 +36,10 @@ provider:
2636
- dynamodb:PutItem
2737
- dynamodb:UpdateItem
2838
- dynamodb:DeleteItem
29-
Resource: "arn:aws:dynamodb:eu-central-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
create:
@@ -78,5 +91,14 @@ functions:
7891
cors: true
7992
authorizer: aws_iam
8093

94+
# Create our resources with separate CloudFormation templates
8195
resources:
96+
# API Gateway Errors
8297
- ${file(resources/api-gateway-errors.yml)}
98+
# DynamoDB
99+
- ${file(resources/dynamodb-table.yml)}
100+
# S3
101+
- ${file(resources/s3-bucket.yml)}
102+
# Cognito
103+
- ${file(resources/cognito-user-pool.yml)}
104+
- ${file(resources/cognito-identity-pool.yml)}

0 commit comments

Comments
 (0)