Skip to content

Commit 67bac9d

Browse files
authored
Merge pull request #21 from NASA-IMPACT/feature/update-requirements
Apply eoAPI updates
2 parents 6d9e816 + 3c42c0d commit 67bac9d

25 files changed

Lines changed: 1016 additions & 219 deletions

File tree

.dockerignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Ignore cdk folder
2+
cdk.out/
3+
.history
4+
.tox
5+
.git
6+
.vscode
7+
node_modules/
8+
.mypy_cache
9+
.pytest_cache

app.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@
99
from stac_api.infrastructure.construct import StacApiLambdaConstruct
1010
from raster_api.infrastructure.construct import RasterApiLambdaConstruct
1111

12-
identifier = os.getenv("IDENTIFIER").lower()
12+
stage = os.getenv("STAGE").lower()
1313
app_name = "delta-backend"
1414

1515
app = App()
1616

17-
1817
class DeltaStack(Stack):
1918
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
2019
super().__init__(scope, construct_id, **kwargs)
2120

2221

23-
delta_stack = DeltaStack(app, f"{app_name}-{identifier}")
22+
delta_stack = DeltaStack(app, f"{app_name}-{stage}")
2423

2524
vpc = VpcConstruct(delta_stack, "network")
2625

27-
database = RdsConstruct(delta_stack, "database", vpc.vpc)
26+
database = RdsConstruct(delta_stack, "database", vpc.vpc, stage=stage)
2827

2928
raster_api = RasterApiLambdaConstruct(
3029
delta_stack, "raster-api", vpc=vpc.vpc, database=database
@@ -40,7 +39,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
4039

4140
for key, value in {
4241
"Project": app_name,
43-
"Stack": identifier,
42+
"Stack": stage,
4443
"Client": "nasa-impact",
4544
"Owner": "ds",
4645
}.items():

cdk.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
{
22
"app": "python3 app.py",
33
"context": {
4-
"@aws-cdk/core:bootstrapQualifier": "toolkitv2"
4+
"@aws-cdk/core:bootstrapQualifier": "toolkitv2",
5+
"dev": {
6+
"pgstac_version": "0.4.3"
7+
},
8+
"prod": {
9+
"pgstac_version": "0.4.3"
10+
}
511
}
612
}

database/infrastructure/construct.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os
22
import json
3+
from platform import node
34

45
from aws_cdk import (
56
aws_ec2,
67
aws_lambda,
8+
aws_logs,
79
aws_rds,
810
aws_secretsmanager,
911
CfnOutput,
@@ -26,21 +28,27 @@ def __init__(
2628
database: aws_rds.DatabaseInstance,
2729
new_dbname: str,
2830
new_username: str,
29-
secrets_prefix: str
31+
secrets_prefix: str,
32+
stage: str,
3033
) -> None:
3134
super().__init__(scope, construct_id)
3235

36+
# get pgstac version from context
37+
pgstac_version = scope.node.try_get_context(stage)["pgstac_version"]
38+
3339
handler = aws_lambda.Function(
3440
self,
3541
"lambda",
3642
handler="handler.handler",
3743
runtime=aws_lambda.Runtime.PYTHON_3_8,
3844
code=aws_lambda.Code.from_docker_build(
3945
path=os.path.abspath("./"),
40-
file="database/runtime/Dockerfile"
46+
file="database/runtime/Dockerfile",
47+
build_args={"PGSTAC_VERSION": pgstac_version},
4148
),
4249
timeout=Duration.minutes(2),
43-
vpc=database.vpc
50+
vpc=database.vpc,
51+
log_retention=aws_logs.RetentionDays.ONE_WEEK,
4452
)
4553

4654
self.secret = aws_secretsmanager.Secret(
@@ -58,7 +66,7 @@ def __init__(
5866
}
5967
),
6068
generate_string_key="password",
61-
exclude_punctuation=True
69+
exclude_punctuation=True,
6270
),
6371
description=f"Pgstac database bootsrapped by {Stack.of(self).stack_name} stack"
6472
)
@@ -78,6 +86,9 @@ def __init__(
7886
id="bootstrapper",
7987
service_token=handler.function_arn,
8088
properties={
89+
# By setting pgstac_version in the properties assures
90+
# that Create/Update events will be passed to the service token
91+
"pgstac_version": pgstac_version,
8192
"conn_secret_arn": database.secret.secret_arn,
8293
"new_user_secret_arn": self.secret.secret_arn
8394
},
@@ -94,6 +105,7 @@ def __init__(
94105
scope: Construct,
95106
construct_id: str,
96107
vpc,
108+
stage: str,
97109
**kwargs
98110
) -> None:
99111
super().__init__(scope, construct_id, **kwargs)
@@ -117,10 +129,8 @@ def __init__(
117129
vpc_subnets=aws_ec2.SubnetSelection(
118130
subnet_type=aws_ec2.SubnetType.PUBLIC
119131
),
120-
deletion_protection=False, # TODO we do want deletion protection
121-
removal_policy=RemovalPolicy.DESTROY, # TODO we need a safe removal policy like snapshot
122-
# deletion_protection=identifier=="prod" , # enables deletion protection for production databases
123-
# removal_policy=RemovalPolicy.RETAIN if identifier == "prod" else RemovalPolicy.DESTROY, # TODO we need a safe removal policy like snapshot
132+
deletion_protection=stage=="prod" , # enables deletion protection for production databases
133+
removal_policy=RemovalPolicy.RETAIN if stage == "prod" else RemovalPolicy.DESTROY,
124134
publicly_accessible=True,
125135
)
126136

@@ -131,7 +141,8 @@ def __init__(
131141
database=database,
132142
new_dbname="postgis", # TODO this is config!
133143
new_username="delta", # TODO this is config!
134-
secrets_prefix=stack_name
144+
secrets_prefix=stack_name,
145+
stage=stage,
135146
)
136147

137148
CfnOutput(

database/runtime/Dockerfile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
FROM --platform=linux/amd64 lambci/lambda:build-python3.8
1+
FROM lambci/lambda:build-python3.8
2+
3+
ARG PGSTAC_VERSION
4+
RUN echo "Using PGSTAC Version ${PGSTAC_VERSION}"
25

36
WORKDIR /tmp
47

5-
RUN pip install "importlib_resources;python_version>='3.8'" requests psycopg2-binary pypgstac==0.3.4 -t /asset
8+
RUN pip install requests psycopg[binary] pypgstac==${PGSTAC_VERSION} -t /asset
69

710
COPY database/runtime/handler.py /asset/handler.py
811

12+
# https://stackoverflow.com/a/61746719
13+
# Tip from eoAPI: turns out, asyncio is part of python
14+
RUN rm -rf /asset/asyncio*
15+
916
CMD ["echo", "hello world database"]

0 commit comments

Comments
 (0)