Skip to content

Instantly share code, notes, and snippets.

View JelsB's full-sized avatar

Jels Boulangier JelsB

View GitHub Profile
from my_pipeline.configuration import RawConfig, AppConfig, ResourceConfig
# ...
class PipelineStack(core.Stack):
def __init__(self, scope: core.Construct, pipeline_id: str, raw_config: RawConfig,
app_config: AppConfig, **kwargs: Any) -> None:
super().__init__(scope, pipeline_id, **kwargs)
# File where you can extend base configuration classes
from dataclasses import dataclass
from my_cdk_package.config.base import BaseAppConfig, BaseResourceConfig
@dataclass
class AppConfig(BaseAppConfig):
"""Custom application config class."""
optional_custom_app_config: str
class MyStack(core.Stack):
"""Example stack."""
def __init__(self, scope: Construct, id: str, config: ResourceConfig, **kwargs: Any):
super().__init__(scope, id, env=config.environment, **kwargs)
# create resources
synth_action = SimpleSynthAction(
cloud_assembly_artifact=cloud_assembly_artifact,
source_artifact=source_artifact,
install_commands=['npm run ci_synth'],
build_commands=['npm run ci_build'],
install_commands=[
# ...
'npm run ci_type_check'
]
# ...
synth_action = SimpleSynthAction(
# ...
install_commands=[
# typo in install
f'npm insatll -g aws-cdk@{cdk_version}',
# ...
],
# ...
)
from pathlib import Path
from aws_cdk import core
from my_pipeline.configuration import RawConfig, AppConfig
from my_pipeline.pipeline_stack import PipelineStack
# Read config file
config_file = Path('config.json')
# Process raw config file
raw_config = RawConfig(config_file)
# Create application config instance
{
"application": {
"application_name": "<application_name>",
"repository_name": "<name_of_the_CodeCommit_mirror_repo>",
"branch": "master",
"build_environment": {
"account": "<account-id-of-build-account>",
"region": "<build-aws-region>"
}
// ...
class RawConfig:
"""
Raw JSON configuration of the application and of all infrastructure resources for
each environment and.
"""
def __init__(self, config_file: Path):
self._all_config: Any = self._read_config(config_file)
self._environments_config: Any = self._all_config['environments']
self._default: Any = self._environments_config['default']
from dataclasses import dataclass
from typing import Any, Dict, Type, TypeVar
from aws_cdk.core import Environment, RemovalPolicy
# https://mypy.readthedocs.io/en/stable/kinds_of_types.html#the-type-of-class-objects
# https://github.com/python/typing/issues/58
# Needed to return correct child class types in classmethod constructor
ResourceConfigClass = TypeVar('ResourceConfigClass', bound='ResourceConfig')
vpc = ec2.Vpc.from_vpc_attributes(self, 'vpc',
vpc_id=vpc_id,
availability_zones=list_of_vpc_availability_zones,
private_subnet_ids=list_of_private_subnet_ids
)
vpc_subnets = ec2.SubnetSelection(subnets=vpc.private_subnets)
# ...
alb = elbv2.ApplicationLoadBalancer(self, 'alb-name',