|
| 1 | +from aws_cdk import CfnOutput, Duration, Stack |
| 2 | +from aws_cdk import aws_cleanrooms as cleanrooms |
| 3 | +from aws_cdk import aws_glue_alpha as glue |
| 4 | +from aws_cdk import aws_iam as iam |
| 5 | +from aws_cdk import aws_s3 as s3 |
| 6 | +from aws_cdk import aws_ssm as ssm |
| 7 | +from constructs import Construct |
| 8 | + |
| 9 | + |
| 10 | +class CleanRoomsStack(Stack): # type: ignore |
| 11 | + def __init__( |
| 12 | + self, |
| 13 | + scope: Construct, |
| 14 | + construct_id: str, |
| 15 | + **kwargs: str, |
| 16 | + ) -> None: |
| 17 | + super().__init__(scope, construct_id, **kwargs) |
| 18 | + |
| 19 | + self.collaboration = cleanrooms.CfnCollaboration( |
| 20 | + self, |
| 21 | + "Collaboration", |
| 22 | + name="AWS SDK for pandas - Testing", |
| 23 | + creator_display_name="Collaborator Creator", |
| 24 | + creator_member_abilities=["CAN_QUERY", "CAN_RECEIVE_RESULTS"], |
| 25 | + description="Collaboration Room for AWS SDK for pandas test infrastructure", |
| 26 | + members=[], |
| 27 | + query_log_status="ENABLED", |
| 28 | + ) |
| 29 | + |
| 30 | + self.membership = cleanrooms.CfnMembership( |
| 31 | + self, |
| 32 | + "Membership", |
| 33 | + collaboration_identifier=self.collaboration.attr_collaboration_identifier, |
| 34 | + query_log_status="ENABLED", |
| 35 | + ) |
| 36 | + |
| 37 | + self.cleanrooms_service_role = iam.Role( |
| 38 | + self, |
| 39 | + "Service Role", |
| 40 | + assumed_by=iam.CompositePrincipal( |
| 41 | + iam.ServicePrincipal("cleanrooms.amazonaws.com").with_conditions( |
| 42 | + { |
| 43 | + "StringLike": { |
| 44 | + "sts:ExternalId": f"arn:aws:*:{self.region}:*:dbuser:*/{self.membership.attr_membership_identifier}*" |
| 45 | + } |
| 46 | + } |
| 47 | + ), |
| 48 | + iam.ServicePrincipal("cleanrooms.amazonaws.com").with_conditions( |
| 49 | + { |
| 50 | + "ForAnyValue:ArnEquals": { |
| 51 | + "aws:SourceArn": f"arn:aws:cleanrooms:{self.region}:{self.account}:membership/{self.membership.attr_membership_identifier}" |
| 52 | + } |
| 53 | + } |
| 54 | + ), |
| 55 | + ), |
| 56 | + managed_policies=[ |
| 57 | + iam.ManagedPolicy.from_aws_managed_policy_name("service-role/AWSGlueServiceRole"), |
| 58 | + iam.ManagedPolicy.from_aws_managed_policy_name("AmazonS3ReadOnlyAccess"), |
| 59 | + ], |
| 60 | + ) |
| 61 | + |
| 62 | + self.bucket = s3.Bucket( |
| 63 | + self, |
| 64 | + "Bucket", |
| 65 | + block_public_access=s3.BlockPublicAccess( |
| 66 | + block_public_acls=True, |
| 67 | + block_public_policy=True, |
| 68 | + ignore_public_acls=True, |
| 69 | + restrict_public_buckets=True, |
| 70 | + ), |
| 71 | + lifecycle_rules=[ |
| 72 | + s3.LifecycleRule( |
| 73 | + id="CleaningUp", |
| 74 | + enabled=True, |
| 75 | + expiration=Duration.days(1), |
| 76 | + abort_incomplete_multipart_upload_after=Duration.days(1), |
| 77 | + ), |
| 78 | + ], |
| 79 | + versioned=True, |
| 80 | + ) |
| 81 | + |
| 82 | + self.database = glue.Database( |
| 83 | + self, |
| 84 | + id="Glue Database", |
| 85 | + database_name="aws_sdk_pandas_cleanrooms", |
| 86 | + location_uri=f"s3://{self.bucket.bucket_name}", |
| 87 | + ) |
| 88 | + |
| 89 | + self.users_table = glue.Table( |
| 90 | + self, |
| 91 | + "Users Table", |
| 92 | + database=self.database, |
| 93 | + table_name="users", |
| 94 | + columns=[ |
| 95 | + glue.Column(name="user_id", type=glue.Type(input_string="int", is_primitive=True)), |
| 96 | + glue.Column(name="city", type=glue.Type(input_string="string", is_primitive=True)), |
| 97 | + ], |
| 98 | + bucket=self.bucket, |
| 99 | + s3_prefix="users", |
| 100 | + data_format=glue.DataFormat( |
| 101 | + input_format=glue.InputFormat("org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat"), |
| 102 | + output_format=glue.OutputFormat("org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat"), |
| 103 | + serialization_library=glue.SerializationLibrary( |
| 104 | + "org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe" |
| 105 | + ), |
| 106 | + ), |
| 107 | + ) |
| 108 | + |
| 109 | + self.purchases_table = glue.Table( |
| 110 | + self, |
| 111 | + "Purchases Table", |
| 112 | + database=self.database, |
| 113 | + table_name="purchases", |
| 114 | + columns=[ |
| 115 | + glue.Column(name="purchase_id", type=glue.Type(input_string="int", is_primitive=True)), |
| 116 | + glue.Column(name="user_id", type=glue.Type(input_string="int", is_primitive=True)), |
| 117 | + glue.Column(name="sale_value", type=glue.Type(input_string="float", is_primitive=True)), |
| 118 | + ], |
| 119 | + bucket=self.bucket, |
| 120 | + s3_prefix="purchases", |
| 121 | + data_format=glue.DataFormat( |
| 122 | + input_format=glue.InputFormat("org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat"), |
| 123 | + output_format=glue.OutputFormat("org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat"), |
| 124 | + serialization_library=glue.SerializationLibrary( |
| 125 | + "org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe" |
| 126 | + ), |
| 127 | + ), |
| 128 | + ) |
| 129 | + |
| 130 | + self.users_configured_table = cleanrooms.CfnConfiguredTable( |
| 131 | + self, |
| 132 | + "Users Configured Table", |
| 133 | + allowed_columns=["user_id", "city"], |
| 134 | + analysis_method="DIRECT_QUERY", |
| 135 | + name="users", |
| 136 | + table_reference=cleanrooms.CfnConfiguredTable.TableReferenceProperty( |
| 137 | + glue=cleanrooms.CfnConfiguredTable.GlueTableReferenceProperty( |
| 138 | + database_name=self.database.database_name, |
| 139 | + table_name=self.users_table.table_name, |
| 140 | + ) |
| 141 | + ), |
| 142 | + analysis_rules=[ |
| 143 | + cleanrooms.CfnConfiguredTable.AnalysisRuleProperty( |
| 144 | + policy=cleanrooms.CfnConfiguredTable.ConfiguredTableAnalysisRulePolicyProperty( |
| 145 | + v1=cleanrooms.CfnConfiguredTable.ConfiguredTableAnalysisRulePolicyV1Property( |
| 146 | + aggregation=cleanrooms.CfnConfiguredTable.AnalysisRuleAggregationProperty( |
| 147 | + aggregate_columns=[ |
| 148 | + cleanrooms.CfnConfiguredTable.AggregateColumnProperty( |
| 149 | + column_names=["user_id"], function="COUNT" |
| 150 | + ) |
| 151 | + ], |
| 152 | + dimension_columns=["city"], |
| 153 | + join_columns=["user_id"], |
| 154 | + output_constraints=[ |
| 155 | + cleanrooms.CfnConfiguredTable.AggregationConstraintProperty( |
| 156 | + column_name="user_id", minimum=2, type="COUNT_DISTINCT" |
| 157 | + ) |
| 158 | + ], |
| 159 | + scalar_functions=["LOWER"], |
| 160 | + join_required="QUERY_RUNNER", |
| 161 | + ), |
| 162 | + ) |
| 163 | + ), |
| 164 | + type="AGGREGATION", |
| 165 | + ) |
| 166 | + ], |
| 167 | + ) |
| 168 | + |
| 169 | + self.purchases_configured_table = cleanrooms.CfnConfiguredTable( |
| 170 | + self, |
| 171 | + "Purchases Configured Table", |
| 172 | + allowed_columns=["purchase_id", "user_id", "sale_value"], |
| 173 | + analysis_method="DIRECT_QUERY", |
| 174 | + name="purchases", |
| 175 | + table_reference=cleanrooms.CfnConfiguredTable.TableReferenceProperty( |
| 176 | + glue=cleanrooms.CfnConfiguredTable.GlueTableReferenceProperty( |
| 177 | + database_name=self.database.database_name, |
| 178 | + table_name=self.purchases_table.table_name, |
| 179 | + ) |
| 180 | + ), |
| 181 | + analysis_rules=[ |
| 182 | + cleanrooms.CfnConfiguredTable.AnalysisRuleProperty( |
| 183 | + policy=cleanrooms.CfnConfiguredTable.ConfiguredTableAnalysisRulePolicyProperty( |
| 184 | + v1=cleanrooms.CfnConfiguredTable.ConfiguredTableAnalysisRulePolicyV1Property( |
| 185 | + aggregation=cleanrooms.CfnConfiguredTable.AnalysisRuleAggregationProperty( |
| 186 | + aggregate_columns=[ |
| 187 | + cleanrooms.CfnConfiguredTable.AggregateColumnProperty( |
| 188 | + column_names=["purchase_id"], function="COUNT" |
| 189 | + ), |
| 190 | + cleanrooms.CfnConfiguredTable.AggregateColumnProperty( |
| 191 | + column_names=["sale_value"], function="AVG" |
| 192 | + ), |
| 193 | + cleanrooms.CfnConfiguredTable.AggregateColumnProperty( |
| 194 | + column_names=["sale_value"], function="SUM" |
| 195 | + ), |
| 196 | + ], |
| 197 | + dimension_columns=[], |
| 198 | + join_columns=["user_id"], |
| 199 | + output_constraints=[ |
| 200 | + cleanrooms.CfnConfiguredTable.AggregationConstraintProperty( |
| 201 | + column_name="user_id", minimum=2, type="COUNT_DISTINCT" |
| 202 | + ) |
| 203 | + ], |
| 204 | + scalar_functions=[], |
| 205 | + join_required="QUERY_RUNNER", |
| 206 | + ), |
| 207 | + ) |
| 208 | + ), |
| 209 | + type="AGGREGATION", |
| 210 | + ) |
| 211 | + ], |
| 212 | + ) |
| 213 | + |
| 214 | + self.users_configured_table_association = cleanrooms.CfnConfiguredTableAssociation( |
| 215 | + self, |
| 216 | + "Users Configured Table Association", |
| 217 | + configured_table_identifier=self.users_configured_table.attr_configured_table_identifier, |
| 218 | + membership_identifier=self.membership.attr_membership_identifier, |
| 219 | + name="users", |
| 220 | + role_arn=self.cleanrooms_service_role.role_arn, |
| 221 | + ) |
| 222 | + |
| 223 | + self.purchases_configured_table_association = cleanrooms.CfnConfiguredTableAssociation( |
| 224 | + self, |
| 225 | + "Purchases Configured Table Association", |
| 226 | + configured_table_identifier=self.purchases_configured_table.attr_configured_table_identifier, |
| 227 | + membership_identifier=self.membership.attr_membership_identifier, |
| 228 | + name="purchases", |
| 229 | + role_arn=self.cleanrooms_service_role.role_arn, |
| 230 | + ) |
| 231 | + |
| 232 | + CfnOutput(self, "CleanRoomsMembershipId", value=self.membership.attr_membership_identifier) |
| 233 | + CfnOutput(self, "CleanRoomsGlueDatabaseName", value=self.database.database_name) |
| 234 | + CfnOutput(self, "CleanRoomsS3BucketName", value=self.bucket.bucket_name) |
| 235 | + |
| 236 | + ssm.StringParameter( |
| 237 | + self, |
| 238 | + "SSM BucketName", |
| 239 | + parameter_name="/sdk-pandas/cleanrooms/BucketName", |
| 240 | + string_value=self.bucket.bucket_name, |
| 241 | + ) |
0 commit comments