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

Bump lodash from 4.17.20 to 4.17.21 in /infrastructure #44

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
909d782
1.1.1
jayair Jul 6, 2020
ca870a9
1.1.2
jayair Jul 6, 2020
380e6a5
1.1.3
jayair Jul 6, 2020
373e9f6
Simplifying error handling
jayair Jul 7, 2020
184c01a
Merge branch 'handle-api-gateway-cors-errors' into deploy-your-server…
jayair Jul 7, 2020
6d372cb
Merge branch 'deploy-your-serverless-infrastructure'
jayair Jul 7, 2020
7c7a7b7
Simplifying error handling
jayair Jul 7, 2020
9fe3510
Bump lodash from 4.17.15 to 4.17.19 (#37)
dependabot[bot] Jul 19, 2020
0ce503f
Bump elliptic from 6.5.2 to 6.5.3 (#39)
dependabot[bot] Aug 1, 2020
4695cbf
Moving to sst
jayair Sep 25, 2020
1bf858e
Forcing the right table name
jayair Sep 25, 2020
b5947b4
Adding new construct
jayair Sep 30, 2020
5618577
Adding eslint and prettier
jayair Sep 30, 2020
0ef9629
Adding ignore
jayair Sep 30, 2020
557a9e7
Updating sst and serverless
jayair Sep 30, 2020
446c79f
Fixing cdk tests
jayair Sep 30, 2020
e90e4b1
Importing table name
jayair Sep 30, 2020
090661a
Cleaning up imports
jayair Sep 30, 2020
750be64
Removing export
jayair Sep 30, 2020
c1b18e3
Working through the steps
jayair Oct 1, 2020
016217c
Adding comments
jayair Oct 2, 2020
537f06e
Tweaking ids
jayair Oct 2, 2020
6ab658a
Add comments to CDK code
fwang Oct 6, 2020
8b6c7fd
Remove authFlows option in User Pool Client
fwang Oct 6, 2020
59112e1
Added comments for CDK code
fwang Oct 7, 2020
1ebff97
Setting up our Serverless infrastructure
fwang Oct 7, 2020
412fe51
Adding error logging
fwang Oct 7, 2020
2279c4d
Fix typo
fwang Oct 7, 2020
f745273
Updating with SST info
jayair Oct 7, 2020
f64f667
Merge branch 'sst-master'
fwang Oct 8, 2020
f4d8694
Small tweaks
jayair Nov 11, 2020
9e56d36
Bump node-notifier from 8.0.0 to 8.0.1 in /infrastructure (#41)
dependabot[bot] Jan 3, 2021
26c4e3b
Update README.md
jayair Feb 19, 2021
ef6f504
Bump lodash from 4.17.20 to 4.17.21 in /infrastructure
dependabot[bot] May 11, 2021
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
42 changes: 32 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# Serverless Stack Demo API

[Serverless Stack](http://serverless-stack.com) is a free comprehensive guide to creating full-stack serverless applications. We create a [note taking app](http://demo2.serverless-stack.com) from scratch.
The [Serverless Stack Guide](http://serverless-stack.com/#guide) is a free comprehensive resource to creating full-stack serverless applications. We create a [note taking app](http://demo2.serverless-stack.com) from scratch.

This repo is for the serverless backend API that we build over the course of the tutorial. You can find the repo for the frontend React app [here](https://github.com/AnomalyInnovations/serverless-stack-demo-client). And the repo for the tutorial [here](https://github.com/AnomalyInnovations/serverless-stack-com).

This repo is split into:

- **services**: [Serverless Framework](https://github.com/serverless/serverless) services
- **infrastructure**: An [SST](https://github.com/serverless-stack/serverless-stack) app

#### Steps

To support the different chapters and steps of the tutorial; we use branches to represent the project codebase at the various points. Here is an index of the various chapters and branches in order.
Expand All @@ -16,26 +21,43 @@ To support the different chapters and steps of the tutorial; we use branches to

To use this repo locally you need to have the [Serverless framework](https://serverless.com) installed.

``` bash
```bash
$ npm install serverless -g
```

Clone this repo and install the NPM packages.
Clone this repo.

``` bash
```bash
$ git clone https://github.com/AnomalyInnovations/serverless-stack-demo-api
```

Head over to the `infrastructure/` directory and install the npm packages.

``` bash
$ npm install
```

Run a single API on local.
And build the SST app.

``` bash
$ npx sst build
```

Then deploy it to your AWS account

``` bash
$ npx sst deploy
```

Then head over to `services/notes/`. And run a single API endpoint locally.

```bash
$ serverless invoke local --function list --path event.json
```

Where, `event.json` contains the request event info and looks something like this.

``` json
```json
{
"requestContext": {
"authorizer": {
Expand All @@ -47,16 +69,16 @@ Where, `event.json` contains the request event info and looks something like thi
}
```

Finally, run this to deploy to your AWS account.
Finally, run this to deploy to the API to your AWS account.

``` bash
```bash
$ serverless deploy
```

This project refers to an `.env` file for secret environment variables that are not checking in to the repo. Make sure to create one before dpeloying - https://serverless-stack.com/chapters/load-secrets-from-env.html.
The API service refers to an `.env` file for secret environment variables that are not checking in to the repo. Make sure to create one before deploying - https://serverless-stack.com/chapters/load-secrets-from-env.html.

---

This repo is maintained by [Anomaly Innovations](https://anoma.ly); makers of [Seed](https://seed.run) and [Serverless Stack](https://serverless-stack.com).

[Email]: mailto:contact@anoma.ly
[email]: mailto:contact@anoma.ly
29 changes: 0 additions & 29 deletions create.js

This file was deleted.

20 changes: 20 additions & 0 deletions infrastructure/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store

# vim
.*.sw*

npm-debug.log*
yarn-debug.log*
yarn-error.log*
3 changes: 3 additions & 0 deletions infrastructure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Getting Started with Serverless Stack Resources

This project was bootstrapped with [Create Serverless Stack](https://github.com/serverless-stack/serverless-stack).
50 changes: 50 additions & 0 deletions infrastructure/lib/CognitoAuthRole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as cdk from "@aws-cdk/core";
import * as iam from "@aws-cdk/aws-iam";
import * as cognito from "@aws-cdk/aws-cognito";

export default class CognitoAuthRole extends cdk.Construct {
// Public reference to the IAM role
role;

constructor(scope, id, props) {
super(scope, id);

const { identityPool } = props;

// IAM role used for authenticated users
this.role = new iam.Role(this, "CognitoDefaultAuthenticatedRole", {
assumedBy: new iam.FederatedPrincipal(
"cognito-identity.amazonaws.com",
{
StringEquals: {
"cognito-identity.amazonaws.com:aud": identityPool.ref,
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "authenticated",
},
},
"sts:AssumeRoleWithWebIdentity"
),
});
this.role.addToPolicy(
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
"mobileanalytics:PutEvents",
"cognito-sync:*",
"cognito-identity:*",
],
resources: ["*"],
})
);

new cognito.CfnIdentityPoolRoleAttachment(
this,
"IdentityPoolRoleAttachment",
{
identityPoolId: identityPool.ref,
roles: { authenticated: this.role.roleArn },
}
);
}
}
66 changes: 66 additions & 0 deletions infrastructure/lib/CognitoStack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { CfnOutput } from "@aws-cdk/core";
import * as iam from "@aws-cdk/aws-iam";
import * as cognito from "@aws-cdk/aws-cognito";
import * as sst from "@serverless-stack/resources";
import CognitoAuthRole from "./CognitoAuthRole";

export default class CognitoStack extends sst.Stack {
constructor(scope, id, props) {
super(scope, id, props);

const { bucketArn } = props;

const app = this.node.root;

const userPool = new cognito.UserPool(this, "UserPool", {
selfSignUpEnabled: true, // Allow users to sign up
autoVerify: { email: true }, // Verify email addresses by sending a verification code
signInAliases: { email: true }, // Set email as an alias
});

const userPoolClient = new cognito.UserPoolClient(this, "UserPoolClient", {
userPool,
generateSecret: false, // Don't need to generate secret for web app running on browsers
});

const identityPool = new cognito.CfnIdentityPool(this, "IdentityPool", {
allowUnauthenticatedIdentities: false, // Don't allow unathenticated users
cognitoIdentityProviders: [
{
clientId: userPoolClient.userPoolClientId,
providerName: userPool.userPoolProviderName,
},
],
});

const authenticatedRole = new CognitoAuthRole(this, "CognitoAuthRole", {
identityPool,
});

authenticatedRole.role.addToPolicy(
// IAM policy granting users permission to a specific folder in the S3 bucket
new iam.PolicyStatement({
actions: ["s3:*"],
effect: iam.Effect.ALLOW,
resources: [
bucketArn + "/private/${cognito-identity.amazonaws.com:sub}/*",
],
})
);

// Export values
new CfnOutput(this, "UserPoolId", {
value: userPool.userPoolId,
});
new CfnOutput(this, "UserPoolClientId", {
value: userPoolClient.userPoolClientId,
});
new CfnOutput(this, "IdentityPoolId", {
value: identityPool.ref,
});
new CfnOutput(this, "AuthenticatedRoleName", {
value: authenticatedRole.role.roleName,
exportName: app.logicalPrefixedName("CognitoAuthRole"),
});
}
}
27 changes: 27 additions & 0 deletions infrastructure/lib/DynamoDBStack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CfnOutput } from "@aws-cdk/core";
import * as dynamodb from "@aws-cdk/aws-dynamodb";
import * as sst from "@serverless-stack/resources";

export default class DynamoDBStack extends sst.Stack {
constructor(scope, id, props) {
super(scope, id, props);

const app = this.node.root;

const table = new dynamodb.Table(this, "Table", {
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST, // Use on-demand billing mode
sortKey: { name: "noteId", type: dynamodb.AttributeType.STRING },
partitionKey: { name: "userId", type: dynamodb.AttributeType.STRING },
});

// Output values
new CfnOutput(this, "TableName", {
value: table.tableName,
exportName: app.logicalPrefixedName("TableName"),
});
new CfnOutput(this, "TableArn", {
value: table.tableArn,
exportName: app.logicalPrefixedName("TableArn"),
});
}
}
29 changes: 29 additions & 0 deletions infrastructure/lib/S3Stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as cdk from "@aws-cdk/core";
import * as s3 from "@aws-cdk/aws-s3";
import * as sst from "@serverless-stack/resources";

export default class S3Stack extends sst.Stack {
// Public reference to the S3 bucket
bucket;

constructor(scope, id, props) {
super(scope, id, props);

this.bucket = new s3.Bucket(this, "Uploads", {
// Allow client side access to the bucket from a different domain
cors: [
{
maxAge: 3000,
allowedOrigins: ["*"],
allowedHeaders: ["*"],
allowedMethods: ["GET", "PUT", "POST", "DELETE", "HEAD"],
},
],
});

// Export values
new cdk.CfnOutput(this, "AttachmentsBucketName", {
value: this.bucket.bucketName,
});
}
}
12 changes: 12 additions & 0 deletions infrastructure/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import S3Stack from "./S3Stack";
import CognitoStack from "./CognitoStack";
import DynamoDBStack from "./DynamoDBStack";

// Add stacks
export default function main(app) {
new DynamoDBStack(app, "dynamodb");

const s3 = new S3Stack(app, "s3");

new CognitoStack(app, "cognito", { bucketArn: s3.bucket.bucketArn });
}
Loading