Skip to content

Instantly share code, notes, and snippets.

@youngsoul
youngsoul / lambda_cdk_example.py
Created March 11, 2022 22:30
Lambda CDK Example
lb1 = lambda_alpha_.PythonFunction(self, f"{resource_prefix}-function",
entry="./rds_lambda",
index="rds_lambda.py",
handler="lambda_handler",
runtime=_lambda.Runtime.PYTHON_3_9,
vpc=vpc,
vpc_subnets=ec2.SubnetSelection(
subnet_type=ec2.SubnetType.PRIVATE_WITH_NAT
),
security_groups=[
bastion_host = ec2.Instance(self, id=f'{resource_prefix}-bastion-host',
instance_type=ec2.InstanceType(instance_type_identifier='t2.micro'),
machine_image=ec2.AmazonLinuxImage(
edition=ec2.AmazonLinuxEdition.STANDARD,
generation=ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
virtualization=ec2.AmazonLinuxVirt.HVM,
storage=ec2.AmazonLinuxStorage.GENERAL_PURPOSE
),
vpc=vpc,
key_name='pryan-aws', # must create the key name manually first
@youngsoul
youngsoul / mysql_db_cdk_example.py
Last active March 11, 2022 19:46
Example of creating a MySQL database
rds_instance = rds.DatabaseInstance(self,
id=f'{resource_prefix}-DBInstance',
engine=rds.DatabaseInstanceEngine.mysql(
version=rds.MysqlEngineVersion.VER_8_0_16),
credentials=rds.Credentials.from_secret(db_credentials_secret),
instance_type=
ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
vpc=vpc,
vpc_subnets=ec2.SubnetSelection(
subnet_type=ec2.SubnetType.PRIVATE_ISOLATED
@youngsoul
youngsoul / vpc_secrets_cdk_example.py
Created March 11, 2022 19:39
CDK example of creating a database password secret
secrets_template = json.dumps({
"username": f'{db_username}'
})
db_credentials_secret = secrets.Secret(self, id=f'{resource_prefix}-DBCredentialsSecret',
secret_name=construct_id + '-rds-credentials',
generate_secret_string=secrets.SecretStringGenerator(
secret_string_template=secrets_template,
exclude_punctuation=True,
include_space=False,
generate_string_key="password"
@youngsoul
youngsoul / vpc_securitygroups_cdk_example.py
Last active March 11, 2022 19:19
Security Group CDK Example
lambda_to_proxy_sg = ec2.SecurityGroup(self, id=f'{resource_prefix}-l2proxy-sg',
vpc=vpc,
allow_all_outbound=True,
description="Lambda to RDS Proxy Connection"
)
bastion_sg = ec2.SecurityGroup(self, id='bastionsg',
security_group_name=f'{resource_prefix}-cdk-bastion-sg',
vpc=vpc,
description=f'{resource_prefix} SG for Bastion',
allow_all_outbound=True)
@youngsoul
youngsoul / vpc_cdk_example.py
Created March 11, 2022 17:46
VPC Python CDK Example
vpc = ec2.Vpc(self, f'{resource_prefix}-Vpc',
cidr='10.40.0.0/16',
nat_gateways=1,
max_azs=2,
enable_dns_hostnames=True,
enable_dns_support=True,
subnet_configuration=[
ec2.SubnetConfiguration(
name=f"{resource_prefix}-Public-subnet",
subnet_type=ec2.SubnetType.PUBLIC,
@youngsoul
youngsoul / lambda_scikit.py
Last active March 7, 2022 20:19
Example lambda using pandas and scikit_learn
import json
import logging
import os
import pandas as pd
import joblib
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
mount_dir = "/mnt/model"
@youngsoul
youngsoul / cdk_http_example.py
Created March 7, 2022 20:03
Http API CDK example
api = api_gw.HttpApi(self, f'{resources_prefix}-Test API Lambda',
default_integration=integrations.HttpLambdaIntegration(id="lb1-lambda-proxy",
handler=lb1)
)
@youngsoul
youngsoul / lambda_cdk_example.py
Last active March 7, 2022 19:58
Lambda CDK Example using V2 API
lb1 = lambda_alpha_.PythonFunction(self, f"{resources_prefix}-heartfailure-function",
entry="./ml_lambda",
index="heart_failure_lambda.py",
handler="lambda_handler",
runtime=_lambda.Runtime.PYTHON_3_7,
vpc=self.vpc,
security_groups=[
self.efs_access_sg
],
timeout=Duration.seconds(300),
@youngsoul
youngsoul / efs_cdk_example.py
Last active March 7, 2022 19:49
CDK example setting up EFS
fs = efs.FileSystem(self, f'{resources_prefix}-FileSystem',
vpc=self.vpc,
security_group=self.efs_access_sg,
removal_policy=RemovalPolicy.DESTROY)
access_point = fs.add_access_point(f'{resources_prefix}-LambdaAccessPoint',
create_acl=efs.Acl(owner_gid='1000', owner_uid='1000', permissions='755'),
path="/export/lambda",
posix_user=efs.PosixUser(gid="1000", uid="1000"))