Skip to main content

A CDK Python Construct for AWS IoT Infrastructure

Project description

multacdkrecipies - Custom CDK Constructs for rapid development

multacdkrecipies logo

Versions Downloads Maintenance Python 3.7 PRs Welcome PyPI license

  • 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
    • 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

  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.1.tar.gz (32.1 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: multacdkrecipies-0.1.1.tar.gz
  • Upload date:
  • Size: 32.1 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.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for multacdkrecipies-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3d0a57f3a9c1f0c2399e99bc2e4813ceadab11319ef2e36d061badb0d7bfb0e4
MD5 d989c84faf66d10f4aab7625bbea0c07
BLAKE2b-256 eeb14dbdc00ef8ad27ab696c6d69d3e5ff9898edfacb08f0da66f618f783abff

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