Skip to content

Commit

Permalink
[Add] scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
tngzng committed Jan 26, 2021
1 parent b0deeb4 commit 89aa379
Show file tree
Hide file tree
Showing 26 changed files with 8,124 additions and 0 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cdk*
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.envrc
.venv
__pycache__
*.egg-info
5 changes: 5 additions & 0 deletions Brewfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tap "aljohri/-"
brew 'awscli'
brew 'awslogs'
brew 'AlJohri/-/kar'
cask 'docker' unless File.directory?('/Applications/Docker.app')
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM python:3.9
WORKDIR /app
COPY setup.py .
COPY lib/__init__.py lib/__init__.py
COPY requirements.txt /app
RUN pip install -r requirements.txt
COPY . .
CMD ["uwsgi", "--http", "0.0.0.0:5000", "--module", "app:app"]
37 changes: 37 additions & 0 deletions Karfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

PORT=5000
REPOSITORY=article-rec-api
DOCKER_OPTS=$(cat <<END_HEREDOC
-v $HOME/.aws/credentials:/root/.aws/credentials:ro \
-v $(pwd):/app \
-p $PORT:$PORT \
-e STAGE=local \
-it $REPOSITORY
END_HEREDOC
)

#@cdk
#+Run cdk.
task-cdk() {
(cd cdk && npx cdk $@)
}

#@build
#+Build docker image.
task-build() {
docker build . -t $REPOSITORY
}

#@run
#+Run docker container.
task-run() {
docker run $DOCKER_OPTS "$@"
}

#@flask
#+Run flask development server with automatic reloading.
task-flask() {
# See: https://flask.palletsprojects.com/en/1.1.x/server/#server
task-run flask run --host 0.0.0.0 --port $PORT --reload --debugger
}
10 changes: 10 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from flask import Flask

from lib.config import config

app = Flask(__name__)

@app.route('/')
def hello_world():
return config.get('HELLO')

11 changes: 11 additions & 0 deletions cdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.js
!jest.config.js
*.d.ts
node_modules

# CDK asset staging directory
.cdk.staging
cdk.out

# Parcel default cache directory
.parcel-cache
6 changes: 6 additions & 0 deletions cdk/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.ts
!*.d.ts

# CDK asset staging directory
.cdk.staging
cdk.out
14 changes: 14 additions & 0 deletions cdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Welcome to your CDK TypeScript project!

This is a blank project for TypeScript development with CDK.

The `cdk.json` file tells the CDK Toolkit how to execute your app.

## Useful commands

* `npm run build` compile typescript to js
* `npm run watch` watch for changes and compile
* `npm run test` perform the jest unit tests
* `cdk deploy` deploy this stack to your default AWS account/region
* `cdk diff` compare deployed stack with current state
* `cdk synth` emits the synthesized CloudFormation template
28 changes: 28 additions & 0 deletions cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from '@aws-cdk/core';
import { AppStack } from "../lib/app-stack";
import { PipelineStack } from "../lib/pipeline-stack";
import { STAGE } from "../lib/helpers";

const app = new cdk.App();
const env = { account: "348955818350", region: "us-east-1" };
const repoName = "article-rec-api";
const appStackName = "ArticleRecAPI";


new AppStack(app, appStackName, {
env,
stage: STAGE.PRODUCTION,
});

new PipelineStack(app, `${appStackName}Pipeline`, {
...env,
repo: { name: repoName },
appStackName: appStackName,
});

new AppStack(app, `Dev${appStackName}`, {
env,
stage: STAGE.DEVELOPMENT,
});
56 changes: 56 additions & 0 deletions cdk/cdk.context.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"hosted-zone:account=348955818350:domainName=localnewslab.io:region=us-east-1": {
"Id": "/hostedzone/Z06424402K2SWPDXBYBCA",
"Name": "localnewslab.io."
},
"vpc-provider:account=348955818350:filter.tag:Name=infrastructure/ProdPublicVPC:region=us-east-1:returnAsymmetricSubnets=true": {
"vpcId": "vpc-0c9addda957e4ab38",
"vpcCidrBlock": "10.0.64.0/18",
"availabilityZones": [],
"subnetGroups": [
{
"name": "Public",
"type": "Public",
"subnets": [
{
"subnetId": "subnet-02509308fad8fd1f0",
"cidr": "10.0.64.0/24",
"availabilityZone": "us-east-1a",
"routeTableId": "rtb-0e40161f67598b786"
},
{
"subnetId": "subnet-05f107b7911c1fe05",
"cidr": "10.0.65.0/24",
"availabilityZone": "us-east-1b",
"routeTableId": "rtb-02406fece9d47b70f"
}
]
}
]
},
"vpc-provider:account=348955818350:filter.tag:Name=infrastructure/DevPublicVPC:region=us-east-1:returnAsymmetricSubnets=true": {
"vpcId": "vpc-05bb341a31d17245e",
"vpcCidrBlock": "10.0.0.0/18",
"availabilityZones": [],
"subnetGroups": [
{
"name": "Public",
"type": "Public",
"subnets": [
{
"subnetId": "subnet-0d7f763ef3d6dc898",
"cidr": "10.0.0.0/24",
"availabilityZone": "us-east-1a",
"routeTableId": "rtb-047dab8a3584bc3f9"
},
{
"subnetId": "subnet-00ff7ea5781b2dffc",
"cidr": "10.0.1.0/24",
"availabilityZone": "us-east-1b",
"routeTableId": "rtb-0c0edaf1fa6e038a0"
}
]
}
]
}
}
8 changes: 8 additions & 0 deletions cdk/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"app": "npx ts-node bin/cdk.ts",
"context": {
"@aws-cdk/core:enableStackNameDuplicates": "true",
"aws-cdk:enableDiffNoFail": "true",
"@aws-cdk/core:stackRelativeExports": "true"
}
}
7 changes: 7 additions & 0 deletions cdk/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
}
};
73 changes: 73 additions & 0 deletions cdk/lib/app-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import * as cdk from "@aws-cdk/core";
import * as ecs from "@aws-cdk/aws-ecs";
import * as ec2 from "@aws-cdk/aws-ec2";
import * as iam from "@aws-cdk/aws-iam";
import * as acm from "@aws-cdk/aws-certificatemanager";
import * as route53 from "@aws-cdk/aws-route53";
import * as helpers from "./helpers";
import { ApplicationLoadBalancedEc2Service } from "@aws-cdk/aws-ecs-patterns";

// TODO this needs to be propagated to the tags
export interface AppStackProps extends cdk.StackProps {
stage: helpers.STAGE;
}

export class AppStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props: AppStackProps) {
super(scope, id, props);

const taskRole = new iam.Role(this, `${id}Role`, {
assumedBy: new iam.ServicePrincipal("ecs-tasks.amazonaws.com"),
inlinePolicies: {
SSMAccess: new iam.PolicyDocument({
statements: [
new iam.PolicyStatement({
sid: `SSMAccess`,
actions: ["ssm:*"],
resources: ["*"],
}),
],
}),
},
});

// const bucketName = helpers.makeBucketName("change-this-bucket-name", props.stage);
// const bucket = helpers.makeBucket(this, bucketName, taskRole, props.stage);

const { cluster } = helpers.getECSCluster(this, props.stage);

const image = ecs.ContainerImage.fromAsset("../", {
extraHash: Date.now().toString(),
});

const domainZone = route53.HostedZone.fromLookup(this, `${id}HostedZone`, {
domainName: 'localnewslab.io'
});

let domainName = "article-rec-api";
if (props.stage == helpers.STAGE.DEVELOPMENT) {
domainName = 'dev-' + domainName;
}

const certificate = acm.Certificate.fromCertificateArn(this, `${id}Certificate`, cdk.Fn.importValue("LocalNewsLab-certificate-arn"));

new ApplicationLoadBalancedEc2Service(this, `${id}Service`, {
cluster,
cpu: 128,
memoryLimitMiB: 128,
desiredCount: 1,
domainName,
domainZone,
certificate,
taskImageOptions: {
image,
containerPort: 5000,
taskRole,
environment: {
STAGE: props.stage,
}
}
})

}
}
Loading

0 comments on commit 89aa379

Please sign in to comment.