Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 5x 5x 5x 13x 13x 13x 7x 7x 7x 6x 6x 6x 6x 6x 6x 6x 9x 9x 6x 6x 13x 1x 1x 1x 5x 5x 5x 13x 1x 1x 1x 1x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 4x 1x 1x 1x 1x 3x 4x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 1x 1x 1x 1x 22x 1x 1x 21x 21x 21x 21x 21x 21x 21x 22x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 4x 4x 4x 21x 22x 1x 1x 21x 22x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 22x 2x 2x 2x 2x 22x 1x 1x 19x 19x 19x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 9x 9x 14x 14x 23x 23x 23x 23x 23x 5x 23x 9x 9x 5x 5x 5x 5x 5x 5x 23x 23x 23x 23x 23x 1x 1x 1x 23x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 23x 23x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 23x 13x 13x 13x 2x 2x 13x 11x 11x 11x 13x 22x 22x 23x 19x 19x 19x 19x 23x 23x 23x 23x 3x 3x 3x 3x 3x 3x 3x 23x | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { CloudFormationClient, DescribeStacksCommand } from "@aws-sdk/client-cloudformation";
import { CdkCustomResourceEvent } from "aws-lambda";
import { RAM_STABILIZATION_MS } from "../config/constants";
import {
batchAssociateAnalyticsDatasets,
batchDisassociateAnalyticsDatasets,
listAllAnalyticsDataAssociations,
listAnalyticsDataLakeDataSets,
} from "../services/connect-operations";
import {
createGlueDatabase,
createResourceLinkTables,
getTableNames,
deleteResourceLinkTables,
} from "../services/glue-operations";
import { setupLakeFormationPermissions } from "../services/lakeformation-operations";
import { acceptRamInvitations, RamResult, listRamSharedTables } from "../services/ram-operations";
import { CustomResourceProperties } from "../types/custom-resource-properties";
import { CustomResourceResponse } from "../types/custom-resource-response";
import { createAwsClients, AwsClients } from "../utils/aws-clients";
/**
* Disassociates datasets that are no longer in the custom resource event and removes the corresponding
* tables during an UPDATE event
*
* Only deletes resource linked tables for valid Connect datasets that are no longer
* associated with any instance.
*
* @param instanceId - Connect instance ID
* @param currentDatasetIds - Array of dataset IDs to exclude from resource removal
* @param targetAccountId - Target account ID where the datasets are associated
* @param clients - AWS service clients
* @returns Array of error messages encountered during resource removal
*/
async function removeOldResources(
instanceId: string,
currentDatasetIds: string[],
targetAccountId: string,
clients: AwsClients,
): Promise<string[]> {
const errors: string[] = [];
const activeDatasetAssociations = await listAllAnalyticsDataAssociations(clients.connect, targetAccountId);
const currentInstanceDatasets = activeDatasetAssociations.get(instanceId) || [];
const removedDatasets = currentInstanceDatasets.filter((dataset) => !currentDatasetIds.includes(dataset));
if (removedDatasets.length > 0) {
const disassociateDatasetsResult = await batchDisassociateAnalyticsDatasets(
instanceId,
removedDatasets,
targetAccountId,
clients.connect,
);
if (disassociateDatasetsResult.Errors.length > 0) {
errors.push(
`Dataset disassociation failures: ${disassociateDatasetsResult.Errors.map((e) => `${e.resourceId} - ${e.error}`).join("; ")}`,
);
}
await new Promise((resolve) => setTimeout(resolve, RAM_STABILIZATION_MS));
}
const allTableNames = await getTableNames(clients.glue);
if (allTableNames.length === 0) {
console.info("No resource linked tables found in Glue database");
return errors;
}
const sharedTableNames = await listRamSharedTables(clients.ram);
const validConnectDatasets = await listAnalyticsDataLakeDataSets(instanceId, clients.connect);
// Calculate tables to delete: all resource linked tables - shared tables - non-Connect datasets
const targetTables = new Set(
allTableNames.filter((tableName) => {
const datasetId = tableName.match(/^(.+)_\d{12}$/)?.[1];
return datasetId && !sharedTableNames.has(tableName) && validConnectDatasets.has(datasetId);
}),
);
if (targetTables.size === 0) {
console.info("No tables to delete from Glue database");
return errors;
}
const deleteTablesResult = await deleteResourceLinkTables(clients.glue, targetTables);
if (deleteTablesResult.Errors.length > 0) {
errors.push(
`Delete table failures: ${deleteTablesResult.Errors.map((e) => `${e.resourceId} - ${e.error}`).join("; ")}`,
);
}
return errors;
}
/**
* Handles expired RAM invitations by re-associating datasets
*
* When RAM invitations expire, this function disassociates and then re-associates
* all datasets to generate new invitations. All dataset associations for an expired invite
* will be included in datasetIds
*
* @param datasetIds - Array of dataset IDs with expired invitations
* @param instanceId - Connect instance ID
* @param targetAccountId - Target account ID for associations
* @param clients - AWS service clients
* @returns RamResult
*/
async function handleExpiredInvitations(
datasetIds: string[],
instanceId: string,
targetAccountId: string,
clients: AwsClients,
): Promise<RamResult> {
const result: RamResult = { acceptedInvitations: new Map(), errors: [] };
const disassociateDatasetsResult = await batchDisassociateAnalyticsDatasets(
instanceId,
datasetIds,
targetAccountId,
clients.connect,
);
if (disassociateDatasetsResult.Errors.length > 0) {
result.errors.push(...disassociateDatasetsResult.Errors.map((e) => `${e.resourceId} - ${e.error}`));
return result;
}
const associateDatasetsResult = await batchAssociateAnalyticsDatasets(
instanceId,
datasetIds,
targetAccountId,
clients.connect,
);
if (associateDatasetsResult.Errors.length > 0) {
result.errors.push(
`Retry association failures: ${associateDatasetsResult.Errors.map((e) => `${e.resourceId} - ${e.error}`).join("; ")}`,
);
}
if (associateDatasetsResult.Created.length === 0) {
result.errors.push(`Failed to re-associate expired datasets: ${datasetIds.join(", ")}`);
return result;
}
const ramInviteResult = await acceptRamInvitations(associateDatasetsResult.Created, clients.ram);
result.acceptedInvitations = ramInviteResult.acceptedInvitations;
result.errors.push(...ramInviteResult.errors);
return result;
}
/**
* Creates the required resources for Data Lake integration
*
* In the instance owner account:
* - Associates analytics datasets with target account
*
* In the target account:
* - Sets up Lake Formation permissions
* - Creates Glue database
* - Accepts RAM resource share invitations
* - Creates Glue resource link tables
*
* @param instanceId - Connect instance ID
* @param datasetIds - Array of dataset IDs to associate
* @param roleArn - IAM role ARN for Lake Formation permissions
* @param targetAccountId - Target account ID for data lake resources
* @param clients - AWS service clients
* @returns Array of error messages encountered during creation
*/
async function createResources(
instanceId: string,
datasetIds: string[],
roleArn: string,
targetAccountId: string,
clients: AwsClients,
): Promise<string[]> {
const errors: string[] = [];
const associateDatasetsResult = await batchAssociateAnalyticsDatasets(
instanceId,
datasetIds,
targetAccountId,
clients.connect,
);
if (associateDatasetsResult.Errors.length > 0) {
errors.push(
`Association failures: ${associateDatasetsResult.Errors.map((e) => `${e.resourceId} - ${e.error}`).join("; ")}`,
);
}
if (associateDatasetsResult.Created.length === 0) {
throw new Error("Failed to create any dataset associations");
}
await setupLakeFormationPermissions(roleArn, clients.lakeformation);
await createGlueDatabase(clients.glue);
const ramInviteResult = await acceptRamInvitations(associateDatasetsResult.Created, clients.ram);
if (ramInviteResult.expiredInvitations?.size) {
const expiredDatasets = Array.from(ramInviteResult.expiredInvitations.values()).flat();
const expiredRamInviteResult = await handleExpiredInvitations(
expiredDatasets,
instanceId,
targetAccountId,
clients,
);
expiredRamInviteResult.acceptedInvitations.forEach((datasets, arn) => {
ramInviteResult.acceptedInvitations.set(arn, datasets);
});
errors.push(...expiredRamInviteResult.errors);
}
if (ramInviteResult.errors.length > 0) {
errors.push(`RAM failures recorded: ${ramInviteResult.errors.join("; ")}`);
}
if (ramInviteResult.acceptedInvitations.size === 0) {
throw new Error("No RAM requests were accepted");
}
const acceptedDatasets = Array.from(ramInviteResult.acceptedInvitations.values()).flat();
// Retrieve the shared catalog ID from the accepted resource share ARN
const resourceShareArn = ramInviteResult.acceptedInvitations.keys().next().value!;
const sharedCatalogId = resourceShareArn.split(":")[4];
const createTablesResult = await createResourceLinkTables(acceptedDatasets, sharedCatalogId, clients.glue);
if (createTablesResult.Errors.length > 0) {
errors.push(
`Create table failures: ${createTablesResult.Errors.map((e) => `${e.resourceId} - ${e.error}`).join("; ")}`,
);
}
if (createTablesResult.Success.length === 0) {
throw new Error("No successful resource link tables created");
}
return errors;
}
/**
* Determines if the handler execution should be skipped for Update events.
* Execution will be skipped if the previous deployment was successful (no errors) and props have not changed
*
* @param event - The CDK custom resource event
* @returns true if execution should be skipped, false otherwise
*/
async function shouldSkipExecution(event: CdkCustomResourceEvent<CustomResourceProperties>): Promise<boolean> {
if (event.RequestType !== "Update") {
return false;
}
const { datasetIds, roleArn, instanceId, constructId } = event.ResourceProperties;
const oldProps = event.OldResourceProperties || {};
if (
JSON.stringify(oldProps.datasetIds) !== JSON.stringify(datasetIds) ||
oldProps.roleArn !== roleArn ||
oldProps.instanceId !== instanceId
) {
return false;
}
try {
const cloudFormationClient = new CloudFormationClient({ region: process.env.AWS_REGION });
const describeStackResponse = await cloudFormationClient.send(
new DescribeStacksCommand({ StackName: event.StackId }),
);
const stackOutputs = describeStackResponse.Stacks?.[0]?.Outputs || [];
const constructErrorOutput = stackOutputs.find((o) => o.OutputKey?.startsWith(`${constructId}Errors`));
return constructErrorOutput?.OutputValue === "None";
} catch (error) {
console.error("Could not read previous errors, proceeding with execution: ", error);
return false;
}
}
/**
* Handles CREATE and UPDATE operations for Connect Data Lake setup
*
* @param event - CloudFormation custom resource event
* @returns CustomResourceResponse
*/
export async function onCreateUpdate(
event: CdkCustomResourceEvent<CustomResourceProperties>,
): Promise<CustomResourceResponse> {
const skipExecution = await shouldSkipExecution(event);
if (skipExecution) {
console.info(`Update not required, skipping execution`);
return {
Status: "SUCCESS",
Data: { errors: "None" },
};
}
const { instanceId, datasetIds, roleArn, lambdaAccountId, targetAccountId } = event.ResourceProperties;
const errors: string[] = [];
try {
const clients = await createAwsClients(lambdaAccountId, targetAccountId, roleArn);
// Handle resource cleanup during UPDATE event
if (event.RequestType === "Update") {
const { instanceId: oldInstanceId } = event.OldResourceProperties || {};
if (oldInstanceId && instanceId !== oldInstanceId) {
console.info(`Removing resources for old instance ID`);
errors.push(...(await removeOldResources(oldInstanceId, [], targetAccountId, clients)));
} else {
console.info(`Removing resources for old dataset associations`);
errors.push(...(await removeOldResources(instanceId, datasetIds, targetAccountId, clients)));
}
}
// Continue with resource creation
errors.push(...(await createResources(instanceId, datasetIds, roleArn, targetAccountId, clients)));
return {
Status: "SUCCESS",
Data: {
errors: errors.length > 0 ? errors.join("; ") : "None",
},
};
} catch (error) {
return {
Status: "SUCCESS",
Data: {
errors: `Critical error during operations. ${(error as Error).message}`,
},
};
}
}
|