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

create the main serverless function with the cdk #3

Merged
merged 2 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dist
.next
.turbo
coverage
env.d.ts
12 changes: 12 additions & 0 deletions apps/core/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type EnvironmentVariables = {
HOST: string;
PORT: string;
};

declare global {
namespace NodeJS {
interface ProcessEnv extends EnvironmentVariables {}
}
}

export {};
8 changes: 4 additions & 4 deletions apps/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { sortNumbers } from '@sharingan/utils';

dotenv.config();

const HOST = process.env.HOST ?? 'http:https://localhost';
const PORT = parseInt(process.env.PORT ?? '7501');
const { HOST, PORT } = process.env;
const SERVER_PORT = parseInt(PORT ?? '7501');

const app = express();

Expand All @@ -16,8 +16,8 @@ app.get('/', (_req, res) => {
return res.json({ message: 'Hello World!' });
});

app.listen(PORT, () => {
app.listen(SERVER_PORT, () => {
console.log(sortNumbers([67, 80, 4, 11, 90, 54, 22]));

console.log(`Application started on URL ${HOST}:${PORT} 🎉`);
console.log(`Application started on URL ${HOST}:${SERVER_PORT} 🎉`);
});
1 change: 1 addition & 0 deletions apps/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"composite": true,
"sourceMap": true
},
"files": ["env.d.ts"],
"include": [
"./src/**/*", "./tests/**/*"
],
Expand Down
118 changes: 118 additions & 0 deletions apps/functions/kyubi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
### Node template
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

6 changes: 6 additions & 0 deletions apps/functions/kyubi/.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
15 changes: 15 additions & 0 deletions apps/functions/kyubi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Welcome to your CDK TypeScript project

You should explore the contents of this project. It demonstrates a CDK app with an instance of a stack (`KyubiStack`)
which contains an Amazon SQS queue that is subscribed to an Amazon SNS topic.

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
6 changes: 6 additions & 0 deletions apps/functions/kyubi/bin/kyubi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import { KyubiStack } from '../lib/kyubi-stack';

const app = new cdk.App();
new KyubiStack(app, 'KyubiStack');
32 changes: 32 additions & 0 deletions apps/functions/kyubi/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"app": "npx ts-node --prefer-ts-exts bin/kyubi.ts",
"watch": {
"include": [
"**"
],
"exclude": [
"README.md",
"cdk*.json",
"**/*.d.ts",
"**/*.js",
"tsconfig.json",
"package*.json",
"yarn.lock",
"node_modules",
"test"
]
},
"context": {
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
"@aws-cdk/core:stackRelativeExports": true,
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true,
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
"@aws-cdk/core:target-partitions": [
"aws",
"aws-cn"
]
}
}
8 changes: 8 additions & 0 deletions apps/functions/kyubi/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
}
};
19 changes: 19 additions & 0 deletions apps/functions/kyubi/lib/kyubi-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Duration, Stack, StackProps } from 'aws-cdk-lib';
import * as sns from 'aws-cdk-lib/aws-sns';
import * as subs from 'aws-cdk-lib/aws-sns-subscriptions';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import { Construct } from 'constructs';

export class KyubiStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);

const queue = new sqs.Queue(this, 'KyubiQueue', {
visibilityTimeout: Duration.seconds(300)
});

const topic = new sns.Topic(this, 'KyubiTopic');

topic.addSubscription(new subs.SqsSubscription(queue));
}
}
Loading