Skip to content

Commit

Permalink
Deploy initial resources working
Browse files Browse the repository at this point in the history
  • Loading branch information
ianpogi5 committed Jun 26, 2021
1 parent 525cc2f commit d245dd1
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 44 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ You'll need [docker](https://www.docker.com/) installed for running [local dynam
PROJECT_NAME: myproject.com
REGION: ap-southeast-1
PROFILE: dev
FIRST_USER_EMAIL: [email protected]
```
`PROJECT_NAME` will be referenced in all resources used in AWS. It must be unique with only letters and numbers. It should also be unique for S3 bucket name and cognito pool client domain usage. I recommend using a subdomain like `admin-dev.myproject.com`.
Expand All @@ -51,7 +52,7 @@ You'll need [docker](https://www.docker.com/) installed for running [local dynam
3. Run local setup script

```bash
yarn setup:local [email protected]
yarn setup:local
```

This will create cognito resources in your AWS account and then run a local dynamodb.
Expand Down Expand Up @@ -93,6 +94,7 @@ Here are the services that will be running:
UPLOAD_BUCKET: admin-upload.myproject.com
REGION: ap-southeast-1
PROFILE: my-production-profile
FIRST_USER_EMAIL: [email protected]
```

`PROJECT_NAME` will be referenced in all resources used in AWS. It must be unique with only letters and numbers. It should also be unique for S3 bucket name and cognito pool client domain usage. I recommend using a subdomain like `admin.myproject.com`.
Expand All @@ -106,7 +108,7 @@ Here are the services that will be running:
3. Run deploy script

```bash
yarn deploy prod [email protected]
yarn deploy prod
```

The script will take about 20 minutes to finish to provisioning everything.
Expand Down
1 change: 1 addition & 0 deletions config/sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ PROJECT_NAME: admin.myproject.com
UPLOAD_BUCKET: admin-upload.myproject.com
REGION: ap-southeast-1
PROFILE: my-profile
FIRST_USER_EMAIL: [email protected]
5 changes: 3 additions & 2 deletions infra/lib/cms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export class CMSStack extends cdk.NestedStack {
super(scope, id, props);

this.bucket = new s3.Bucket(this, `${id}-S3`, {
bucketName: `abu-cms-cdk-s3bucket`,
bucketName: process.env.PROJECT,
removalPolicy: cdk.RemovalPolicy.DESTROY,
});

const cloudFrontOAI = new cloudFront.OriginAccessIdentity(
Expand Down Expand Up @@ -64,7 +65,7 @@ export class CMSStack extends cdk.NestedStack {
errorCachingMinTtl: 0,
},
],
comment: `abu-cms CDK - CloudFront Distribution`,
comment: `${process.env.PROJECT} - CloudFront Distribution`,
viewerProtocolPolicy: cloudFront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
});
}
Expand Down
15 changes: 5 additions & 10 deletions infra/lib/db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as cdk from "@aws-cdk/core";
import * as dynamodb from "@aws-cdk/aws-dynamodb";
import schema from "./schema.json";

export class DBStack extends cdk.NestedStack {
public readonly ddb: dynamodb.Table;
Expand All @@ -9,7 +8,7 @@ export class DBStack extends cdk.NestedStack {
super(scope, id, props);

this.ddb = new dynamodb.Table(this, `${id}-DDB`, {
tableName: schema.TableName,
tableName: process.env.PROJECT,
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
stream: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES,
partitionKey: {
Expand All @@ -20,17 +19,17 @@ export class DBStack extends cdk.NestedStack {
name: "sk",
type: dynamodb.AttributeType.STRING,
},
// removalPolicy: cdk.RemovalPolicy.DESTROY,
removalPolicy: cdk.RemovalPolicy.DESTROY,
});

this.ddb.addGlobalSecondaryIndex({
indexName: "GSI",
partitionKey: {
name: "pk2",
name: "gsi1pk",
type: dynamodb.AttributeType.STRING,
},
sortKey: {
name: "sk2",
name: "gsi1sk",
type: dynamodb.AttributeType.STRING,
},
projectionType: dynamodb.ProjectionType.ALL,
Expand All @@ -39,11 +38,7 @@ export class DBStack extends cdk.NestedStack {
this.ddb.addGlobalSecondaryIndex({
indexName: "GSI2",
partitionKey: {
name: "pk3",
type: dynamodb.AttributeType.STRING,
},
sortKey: {
name: "sk3",
name: "gsi2pk",
type: dynamodb.AttributeType.STRING,
},
projectionType: dynamodb.ProjectionType.ALL,
Expand Down
14 changes: 7 additions & 7 deletions infra/lib/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
"AttributeType": "S"
},
{
"AttributeName": "pk2",
"AttributeName": "gsi1pk",
"AttributeType": "S"
},
{
"AttributeName": "sk2",
"AttributeName": "gsi1sk",
"AttributeType": "S"
},
{
"AttributeName": "pk3",
"AttributeName": "gsi2pk",
"AttributeType": "S"
}
],
Expand All @@ -42,11 +42,11 @@
"IndexName": "GSI",
"KeySchema": [
{
"AttributeName": "pk2",
"AttributeName": "gsi1pk",
"KeyType": "HASH"
},
{
"AttributeName": "sk2",
"AttributeName": "gsi2pk",
"KeyType": "RANGE"
}
],
Expand All @@ -58,7 +58,7 @@
"IndexName": "GSI2",
"KeySchema": [
{
"AttributeName": "pk3",
"AttributeName": "gsi2pk",
"KeyType": "HASH"
}
],
Expand All @@ -67,4 +67,4 @@
}
}
]
}
}
11 changes: 7 additions & 4 deletions scripts/config-to-env.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#!/bin/bash

SEARCH_KEY="PROJECT_NAME: "
PROJECT=$(grep "$SEARCH_KEY" ../config/$STAGE.yml | sed -e "s/^$SEARCH_KEY//")
export PROJECT=$(grep "$SEARCH_KEY" ../config/$STAGE.yml | sed -e "s/^$SEARCH_KEY//")

SEARCH_KEY="UPLOAD_BUCKET: "
UPLOAD_BUCKET=$(grep "$SEARCH_KEY" ../config/$STAGE.yml | sed -e "s/^$SEARCH_KEY//")
export UPLOAD_BUCKET=$(grep "$SEARCH_KEY" ../config/$STAGE.yml | sed -e "s/^$SEARCH_KEY//")

SEARCH_KEY="REGION: "
REGION=$(grep "$SEARCH_KEY" ../config/$STAGE.yml | sed -e "s/^$SEARCH_KEY//")
export REGION=$(grep "$SEARCH_KEY" ../config/$STAGE.yml | sed -e "s/^$SEARCH_KEY//")

SEARCH_KEY="PROFILE: "
PROFILE=$(grep "$SEARCH_KEY" ../config/$STAGE.yml | sed -e "s/^$SEARCH_KEY//")
export PROFILE=$(grep "$SEARCH_KEY" ../config/$STAGE.yml | sed -e "s/^$SEARCH_KEY//")

SEARCH_KEY="FIRST_USER_EMAIL: "
export FIRST_USER_EMAIL=$(grep "$SEARCH_KEY" ../config/$STAGE.yml | sed -e "s/^$SEARCH_KEY//")
48 changes: 29 additions & 19 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ NC='\033[0m' # No Color
if [ -z "$1" ]
then
echo -e "\nThis script will deploy the CMS.\n"
echo -e "Usage: deploy.sh ${BLUE}<stage> <email>${NC}\n"
echo -e "Example: deploy.sh prod [email protected]\n"
echo -e "Usage: deploy.sh ${BLUE}<stage>${NC}\n"
echo -e "Example: deploy.sh prod\n"
exit 1
fi

STAGE=$1
export STAGE=$1
CONFIG_FILE=./config/$STAGE.yml

if [ ! -f $CONFIG_FILE ]
Expand All @@ -25,26 +25,36 @@ if [ ! -f $CONFIG_FILE ]
exit 1
fi

if [ -z "$2" ]
then
echo -e "\nMissing ${RED}email${NC}\n"
exit 1
fi
# Change to script dir
cd "$(dirname "$0")"
source ./config-to-env.sh

# Export AWS Profile to use
export AWS_PROFILE=$PROFILE
export AWS_REGION=$REGION
export ABU_STAGE=$STAGE

# Change to root dir
cd ..

yarn workspace infra cdk bootstrap
yarn workspace infra build
yarn workspace infra deploy

EMAIL=$2
exit 0

# Run setup
./scripts/setup-stage.sh $STAGE $EMAIL
# # Run setup
# ./scripts/setup-stage.sh $STAGE

# update lambda memory and timeout
./scripts/update-lambda-edge.sh $STAGE
# # update lambda memory and timeout
# ./scripts/update-lambda-edge.sh $STAGE

# Build API
yarn build:api
# # Build API
# yarn build:api

# Run deploy api
yarn deploy:api $STAGE
# # Run deploy api
# yarn deploy:api $STAGE

# Run deploy cms
yarn deploy:cms $STAGE
# # Run deploy cms
# yarn deploy:cms $STAGE

0 comments on commit d245dd1

Please sign in to comment.