Skip to main content

A CDK Python Construct for AWS IoT Infrastructure

Project description

multacdkrecipies - Custom CDK Constructs for rapid development

multacdkrecipies logo

  • 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

  1. Install with pip
    • $ pip install multacdkrecipies

Usage

  • config.py
"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
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
from aws_cdk import core
from stack import ApiStack

app = core.App()
ApiStack(app, id=f"GatewayApiStack-dev")

app.synth()

How to Contribute

  1. Clone repo and create a new branch: $ git checkout https://github.com/u93/multacdkrecipies -b ${BRANCH_NAME}.
  2. Make changes and test
  3. 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.1.0.tar.gz (31.4 kB view details)

Uploaded Source

File details

Details for the file multacdkrecipies-0.1.0.tar.gz.

File metadata

  • Download URL: multacdkrecipies-0.1.0.tar.gz
  • Upload date:
  • Size: 31.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5

File hashes

Hashes for multacdkrecipies-0.1.0.tar.gz
Algorithm Hash digest
SHA256 af2a5c3e695ae3fa69fe6eb15b833859a8a0ef6fda5c6b21cf65da1daf2bc944
MD5 2ee66dd1fa9435d2a79185e245d0a38a
BLAKE2b-256 ee23c3d4b4a2ffe8739cc45b2ba1e0b7b9e92e88210aaef28d570a6c49aa983a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page