-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from Cameronsplaze/optimization/combine-hosted…
…-zones [optimization/combine hosted zones] Moved HostedZone from Leaf Stack, to Base Stack
- Loading branch information
Showing
17 changed files
with
310 additions
and
254 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Base Stack Summary | ||
|
||
This is common architecture between leaf-stacks, combined to reduce costs and complexity. | ||
|
||
## Base Stack Main ([main.py](./main.py)) | ||
|
||
Deployed to the same region as you want to run the containers from. | ||
|
||
- **VPC**: The overall network for all the containers and EFS. We used a public VPC, because private cost ~$32/month per subnet (because of the NAT). WITH ec2 costs, I want to shoot for less than $100/year with solid usage. | ||
- **SSH Key Pair**: The key pair to SSH into the EC2 instances. Keeping it here lets you get into all the leaf_stacks without having to log into AWS each time you deploy a new leaf. If you destroy and re-build the leaf, this keeps the key consistent too. | ||
- **SNS Notify Logic**: Designed for things admin would care about. This tells you whenever the instance spins up or down, if it runs into errors, etc. | ||
|
||
## Base Stack Domain ([domain.py](./domain.py)) | ||
|
||
Deployed to `us-east-1`, since Route53 logs can only go there. | ||
|
||
- **Route53 HostedZone**: The base domain for all the leaf stacks. This is where the DNS records will be created for each leaf stack. The leaf stacks add their DNS record to this, and watch this log group for when their specific DNS record gets a query. |
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,7 @@ | ||
""" | ||
The different components of the base stack, broken | ||
apart since they're in different regions. | ||
""" | ||
|
||
from .main import BaseStackMain | ||
from .domain import BaseStackDomain |
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,97 @@ | ||
|
||
""" | ||
This module contains the BaseStackDomain class. | ||
""" | ||
|
||
from constructs import Construct | ||
from aws_cdk import ( | ||
Stack, | ||
RemovalPolicy, | ||
aws_route53 as route53, | ||
aws_logs as logs, | ||
aws_iam as iam, | ||
) | ||
|
||
# from cdk_nag import NagSuppressions | ||
|
||
|
||
class BaseStackDomain(Stack): | ||
""" | ||
Contains shared resources for all leaf stacks. | ||
Most importantly, the hosted zone. | ||
""" | ||
def __init__( | ||
self, | ||
scope: Construct, | ||
construct_id: str, | ||
config: dict, | ||
**kwargs, | ||
) -> None: | ||
super().__init__(scope, construct_id, **kwargs) | ||
|
||
|
||
##################### | ||
### Route53 STUFF ### | ||
##################### | ||
### These are also imported to other stacks, so save them here: | ||
self.domain_name = config["Domain"]["Name"] | ||
## The instance isn't up, use the "unknown" ip address: | ||
# https://www.lifewire.com/four-zero-ip-address-818384 | ||
self.unavailable_ip = "0.0.0.0" | ||
## Never set TTL to 0, it's not defined in the standard | ||
# (Since the container is constantly changing, update DNS asap) | ||
self.dns_ttl = 1 | ||
self.record_type = route53.RecordType.A | ||
|
||
|
||
## Log group for the Route53 DNS logs: | ||
# https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_logs.LogGroup.html | ||
self.route53_query_log_group = logs.LogGroup( | ||
self, | ||
"QueryLogGroup", | ||
log_group_name=f"/aws/route53/{construct_id}-query-logs", | ||
# Only need logs to trigger the lambda, don't need long-term: | ||
retention=logs.RetentionDays.ONE_DAY, | ||
removal_policy=RemovalPolicy.DESTROY, | ||
) | ||
## You can't grant direct access after creating the sub_hosted_zone, since it needs to | ||
# write to the log group when you create the zone. AND you can't do a wildcard arn, since the | ||
# account number isn't in the arn. | ||
self.route53_query_log_group.grant_write(iam.ServicePrincipal("route53.amazonaws.com")) | ||
|
||
## The subdomain for the Hosted Zone: | ||
# https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_route53.PublicHostedZone.html | ||
self.hosted_zone = route53.PublicHostedZone( | ||
self, | ||
"HostedZone", | ||
zone_name=self.domain_name, | ||
query_logs_log_group_arn=self.route53_query_log_group.log_group_arn, | ||
comment=f"{construct_id}: DNS query for all containers.", | ||
) | ||
|
||
## If you bought a domain through AWS, and have an existing Hosted Zone. We can't | ||
# modify it, so we import it and tie ours to the existing one: | ||
if config["Domain"]["HostedZoneId"]: | ||
## Import the existing Route53 Hosted Zone: | ||
# https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_route53.PublicHostedZoneAttributes.html | ||
self.imported_hosted_zone = route53.PublicHostedZone.from_hosted_zone_attributes( | ||
self, | ||
"RootHostedZone", | ||
hosted_zone_id=config["Domain"]["HostedZoneId"], | ||
zone_name=self.domain_name, | ||
) | ||
else: | ||
# This is checked in the leaf stack, to see if it needs to add | ||
# a NS record to this hosted zone. | ||
self.imported_hosted_zone = None | ||
|
||
##################### | ||
### Export Values ### | ||
##################### | ||
## To stop cdk from trying to delete the exports when cdk is deployed by | ||
## itself, but still has leaf stacks attached to it. | ||
# https://blogs.thedevs.co/aws-cdk-export-cannot-be-deleted-as-it-is-in-use-by-stack-5c205b8004b4 | ||
self.export_value(self.hosted_zone.hosted_zone_name_servers) | ||
self.export_value(self.route53_query_log_group.log_group_arn) | ||
self.export_value(self.hosted_zone.hosted_zone_id) | ||
self.export_value(self.route53_query_log_group.log_group_name) |
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
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
Oops, something went wrong.