Skip to content

Commit

Permalink
get configs from context (#23)
Browse files Browse the repository at this point in the history
Setup context environments using cdk.json so that we can deploy
to different environments (dev/stage/prod)
  • Loading branch information
zaro0508 authored Jul 29, 2024
1 parent 0783b6f commit 71b4ed0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
AWS_LOADER_S3_SECRET_ACCESS_KEY=DUMMY_S3_SECRET_KEY
SECURITY_KEY=00000
EOT
- name: cdk synth
- name: cdk --context env=dev synth
uses: youyo/aws-cdk-github-actions@v2
with:
cdk_subcommand: 'synth'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ $ pip install -r requirements.txt -r requirements-dev.txt
At this point you can now synthesize the CloudFormation template for this code.

```
$ cdk synth
$ cdk --context env=dev synth
```

To add additional dependencies, for example other CDK libraries, just add
Expand Down Expand Up @@ -74,7 +74,7 @@ execute the validations by running `pre-commit run --all-files`.

Verify CDK to Cloudformation conversion by running [cdk synth]:
```text
cdk synth
cdk --context env=dev synth
```
The Cloudformation output is saved to the `cdk.out` folder

Expand Down
12 changes: 6 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
from openchallenges.load_balancer_stack import LoadBalancerStack
from openchallenges.service_props import ServiceProps

# get configs from file
# get secrets from file
configs = Properties()
with open(".openchallenges", "rb") as config_file:
configs.load(config_file)

DNS_NAMESPACE = "openchallenges.io"
VPC_CIDR = "10.255.92.0/24"

app = cdk.App()
env_context = app.node.try_get_context("dev")

bucket_stack = BucketStack(app, "openchallenges-buckets")
network_stack = NetworkStack(app, "openchallenges-network", VPC_CIDR)
ecs_stack = EcsStack(app, "openchallenges-ecs", network_stack.vpc, DNS_NAMESPACE)
network_stack = NetworkStack(app, "openchallenges-network", env_context["VPC_CIDR"])
ecs_stack = EcsStack(
app, "openchallenges-ecs", network_stack.vpc, env_context["DNS_NAMESPACE"]
)

mariadb_props = ServiceProps(
"openchallenges-mariadb",
Expand Down
6 changes: 5 additions & 1 deletion cdk.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
"@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse": true,
"@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true,
"@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope": true,
"@aws-cdk/aws-eks:nodegroupNameAttribute": true
"@aws-cdk/aws-eks:nodegroupNameAttribute": true,
"dev": {
"VPC_CIDR": "10.255.92.0/24",
"DNS_NAMESPACE": "openchallenges.io"
}
}
}
17 changes: 17 additions & 0 deletions tests/unit/test_network_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import aws_cdk as core
import aws_cdk.assertions as assertions

from openchallenges.network_stack import NetworkStack


def test_vpc_created():
context = {
"dev": {"VPC_CIDR": "10.255.92.0/24", "DNS_NAMESPACE": "openchallenges.io"}
}

app = core.App(context=context)
env_context = app.node.try_get_context("dev")
vpc_cidr = env_context["VPC_CIDR"]
network = NetworkStack(app, "NetworkStack", vpc_cidr)
template = assertions.Template.from_stack(network)
template.has_resource_properties("AWS::EC2::VPC", {"CidrBlock": vpc_cidr})
14 changes: 0 additions & 14 deletions tests/unit/test_service_stack.py

This file was deleted.

0 comments on commit 71b4ed0

Please sign in to comment.