11import os
22import json
3+ from platform import node
34
45from 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 (
0 commit comments