A CDK Python Construct for AWS IoT Infrastructure
Project description
multacdkrecipies - Custom CDK Constructs for rapid development
- Designed to facilitate AWS CDK usage in Python CDK Apps by creating high-level constructs that help development in Serverless Applications.
- Designed to be used like a regular Python CDK application.
- Designed to expose resources over Python Classes properties, keeping flexibility in the CDK application.
- Designed to configure constructs by passing settings in Python dictionaries.
Installation
- Install with
pip
$ pip install multacdkrecipies
Usage
config.py
- Set configuration for the multacdkrecipies CDK Constructs.
"LAMBDA_LAYER_CONFIG": {
"identifier": "api_gateway",
"layer_name": "api_gateway_venv_layer",
"description": "Lambda Layer containing local Python's Virtual Environment needed for the handler functions.",
"layer_runtimes": ["PYTHON_3_7"],
}
API_CONFIG = {
"api": {
"apigateway_name": "device_gateway",
"apigateway_description": "API Gateway used for Multa Device Agents to be associated to the AWS IoT",
"authorizer_function": {
"origin": {
"lambda_name": "authorizer",
"description": "Authorizer Lambda function for API resources.",
"code_path": "./src/functions/",
"runtime": "PYTHON_3_7",
"handler": "authorizer.lambda_handler",
"layers": [],
"timeout": 10,
"environment_vars": {
"LOG_LEVEL": "INFO",
},
"iam_actions": ["*"],
}
},
"settings": {
"proxy": False,
"custom_domain": {
"domain_name": "cvm-agent.dev.multa.io",
"certificate_arn": "arn:aws:acm:us-east-1:112646120612:certificate/48e19da0-71a4-417a-9247-c02ef100749c",
},
"default_cors_options": {"allow_origins": ["*"], "options_status_code": 200},
"default_http_methods": ["GET"],
"default_stage_options": {"metrics_enabled": True, "logging_level": "INFO"},
"default_handler": {
"lambda_name": "device_default_handler",
"description": "Handler Lambda for API Gateway root resource.",
"code_path": "./src/functions/",
"runtime": "PYTHON_3_7",
"handler": "main_handler.lambda_handler",
"layers": [],
"timeout": 10,
"environment_vars": {
"LOG_LEVEL": "INFO"
},
"iam_actions": ["*"],
},
},
"resource_trees": [
{
"resource_name": "demo",
"methods": ["POST"],
"handler": {
"lambda_name": "device_gateway_handler",
"description": "Handler Lambda for API Gateway demo resource.",
"code_path": "./src/functions/",
"runtime": "PYTHON_3_7",
"handler": "demo_handler.lambda_handler",
"layers": [],
"timeout": 10,
"environment_vars": {
"LOG_LEVEL": "INFO",
},
"iam_actions": ["*"],
},
},
],
}
}
stack.py
- Create the CDK App Stack by using multacdkrecipies CDK Constructs with the configuration defined above.
from aws_cdk import core
from multacdkrecipies import (
AwsApiGatewayLambdaPipes,
AwsLambdaLayerVenv,
)
from config import API_CONFIG, LAMBDA_LAYER_CONFIG
class ApiStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, config=None, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Define Lambda Layer to be used by the API Resources Lambda handlers.
self._device_gateway_api_lambdalayer = AwsLambdaLayerVenv(
self,
id="GatewayApiLayer-dev",
prefix="gateway_api",
environment="dev",
configuration=LAMBDA_LAYER_CONFIG,
)
layer_arn = self._device_gateway_api_lambdalayer.lambda_layer.layer_version_arn
# Add Lambda Layer ARN to the Lambda Functions configuration.
API_CONFIG["api"]["authorizer_function"]["origin"]["layers"].append(layer_arn)
API_CONFIG["api"]["settings"]["default_handler"]["layers"].append(layer_arn)
API_CONFIG["api"]["resource_trees"]:
function["handler"]["layers"].append(layer_arn)
# Define Lambda Functions handlers for the API Gateway resources.
self._gateway_api = AwsApiGatewayLambdaPipes(
self,
id="GatewayApiGw-dev",
prefix="gateway_api",
environment="dev",
configuration=API_CONFIG,
)
app.py
- Initialize the CDK App like a regular CDK App.
from aws_cdk import core
from stack import ApiStack
app = core.App()
ApiStack(app, id=f"GatewayApiStack-dev")
app.synth()
How to Contribute
- Clone repo and create a new branch:
$ git checkout https://github.com/u93/multacdkrecipies -b ${BRANCH_NAME}
. - Make changes and test
- Submit Pull Request with comprehensive description of changes
Acknowledgements
- @sfernandezf for all the help and introduce me to AWS.
- Mauricio Villaescusa for introduce me to CDK and listen all my dummy ideas.
- @destradar93 and @yoya93 for all the help testing and using this project.
Donations
This is free, open-source software, so no need to donate anything except knowledge... Contributions are good enough :)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
multacdkrecipies-0.2.0.tar.gz
(33.8 kB
view details)
File details
Details for the file multacdkrecipies-0.2.0.tar.gz
.
File metadata
- Download URL: multacdkrecipies-0.2.0.tar.gz
- Upload date:
- Size: 33.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad106b595217420ee0bebac9c6203bd6412d06ed9f2b725a578bbb1d92cdd47f |
|
MD5 | a7139fe7ab0191bf01831047fa0f57cf |
|
BLAKE2b-256 | 39e4a88520f7785105e4983432a0193cb80c6060743a460a070ee9633b8cc03d |