Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IT-4243: fix region lookup #21

Merged
merged 6 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,29 @@
from src.bastion_props import BastionProps
from src.bastion_stack import BastionStack

# Define stacks
cdk_app = cdk.App()

# get the environment and set environment specific variables
VALID_ENVIRONMENTS = ["dev", "stage", "prod"]
environment = environ.get("ENV")
region = cdk_app.region
hallieswan marked this conversation as resolved.
Show resolved Hide resolved
match environment:
case "prod":
environment_variables = {
"VPC_CIDR": "10.254.174.0/24",
"FQDN": "prod.agora.io",
"CERTIFICATE_ARN": f"arn:aws:acm:{region}:681175625864:certificate/69b3ba97-b382-4648-8f94-a250b77b4994",
"CERTIFICATE_ID": "69b3ba97-b382-4648-8f94-a250b77b4994",
"TAGS": {"CostCenter": "Agora / 112300"},
}
case "stage":
environment_variables = {
"VPC_CIDR": "10.254.173.0/24",
"FQDN": "stage.agora.io",
"CERTIFICATE_ARN": f"arn:aws:acm:{region}:681175625864:certificate/69b3ba97-b382-4648-8f94-a250b77b4994",
"CERTIFICATE_ID": "69b3ba97-b382-4648-8f94-a250b77b4994",
"TAGS": {"CostCenter": "Agora / 112300"},
}
case "dev":
environment_variables = {
"VPC_CIDR": "10.254.172.0/24",
"FQDN": "dev.agora.io",
"CERTIFICATE_ARN": f"arn:aws:acm:{region}:607346494281:certificate/e8093404-7db1-4042-90d0-01eb5bde1ffc",
"CERTIFICATE_ID": "e8093404-7db1-4042-90d0-01eb5bde1ffc",
"TAGS": {"CostCenter": "Agora / 112300"},
}
case _:
Expand All @@ -56,6 +52,9 @@
mongodb_port = 27017
vpn_cidr = "10.1.0.0/16"

# Define stacks
cdk_app = cdk.App()

# recursively apply tags to all stack resources
if environment_tags:
for key, value in environment_tags.items():
Expand Down Expand Up @@ -176,7 +175,7 @@
cluster=ecs_stack.cluster,
props=apex_props,
load_balancer=load_balancer_stack.alb,
certificate_arn=environment_variables["CERTIFICATE_ARN"],
certificate_id=environment_variables["CERTIFICATE_ID"],
health_check_path="/health",
)
apex_stack.add_dependency(app_stack)
Expand All @@ -186,7 +185,6 @@
key_name="agora-ci",
instance_type=ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MICRO),
ami_id="ami-074a6fac5773fe883",
ami_region=region,
)
bastion_stack = BastionStack(
scope=cdk_app,
Expand Down
3 changes: 0 additions & 3 deletions src/bastion_props.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class BastionProps:
key_name: Name of an existing EC2 KeyPair to enable SSH access to the instance
instance_type: The EC2 instance type
ami_id: AMI ID of the base AMI
ami_region: Region of the AMI to deploy
block_devices: Optional block devices to override the base AMI settings
"""

Expand All @@ -18,13 +17,11 @@ def __init__(
key_name: str,
instance_type: ec2.InstanceType,
ami_id: str,
ami_region: str,
hallieswan marked this conversation as resolved.
Show resolved Hide resolved
block_devices: Optional[Sequence[ec2.BlockDevice]] = None,
) -> None:
self.key_name = key_name
self.instance_type = instance_type
self.ami_id = ami_id
self.ami_region = ami_region
if block_devices is None:
self.block_devices = []
else:
Expand Down
4 changes: 3 additions & 1 deletion src/bastion_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ def __init__(
self, "BastionKeyPair", props.key_name
)

region_json = cdk.CfnJson(self, "AmiRegionJson", value=self.region)

# https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_ec2/Instance.html
self.instance = ec2.Instance(
self,
"BastionHost",
instance_type=props.instance_type,
machine_image=ec2.MachineImage.generic_linux(
{props.ami_region: props.ami_id}
{region_json.to_string(): props.ami_id}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you can reference with self.region in LoadBalancedServiceStack then you can probably do the same here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using self.region as the key in a map causes this error -- RuntimeError: Error: "${Token[AWS.Region.11]}" is used as the key in a map so must resolve to a string, but it resolves to: {"Ref":"AWS::Region"}. Consider using "CfnJson" to delay resolution to deployment-time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wacko! i'm not a fan of CfnJson workaround but it works so let's just move on for now.

),
vpc=vpc,
key_pair=key_pair,
Expand Down
5 changes: 4 additions & 1 deletion src/service_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def __init__(
cluster: ecs.Cluster,
props: ServiceProps,
load_balancer: elbv2.ApplicationLoadBalancer,
certificate_arn: str,
certificate_id: str,
health_check_path: str = "/",
health_check_interval: int = 1, # max is 5
**kwargs,
Expand All @@ -227,6 +227,9 @@ def __init__(
# -------------------
# ACM Certificate for HTTPS
# -------------------
certificate_arn = (
f"arn:aws:acm:{self.region}:{self.account}:certificate/{certificate_id}"
)
self.cert = acm.Certificate.from_certificate_arn(
self, "Cert", certificate_arn=certificate_arn
)
Expand Down
1 change: 0 additions & 1 deletion tests/unit/test_bastion_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def test_bastion_created():
key_name=key_name,
instance_type=ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MICRO),
ami_id="ami-074a6fac5773fe883",
ami_region="us-east-1",
)
bastion_stack = BastionStack(
scope=cdk_app,
Expand Down