Skip to content
Merged

. #3

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
163 changes: 151 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/
.gradle
.vscode
**/logs

# Package Files #
*.log
*.jar
*.war
*.nar
Expand All @@ -19,5 +13,150 @@
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# internal
*internal*

# MAC
**/.DS_Store

# Lambda layer
**/layers
**/temp

# CDK
**/node_modules
**/cdk.out
**/cdk.context.json

#-------------------

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
# CDK
#lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
83 changes: 81 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,81 @@
# cdk-lambda-apigateway
CDK Lambda API Gateway sample project
# Sample project for API Gateway and Lambda with CDK

To deploy without dependencies and CloudFormation limitation of Template body size(512k), create small API stacks for each Microservice or module.

![apigw](./screenshots/apigw.png?raw=true)

Table of Contents

1. Deploy root-api stack
2. Deploy device-api stack
3. Deploy product-api stack

## Prerequisites

```bash
npm install -g aws-cdk@2.25.0

# install packages in the root folder
npm install
cdk bootstrap
```

Use the `cdk` command-line toolkit to interact with your project:

* `cdk deploy`: deploys your app into an AWS account
* `cdk synth`: synthesizes an AWS CloudFormation template for your app
* `cdk diff`: compares your app with the deployed stack
* `cdk watch`: deployment every time a file change is detected

## CDK Stack

| Stack | Time |
|-------------------------------|---------|
| root-api (24kb) | 1m 30s |
| device-api (24kb) | 1m 30s |
| product-api (20kb) | 1m 30s |
| Total | 4m 30s |

### Step 1: root-api

Create the Root API and dummy method to import from device-api and product-api.

```bash
cd root-api
cdk deploy
```

[root-api/lib/root-api-stack.ts](./root-api/lib/root-api-stack.ts)

### Step 2: device-api

```bash
cd ../device-api
cdk deploy
```

[device-api/lib/device-api-stack.ts](./device-api/lib/device-api-stack.ts)

SSM parameter:

* /cdk-lambda-apigateway/rest-api-id
* /cdk-lambda-apigateway/root-resource-id

### Step 3: product-api

```bash
cd ../product-api
cdk deploy
```

[product-api/lib/product-api-stack.ts](./product-api/lib/product-api-stack.ts)

SSM parameter:

* /cdk-lambda-apigateway/rest-api-id
* /cdk-lambda-apigateway/root-resource-id

### Reference

* Template body size: 512k
* Template body size in an Amazon S3: 1M
8 changes: 8 additions & 0 deletions app/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.handler = async function(event) {
console.log("request:", JSON.stringify(event, undefined, 2));
return {
statusCode: 200,
headers: { "Content-Type": "text/plain" },
body: `API path: ${event.path} \n`
};
};
19 changes: 19 additions & 0 deletions device-api/bin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import { DeviceApiStack } from '../lib/device-api-stack';
import { DEFAULT_STAGE } from '../config';


const app = new cdk.App();
const env = {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION
};
const stage = app.node.tryGetContext('stage') || DEFAULT_STAGE;

new DeviceApiStack(app, `apigw-device-api-${stage}`, {
env,
stage,
description: `/device API`,
terminationProtection: stage!==DEFAULT_STAGE
});
25 changes: 25 additions & 0 deletions device-api/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apply plugin: 'base'
apply plugin: 'org.sonarqube'
archivesBaseName = 'cdk-lambda-apigateway'

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.5")
}
}

sonarqube {
properties {
property "sonar.projectName", "cdk-lambda-apigateway"
property "sonar.projectKey", "cdk-lambda-apigateway"
property "sonar.sourceEncoding", "UTF-8"
property "sonar.sources", "."
property "sonar.exclusions", "**/node_modules/**"
property "sonar.cpd.exclusions", "**/*index.ts"
property "sonar.links.ci", "https://github.com/engel80/cdk-lambda-apigateway"
property "sonar.log.level", "DEBUG"
}
}
32 changes: 32 additions & 0 deletions device-api/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"app": "npx ts-node --prefer-ts-exts bin/index.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"
]
}
}
22 changes: 22 additions & 0 deletions device-api/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as cdk from 'aws-cdk-lib';

/**
* /cdk-lambda-apigateway/vpc-id
*
* ecs-fargate-cluster:
* /cdk-lambda-apigateway/cluster-capacityprovider-name
* /cdk-lambda-apigateway/cluster-securitygroup-id
*
* iam-role:
* /cdk-lambda-apigateway/task-execution-role-arn
* /cdk-lambda-apigateway/default-task-role-arn
*
*/
export const SSM_PREFIX = '/cdk-lambda-apigateway';


export const DEFAULT_STAGE = 'dev';

export interface StackCommonProps extends cdk.StackProps {
stage: string;
}
8 changes: 8 additions & 0 deletions device-api/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'
}
};
Loading