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 | 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 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 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 1x 1x 6x 6x 1x 1x 6x 6x 6x 6x 3x 6x 1x 1x 6x 6x 1x 1x 6x 1x 1x 6x 6x 6x 6x 3x 6x 1x 1x 6x 6x 3x 3x 3x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 1x | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { join } from "path";
import { Duration, CustomResource, CfnOutput, Stack, Annotations, Token } from "aws-cdk-lib";
import { PolicyStatement, Effect } from "aws-cdk-lib/aws-iam";
import { Function, Runtime, Code } from "aws-cdk-lib/aws-lambda";
import { Provider } from "aws-cdk-lib/custom-resources";
import { Construct } from "constructs";
import { DataType } from "./data-types";
export * from "./data-types";
/**
* Properties for the DataLakeAccess construct
*/
export interface DataLakeAccessProps {
/**
* The identifier of the Amazon Connect instance
*/
readonly instanceId: string;
/**
* Array of dataset IDs to associate with the Connect instance
*
* Can include DataType enum values or string dataset IDs
*/
readonly datasetIds: Array<string | DataType>;
/**
* The identifier of the target account
*
* If not specified, defaults to the current AWS account. For cross-account setups,
* specify an external account ID to associate datasets and create resources in that account
*
* When specified with an external account, `targetAccountRoleArn` is also required
*/
readonly targetAccountId?: string;
/**
* The IAM role ARN in the target account for cross-account role assumption
*
* Only required when `targetAccountId` is specified with an external account. A template
* for the required permissions can be found in the [Cross Account Setup](../CROSS_ACCOUNT_SETUP.md) documentation
*/
readonly targetAccountRoleArn?: string;
}
/**
* Automates Amazon Connect Data Lake integration setup and management.
*
* This construct simplifies the process of associating Amazon Connect analytics datasets with AWS data lake services,
* handling resource sharing, Lake Formation permissions, and data catalog management through a centralized Glue database.
*/
export class DataLakeAccess extends Construct {
private static readonly DATABASE_NAME = "connect_datalake_database";
/**
* @param scope Parent to which the Custom Resource belongs
* @param id Unique identifier for this instance
* @param props Metadata for configuring the Custom Resource
*/
constructor(scope: Construct, id: string, props: DataLakeAccessProps) {
super(scope, id);
const stack = Stack.of(this);
const partition = stack.partition;
const region = stack.region;
const account = stack.account;
const stackName = stack.stackName;
const targetAccount: string = props.targetAccountId ?? account;
const isCrossAccountDeployment: boolean = targetAccount !== account;
this.validateProps(props, isCrossAccountDeployment);
// Used to manage change in targetAccountId scenarios
const targetAccountIdSuffix = isCrossAccountDeployment ? `${props.targetAccountId}` : "";
/**
* Lambda function that handles the data lake setup and teardown process
* This function orchestrates the entire Connect data lake integration workflow
*/
const dataLakeLambda = new Function(this, `DataLakeHandler${targetAccountIdSuffix}`, {
runtime: Runtime.NODEJS_22_X,
handler: "index.handler",
code: Code.fromAsset(join(__dirname, "..", "lib", "handler")),
memorySize: 512,
timeout: Duration.minutes(15),
});
const roleArn = isCrossAccountDeployment ? props.targetAccountRoleArn! : dataLakeLambda.role!.roleArn;
this.configureLambdaPermissions(
dataLakeLambda,
partition,
region,
account,
stackName,
isCrossAccountDeployment,
roleArn,
);
/**
* Custom resource provider that manages the Lambda function lifecycle
*/
const dataLakeProvider = new Provider(this, `DataLakeProvider${targetAccountIdSuffix}`, {
onEventHandler: dataLakeLambda,
});
/**
* Custom resource that triggers the data lake setup process during CloudFormation stack lifecycle events
*/
const dataLakeCustomResource = new CustomResource(this, `DataLakeCustomResource${targetAccountIdSuffix}`, {
serviceToken: dataLakeProvider.serviceToken,
properties: {
instanceId: props.instanceId,
datasetIds: props.datasetIds.map((d) => String(d)),
roleArn: roleArn,
lambdaAccountId: account,
targetAccountId: targetAccount,
timeStamp: Date.now().toString(), // Force update even if input properties do not change
constructId: id,
},
});
/**
* CloudFormation output containing custom resource lambda errors
*/
new CfnOutput(this, "Errors", {
value: dataLakeCustomResource.getAttString("errors"),
exportName: `${id}Errors`,
});
Annotations.of(this).addWarning(
`DataLakeAccess reports setup errors via the stack output. ` +
`Check output '${id}Errors' after each deployment to verify the setup completed successfully.`,
);
}
/**
* Validates the construct input parameters
*
* @param props The construct properties to validate
* @param isCrossAccountDeployment Indicates if target account differs from current account
*/
private validateProps(props: DataLakeAccessProps, isCrossAccountDeployment: boolean): void {
const errors: string[] = [];
if (
!Token.isUnresolved(props.instanceId) &&
!/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/.test(props.instanceId)
) {
errors.push("instanceId must be a valid UUID format");
}
if (props.datasetIds.length === 0) {
errors.push("datasetIds cannot be empty");
}
if (
props.targetAccountId &&
!Token.isUnresolved(props.targetAccountId) &&
!/^\d{12}$/.test(props.targetAccountId)
) {
errors.push("targetAccountId must be a 12-digit AWS account ID");
}
if (isCrossAccountDeployment && !props.targetAccountRoleArn) {
errors.push("targetAccountRoleArn is required when targetAccountId differs from current account");
}
if (!isCrossAccountDeployment && props.targetAccountRoleArn) {
errors.push("targetAccountRoleArn should not be specified without targetAccountId");
}
if (
props.targetAccountRoleArn &&
!Token.isUnresolved(props.targetAccountRoleArn) &&
!/^arn:(aws|aws-us-gov):iam::\d{12}:role\/[\w+=,.@/-]+$/.test(props.targetAccountRoleArn)
) {
errors.push("targetAccountRoleArn must be a valid IAM role ARN");
}
if (errors.length > 0) {
errors.forEach((error) => Annotations.of(this).addError(error));
throw new Error(`DataLakeAccess validation failed: ${errors.join("; ")}`);
}
}
/**
* Configures Lambda permissions for same-account or cross-account deployments
*
* @param lambdaFunction The Lambda function to configure permissions for
* @param partition AWS partition for resource ARN construction
* @param region AWS region for resource ARN construction
* @param account Current account ID where the Connect instance resides
* @param stackName Name of the CloudFormation stack
* @param isCrossAccountDeployment Indicates if target account differs from current account
* @param roleArn Role ARN used for cross-account deployments
*/
private configureLambdaPermissions(
lambdaFunction: Function,
partition: string,
region: string,
account: string,
stackName: string,
isCrossAccountDeployment: boolean,
roleArn: string,
): void {
lambdaFunction.addToRolePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: [
"connect:BatchAssociateAnalyticsDataSet",
"connect:BatchDisassociateAnalyticsDataSet",
"connect:AssociateAnalyticsDataSet",
"connect:DisassociateAnalyticsDataSet",
"connect:ListAnalyticsDataAssociations",
"connect:ListAnalyticsDataLakeDataSets",
],
resources: [`arn:${partition}:connect:${region}:${account}:instance/*`],
}),
);
lambdaFunction.addToRolePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["connect:ListInstances"],
resources: [`arn:${partition}:connect:${region}:${account}:*`],
}),
);
lambdaFunction.addToRolePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["ds:DescribeDirectories"],
resources: [`*`],
}),
);
lambdaFunction.addToRolePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["cloudformation:DescribeStacks"],
resources: [`arn:${partition}:cloudformation:${region}:${account}:stack/${stackName}/*`],
}),
);
// Allow assuming ConnectDataLakeRole for cross-account operations
if (isCrossAccountDeployment) {
lambdaFunction.addToRolePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["sts:AssumeRole"],
resources: [roleArn],
}),
);
} else {
lambdaFunction.addToRolePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["ram:GetResourceShareInvitations", "ram:GetResourceShares", "ram:ListResources"],
resources: ["*"],
}),
);
lambdaFunction.addToRolePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["ram:AcceptResourceShareInvitation"],
resources: [`arn:${partition}:ram:${region}:*:resource-share-invitation/*`],
}),
);
lambdaFunction.addToRolePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["lakeformation:GetDataLakeSettings", "lakeformation:PutDataLakeSettings"],
resources: ["*"],
}),
);
lambdaFunction.addToRolePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: [
"glue:GetDatabase",
"glue:CreateDatabase",
"glue:DeleteDatabase",
"glue:CreateTable",
"glue:DeleteTable",
"glue:GetTables",
],
resources: [
`arn:${partition}:glue:${region}:${account}:database/${DataLakeAccess.DATABASE_NAME}`,
`arn:${partition}:glue:${region}:${account}:table/${DataLakeAccess.DATABASE_NAME}/*`,
`arn:${partition}:glue:${region}:${account}:userDefinedFunction/${DataLakeAccess.DATABASE_NAME}/*`,
`arn:${partition}:glue:${region}:${account}:catalog`,
],
}),
);
}
}
}
|