-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IT-4241: create documentDB instance and allow connection from ECS API…
… container
- Loading branch information
Hallie Swan
committed
Jan 22, 2025
1 parent
6b898bb
commit 1f1bb23
Showing
8 changed files
with
206 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
aws-cdk-lib~=2.139 | ||
aws-cdk-lib~=2.176 | ||
constructs~=10.0 | ||
boto3~=1.34 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from aws_cdk import aws_ec2 as ec2 | ||
|
||
|
||
class DocdbProps: | ||
""" | ||
DocumentDB properties | ||
instance_type: What type of instance to start for the replicas | ||
master_username: The database admin account username | ||
""" | ||
|
||
def __init__(self, instance_type: ec2.InstanceType, master_username: str) -> None: | ||
self.instance_type = instance_type | ||
self.master_username = master_username |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import aws_cdk as cdk | ||
from aws_cdk import ( | ||
aws_docdb as docdb, | ||
aws_ec2 as ec2, | ||
aws_secretsmanager as sm, | ||
) | ||
from src.docdb_props import DocdbProps | ||
|
||
from constructs import Construct | ||
|
||
|
||
class DocdbStack(cdk.Stack): | ||
""" | ||
DocumentDB cluster | ||
""" | ||
|
||
def __init__( | ||
self, | ||
scope: Construct, | ||
construct_id: str, | ||
vpc: ec2.Vpc, | ||
props: DocdbProps, | ||
**kwargs, | ||
) -> None: | ||
super().__init__(scope, construct_id, **kwargs) | ||
|
||
self.master_password_secret = sm.Secret( | ||
self, | ||
f"{construct_id}-master-password", | ||
generate_secret_string=sm.SecretStringGenerator( | ||
password_length=32, exclude_punctuation=True | ||
), | ||
) | ||
|
||
self.access_docdb_security_group = ec2.SecurityGroup( | ||
self, | ||
f"{construct_id}-access-docdb-sg", | ||
vpc=vpc, | ||
description="Instances with access to document DB servers", | ||
) | ||
self.docdb_security_group = ec2.SecurityGroup( | ||
self, | ||
f"{construct_id}-docdb-sg", | ||
vpc=vpc, | ||
description="Document DB server management and access ports", | ||
) | ||
self.docdb_security_group.add_ingress_rule( | ||
peer=self.access_docdb_security_group, | ||
connection=ec2.Port.tcp_range(27017, 27030), | ||
) | ||
self.docdb_security_group.add_ingress_rule( | ||
peer=self.access_docdb_security_group, connection=ec2.Port.tcp(28017) | ||
) | ||
|
||
cluster_parameter_group = docdb.ClusterParameterGroup( | ||
self, | ||
"ClusterParameterGroup", | ||
family="docdb5.0", | ||
parameters={ | ||
"audit_logs": "disabled", | ||
"audit_logs": "disabled", | ||
"profiler": "enabled", | ||
"profiler_sampling_rate": "1.0", | ||
"profiler_threshold_ms": "50", | ||
"change_stream_log_retention_duration": "10800", | ||
"tls": "disabled", | ||
"ttl_monitor": "disabled", | ||
}, | ||
db_cluster_parameter_group_name=f"{construct_id}-cluster-parameter-group", | ||
) | ||
|
||
# https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_docdb/DatabaseCluster.html | ||
self.cluster = docdb.DatabaseCluster( | ||
self, | ||
"Database", | ||
master_user=docdb.Login( | ||
username=props.master_username, | ||
password=self.master_password_secret.secret_value, | ||
), | ||
instance_type=props.instance_type, | ||
vpc=vpc, | ||
vpc_subnets=ec2.SubnetSelection( | ||
subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS | ||
), | ||
parameter_group=cluster_parameter_group, | ||
removal_policy=cdk.RemovalPolicy.DESTROY, | ||
storage_encrypted=True, | ||
preferred_maintenance_window="sat:06:54-sat:07:24", | ||
port=27017, | ||
export_profiler_logs_to_cloud_watch=True, | ||
security_group=self.docdb_security_group, | ||
) | ||
|
||
self.cluster.add_security_groups(self.access_docdb_security_group) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import aws_cdk as cdk | ||
from aws_cdk import aws_ec2 as ec2, assertions as assertions | ||
|
||
from src.network_stack import NetworkStack | ||
from src.docdb_stack import DocdbStack | ||
from src.docdb_props import DocdbProps | ||
|
||
|
||
def test_docdb_created(): | ||
cdk_app = cdk.App() | ||
master_username = "myuser" | ||
vpc_cidr = "10.254.192.0/24" | ||
network_stack = NetworkStack(cdk_app, "NetworkStack", vpc_cidr=vpc_cidr) | ||
|
||
docdb_props = DocdbProps( | ||
instance_type=ec2.InstanceType.of( | ||
ec2.InstanceClass.MEMORY5, ec2.InstanceSize.LARGE | ||
), | ||
master_username=master_username, | ||
) | ||
docdb_stack = DocdbStack( | ||
scope=cdk_app, | ||
construct_id="docdb", | ||
vpc=network_stack.vpc, | ||
props=docdb_props, | ||
) | ||
|
||
template = assertions.Template.from_stack(docdb_stack) | ||
template.has_resource_properties( | ||
"AWS::DocDB::DBClusterParameterGroup", | ||
{ | ||
"Parameters": { | ||
"audit_logs": "disabled", | ||
"profiler": "enabled", | ||
"profiler_sampling_rate": "1.0", | ||
"profiler_threshold_ms": "50", | ||
"change_stream_log_retention_duration": "10800", | ||
"tls": "disabled", | ||
"ttl_monitor": "disabled", | ||
} | ||
}, | ||
) | ||
template.has_resource_properties( | ||
"AWS::DocDB::DBCluster", | ||
{ | ||
"MasterUsername": master_username, | ||
"MasterUserPassword": assertions.Match.any_value(), | ||
"DBSubnetGroupName": assertions.Match.any_value(), | ||
"DBClusterParameterGroupName": assertions.Match.any_value(), | ||
"StorageEncrypted": True, | ||
"PreferredMaintenanceWindow": "sat:06:54-sat:07:24", | ||
"Port": 27017, | ||
"EnableCloudwatchLogsExports": ["profiler"], | ||
"VpcSecurityGroupIds": [ | ||
assertions.Match.any_value(), | ||
assertions.Match.any_value(), | ||
], | ||
}, | ||
) | ||
template.resource_properties_count_is( | ||
"AWS::EC2::SecurityGroup", assertions.Match.any_value(), 2 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters