Skip to content

Commit 777bad3

Browse files
committed
Setting up our Serverless infrastructure
1 parent 08789dc commit 777bad3

5 files changed

Lines changed: 187 additions & 6 deletions

File tree

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: 21 additions & 6 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:
@@ -9,20 +9,25 @@ plugins:
99
- serverless-offline
1010
- serverless-dotenv-plugin # Load .env as environment variables
1111

12+
custom:
13+
# Our stage is based on what is passed in when running serverless
14+
# commands. Or fallsback to what we have set in the provider section.
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+
1219
provider:
1320
name: aws
1421
runtime: nodejs12.x
15-
stage: prod
22+
stage: dev
1623
region: us-east-1
1724

1825
# These environment variables are made available to our functions
1926
# under process.env.
2027
environment:
21-
tableName: notes
28+
tableName: ${self:custom.tableName}
2229
stripeSecretKey: ${env:STRIPE_SECRET_KEY}
2330

24-
# 'iamRoleStatements' defines the permission policy for the Lambda function.
25-
# In this case Lambda functions are granted with permissions to access DynamoDB.
2631
iamRoleStatements:
2732
- Effect: Allow
2833
Action:
@@ -33,7 +38,10 @@ provider:
3338
- dynamodb:PutItem
3439
- dynamodb:UpdateItem
3540
- dynamodb:DeleteItem
36-
Resource: "arn:aws:dynamodb:us-east-1:*:*"
41+
# Restrict our IAM role permissions to
42+
# the specific table for the stage
43+
Resource:
44+
- "Fn::GetAtt": [ NotesTable, Arn ]
3745

3846
functions:
3947
# Defines an HTTP API endpoint that calls the main function in create.js
@@ -115,3 +123,10 @@ functions:
115123
resources:
116124
# API Gateway Errors
117125
- ${file(resources/api-gateway-errors.yml)}
126+
# DynamoDB
127+
- ${file(resources/dynamodb-table.yml)}
128+
# S3
129+
- ${file(resources/s3-bucket.yml)}
130+
# Cognito
131+
- ${file(resources/cognito-user-pool.yml)}
132+
- ${file(resources/cognito-identity-pool.yml)}

0 commit comments

Comments
 (0)