Skip to content

Commit 456e2b7

Browse files
authored
. (#3)
1 parent ee21af8 commit 456e2b7

28 files changed

+11936
-14
lines changed

.gitignore

Lines changed: 151 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
# Compiled class file
2-
*.class
31

4-
# Log file
5-
*.log
6-
7-
# BlueJ files
8-
*.ctxt
92

10-
# Mobile Tools for Java (J2ME)
11-
.mtj.tmp/
3+
.gradle
4+
.vscode
5+
**/logs
126

13-
# Package Files #
7+
*.log
148
*.jar
159
*.war
1610
*.nar
@@ -19,5 +13,150 @@
1913
*.tar.gz
2014
*.rar
2115

22-
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23-
hs_err_pid*
16+
# internal
17+
*internal*
18+
19+
# MAC
20+
**/.DS_Store
21+
22+
# Lambda layer
23+
**/layers
24+
**/temp
25+
26+
# CDK
27+
**/node_modules
28+
**/cdk.out
29+
**/cdk.context.json
30+
31+
#-------------------
32+
33+
# Byte-compiled / optimized / DLL files
34+
__pycache__/
35+
*.py[cod]
36+
*$py.class
37+
38+
# C extensions
39+
*.so
40+
41+
# Distribution / packaging
42+
.Python
43+
build/
44+
develop-eggs/
45+
dist/
46+
downloads/
47+
eggs/
48+
.eggs/
49+
# CDK
50+
#lib/
51+
lib64/
52+
parts/
53+
sdist/
54+
var/
55+
wheels/
56+
pip-wheel-metadata/
57+
share/python-wheels/
58+
*.egg-info/
59+
.installed.cfg
60+
*.egg
61+
MANIFEST
62+
63+
# PyInstaller
64+
# Usually these files are written by a python script from a template
65+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
66+
*.manifest
67+
*.spec
68+
69+
# Installer logs
70+
pip-log.txt
71+
pip-delete-this-directory.txt
72+
73+
# Unit test / coverage reports
74+
htmlcov/
75+
.tox/
76+
.nox/
77+
.coverage
78+
.coverage.*
79+
.cache
80+
nosetests.xml
81+
coverage.xml
82+
*.cover
83+
*.py,cover
84+
.hypothesis/
85+
.pytest_cache/
86+
87+
# Translations
88+
*.mo
89+
*.pot
90+
91+
# Django stuff:
92+
*.log
93+
local_settings.py
94+
db.sqlite3
95+
db.sqlite3-journal
96+
97+
# Flask stuff:
98+
instance/
99+
.webassets-cache
100+
101+
# Scrapy stuff:
102+
.scrapy
103+
104+
# Sphinx documentation
105+
docs/_build/
106+
107+
# PyBuilder
108+
target/
109+
110+
# Jupyter Notebook
111+
.ipynb_checkpoints
112+
113+
# IPython
114+
profile_default/
115+
ipython_config.py
116+
117+
# pyenv
118+
.python-version
119+
120+
# pipenv
121+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
122+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
123+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
124+
# install all needed dependencies.
125+
#Pipfile.lock
126+
127+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
128+
__pypackages__/
129+
130+
# Celery stuff
131+
celerybeat-schedule
132+
celerybeat.pid
133+
134+
# SageMath parsed files
135+
*.sage.py
136+
137+
# Environments
138+
.env
139+
.venv
140+
env/
141+
venv/
142+
ENV/
143+
env.bak/
144+
venv.bak/
145+
146+
# Spyder project settings
147+
.spyderproject
148+
.spyproject
149+
150+
# Rope project settings
151+
.ropeproject
152+
153+
# mkdocs documentation
154+
/site
155+
156+
# mypy
157+
.mypy_cache/
158+
.dmypy.json
159+
dmypy.json
160+
161+
# Pyre type checker
162+
.pyre/

README.md

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,81 @@
1-
# cdk-lambda-apigateway
2-
CDK Lambda API Gateway sample project
1+
# Sample project for API Gateway and Lambda with CDK
2+
3+
To deploy without dependencies and CloudFormation limitation of Template body size(512k), create small API stacks for each Microservice or module.
4+
5+
![apigw](./screenshots/apigw.png?raw=true)
6+
7+
Table of Contents
8+
9+
1. Deploy root-api stack
10+
2. Deploy device-api stack
11+
3. Deploy product-api stack
12+
13+
## Prerequisites
14+
15+
```bash
16+
npm install -g aws-cdk@2.25.0
17+
18+
# install packages in the root folder
19+
npm install
20+
cdk bootstrap
21+
```
22+
23+
Use the `cdk` command-line toolkit to interact with your project:
24+
25+
* `cdk deploy`: deploys your app into an AWS account
26+
* `cdk synth`: synthesizes an AWS CloudFormation template for your app
27+
* `cdk diff`: compares your app with the deployed stack
28+
* `cdk watch`: deployment every time a file change is detected
29+
30+
## CDK Stack
31+
32+
| Stack | Time |
33+
|-------------------------------|---------|
34+
| root-api (24kb) | 1m 30s |
35+
| device-api (24kb) | 1m 30s |
36+
| product-api (20kb) | 1m 30s |
37+
| Total | 4m 30s |
38+
39+
### Step 1: root-api
40+
41+
Create the Root API and dummy method to import from device-api and product-api.
42+
43+
```bash
44+
cd root-api
45+
cdk deploy
46+
```
47+
48+
[root-api/lib/root-api-stack.ts](./root-api/lib/root-api-stack.ts)
49+
50+
### Step 2: device-api
51+
52+
```bash
53+
cd ../device-api
54+
cdk deploy
55+
```
56+
57+
[device-api/lib/device-api-stack.ts](./device-api/lib/device-api-stack.ts)
58+
59+
SSM parameter:
60+
61+
* /cdk-lambda-apigateway/rest-api-id
62+
* /cdk-lambda-apigateway/root-resource-id
63+
64+
### Step 3: product-api
65+
66+
```bash
67+
cd ../product-api
68+
cdk deploy
69+
```
70+
71+
[product-api/lib/product-api-stack.ts](./product-api/lib/product-api-stack.ts)
72+
73+
SSM parameter:
74+
75+
* /cdk-lambda-apigateway/rest-api-id
76+
* /cdk-lambda-apigateway/root-resource-id
77+
78+
### Reference
79+
80+
* Template body size: 512k
81+
* Template body size in an Amazon S3: 1M

app/hello.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
exports.handler = async function(event) {
2+
console.log("request:", JSON.stringify(event, undefined, 2));
3+
return {
4+
statusCode: 200,
5+
headers: { "Content-Type": "text/plain" },
6+
body: `API path: ${event.path} \n`
7+
};
8+
};

device-api/bin/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env node
2+
import * as cdk from 'aws-cdk-lib';
3+
import { DeviceApiStack } from '../lib/device-api-stack';
4+
import { DEFAULT_STAGE } from '../config';
5+
6+
7+
const app = new cdk.App();
8+
const env = {
9+
account: process.env.CDK_DEFAULT_ACCOUNT,
10+
region: process.env.CDK_DEFAULT_REGION
11+
};
12+
const stage = app.node.tryGetContext('stage') || DEFAULT_STAGE;
13+
14+
new DeviceApiStack(app, `apigw-device-api-${stage}`, {
15+
env,
16+
stage,
17+
description: `/device API`,
18+
terminationProtection: stage!==DEFAULT_STAGE
19+
});

device-api/build.gradle

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apply plugin: 'base'
2+
apply plugin: 'org.sonarqube'
3+
archivesBaseName = 'cdk-lambda-apigateway'
4+
5+
buildscript {
6+
repositories {
7+
mavenCentral()
8+
}
9+
dependencies {
10+
classpath("org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.5")
11+
}
12+
}
13+
14+
sonarqube {
15+
properties {
16+
property "sonar.projectName", "cdk-lambda-apigateway"
17+
property "sonar.projectKey", "cdk-lambda-apigateway"
18+
property "sonar.sourceEncoding", "UTF-8"
19+
property "sonar.sources", "."
20+
property "sonar.exclusions", "**/node_modules/**"
21+
property "sonar.cpd.exclusions", "**/*index.ts"
22+
property "sonar.links.ci", "https://github.com/engel80/cdk-lambda-apigateway"
23+
property "sonar.log.level", "DEBUG"
24+
}
25+
}

device-api/cdk.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"app": "npx ts-node --prefer-ts-exts bin/index.ts",
3+
"watch": {
4+
"include": [
5+
"**"
6+
],
7+
"exclude": [
8+
"README.md",
9+
"cdk*.json",
10+
"**/*.d.ts",
11+
"**/*.js",
12+
"tsconfig.json",
13+
"package*.json",
14+
"yarn.lock",
15+
"node_modules",
16+
"test"
17+
]
18+
},
19+
"context": {
20+
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
21+
"@aws-cdk/core:stackRelativeExports": true,
22+
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
23+
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
24+
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true,
25+
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
26+
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
27+
"@aws-cdk/core:target-partitions": [
28+
"aws",
29+
"aws-cn"
30+
]
31+
}
32+
}

device-api/config.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as cdk from 'aws-cdk-lib';
2+
3+
/**
4+
* /cdk-lambda-apigateway/vpc-id
5+
*
6+
* ecs-fargate-cluster:
7+
* /cdk-lambda-apigateway/cluster-capacityprovider-name
8+
* /cdk-lambda-apigateway/cluster-securitygroup-id
9+
*
10+
* iam-role:
11+
* /cdk-lambda-apigateway/task-execution-role-arn
12+
* /cdk-lambda-apigateway/default-task-role-arn
13+
*
14+
*/
15+
export const SSM_PREFIX = '/cdk-lambda-apigateway';
16+
17+
18+
export const DEFAULT_STAGE = 'dev';
19+
20+
export interface StackCommonProps extends cdk.StackProps {
21+
stage: string;
22+
}

device-api/jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
roots: ['<rootDir>/test'],
4+
testMatch: ['**/*.test.ts'],
5+
transform: {
6+
'^.+\\.tsx?$': 'ts-jest'
7+
}
8+
};

0 commit comments

Comments
 (0)