A collection of AWS CDK constructs and utils written in python
Project description
Custom AWS CDK Constructs
Contains a set of higher level AWS CDK constructs.
see also https://github.com/aws/aws-cdk
Following components are available:
- ALB
Support for HTTPS is implemented. There are additional methods to register target groups of type EC2 and lambda. Create a simple ALB on port 443 with security groups and with "401 Access denied" fix response as default action:
from awscdk_components.elb.alb_https import (
AlbHttpsConstruct,
AlbCfg,
add_access_denied_fix_response
)
# create the config
app = core.App()
stack = GenericTestStack(app, 'test-stack')
alb_cfg = AlbCfg(
alb_name='TestALB',
vpc=stack.vpc,
subnets=stack.subnets,
certificate_arns=['arn:aws:acm:us-east-1:023475735288:certificate/ff6967d7-0fdf-4967-bd68-4caffc983447'],
cidr_ingress_ranges=['10.0.0.0/16'],
icmp_ranges=['10.0.0.0/16']
)
alb_construct = AlbHttpsConstruct(stack, 'albhttps', alb_cfg)
add_access_denied_fix_response('fix401resp', alb_construct.https_listener)
To add a target group for a given EC2 instance, accessible under /ec2 path, which has a service run also on port 443 (change the port parameter if necessary, i.e. port=8443):
from awscdk_components.elb.alb_utils import (
register_ec2_as_alb_target
)
alb_construct = AlbHttpsConstruct(stack, 'albhttps', alb_cfg)
ec2 = aws_ec2.Instance(
scope=stack,
id='ec2foralb',
vpc=stack.vpc,
instance_type=aws_ec2.InstanceType(instance_type_identifier='t3.micro'),
machine_image=aws_ec2.MachineImage.latest_amazon_linux()
)
register_ec2_as_alb_target(
stack,
ec2=ec2,
listener=alb_construct.https_listener,
vpc=stack.vpc,
path_pattern_values=['/ec2'],
port=443
)
add_access_denied_fix_response('fix401resp', alb_construct.https_listener)
More complicated utility method is registering lambda function behind authentication with AWS Cognito rule (currently the low level Cfn constructs for the UserPool are implemented only):
from awscdk_components.elb.alb_utils import (
register_lambda_target_group_with_cognito_auth_rule
)
alb_construct = AlbHttpsConstruct(stack, 'albhttps', alb_cfg)
function = aws_lambda.Function(
stack,
"lambda_function",
runtime=aws_lambda.Runtime.PYTHON_3_7,
handler="index.handler",
code=aws_lambda.Code.from_inline(
"def handler(event, context): return { 'statusCode': 200, 'body': 'Lambda was invoked successfully.' }"
),
vpc=stack.vpc
)
register_lambda_target_group_with_cognito_auth_rule(
scope=stack,
fn=function,
vpc=stack.vpc,
listener=alb_construct.https_listener,
user_pool=user_pool_cfn,
user_pool_app_client=user_pool_app_client_cfn,
user_pool_domain=user_pool_domain_cfn,
path_pattern_values=['/mylambda', '/mylambda/*']
)
add_access_denied_fix_response('fix401resp', alb_construct.https_listener)
For more details see the unittests in the tests package.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for rbi-oss-awscdk-components-pkg-0.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | af7fa82488a6061eae5156047cd92822278a2fd2881da53e5496f6c8bd160377 |
|
MD5 | fcd555465ba80f48c06c7639c16b0237 |
|
BLAKE2b-256 | 1ed64125d7b6351fa61f09422b29613dd8ecb16fe9445bbfb597357eec00a1f2 |
Hashes for rbi_oss_awscdk_components_pkg-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a6a9d973d2f2f1256decbf606dcc4e7531bff3ad9db4738ea96fbbc99d7957a |
|
MD5 | 645e139b116039688c5ae8961d59a162 |
|
BLAKE2b-256 | 1c44d106ae1775961ff3e866d5c3d244b66dc7356ea9f8df8a9a6bd404655c62 |