|
| 1 | +import aws_cdk as cdk |
| 2 | + |
| 3 | +from aws_cdk import ( |
| 4 | + Stack, |
| 5 | + aws_lakeformation |
| 6 | +) |
| 7 | +from constructs import Construct |
| 8 | + |
| 9 | +class DataLakePermissionsStack(Stack): |
| 10 | + |
| 11 | + def __init__(self, scope: Construct, construct_id: str, glue_job_role, **kwargs) -> None: |
| 12 | + super().__init__(scope, construct_id, **kwargs) |
| 13 | + |
| 14 | + glue_job_input_arguments = self.node.try_get_context('glue_kinesis_table') |
| 15 | + database_name = glue_job_input_arguments["database_name"] |
| 16 | + |
| 17 | + #XXXX: The role assumed by cdk is not a data lake administrator. |
| 18 | + # So, deploying PrincipalPermissions meets the error such as: |
| 19 | + # "Resource does not exist or requester is not authorized to access requested permissions." |
| 20 | + # In order to solve the error, it is necessary to promote the cdk execution role to the data lake administrator. |
| 21 | + # For example, https://github.com/aws-samples/data-lake-as-code/blob/mainline/lib/stacks/datalake-stack.ts#L68 |
| 22 | + cfn_data_lake_settings = aws_lakeformation.CfnDataLakeSettings(self, "CfnDataLakeSettings", |
| 23 | + admins=[aws_lakeformation.CfnDataLakeSettings.DataLakePrincipalProperty( |
| 24 | + data_lake_principal_identifier=cdk.Fn.sub(self.synthesizer.cloud_formation_execution_role_arn) |
| 25 | + )] |
| 26 | + ) |
| 27 | + |
| 28 | + cfn_principal_permissions = aws_lakeformation.CfnPrincipalPermissions(self, "CfnPrincipalPermissions", |
| 29 | + permissions=["SELECT", "INSERT", "DELETE", "DESCRIBE", "ALTER"], |
| 30 | + permissions_with_grant_option=[], |
| 31 | + principal=aws_lakeformation.CfnPrincipalPermissions.DataLakePrincipalProperty( |
| 32 | + data_lake_principal_identifier=glue_job_role.role_arn |
| 33 | + ), |
| 34 | + resource=aws_lakeformation.CfnPrincipalPermissions.ResourceProperty( |
| 35 | + #XXX: Can't specify a TableWithColumns resource and a Table resource |
| 36 | + table=aws_lakeformation.CfnPrincipalPermissions.TableResourceProperty( |
| 37 | + catalog_id=cdk.Aws.ACCOUNT_ID, |
| 38 | + database_name=database_name, |
| 39 | + # name="ALL_TABLES", |
| 40 | + table_wildcard={} |
| 41 | + ) |
| 42 | + ) |
| 43 | + ) |
| 44 | + cfn_principal_permissions.apply_removal_policy(cdk.RemovalPolicy.DESTROY) |
| 45 | + |
| 46 | + #XXX: In order to keep resource destruction order, |
| 47 | + # set dependency between CfnDataLakeSettings and CfnPrincipalPermissions |
| 48 | + cfn_principal_permissions.add_dependency(cfn_data_lake_settings) |
| 49 | + |
| 50 | + cdk.CfnOutput(self, f'{self.stack_name}_Principal', |
| 51 | + value=cfn_principal_permissions.attr_principal_identifier) |
0 commit comments