我正在使用 cdk 在跨账户上下文中部署 Sagemaker 端点。
创建 Sagemaker 端点时出现以下错误: 无法从 URL“s3://.../model.tar.gz”下载容器“container_1”的模型数据。请确保该 URL 处存在一个对象,并且传递给 CreateModel 的角色具有下载该对象的权限。
这里有一些有用的细节。
我有两个帐户:
- 账户 A:包括保存模型工件的加密 s3 存储桶、具有最新批准版本的 Sagemaker 模型包组以及在账户 A 本身和账户 B 中部署端点的 CodePipeline。
- 账户 B:包括账户 A 中 CodePipeline 部署的端点。
在帐户A中:
- 为存储桶和用于加密该存储桶的 kms key 设置跨账户权限
// Create bucket and kms key to be used by Sagemaker Pipeline
//KMS
const sagemakerKmsKey = new Key(
this,
"SagemakerBucketKMSKey",
{
description: "key used for encryption of data in Amazon S3",
enableKeyRotation: true,
policy: new PolicyDocument(
{
statements:[
new PolicyStatement(
{
actions:["kms:*"],
effect: Effect.ALLOW,
resources:["*"],
principals: [new AccountRootPrincipal()]
}
),
new PolicyStatement(
{
actions:[
"kms:*"
],
effect: Effect.ALLOW,
resources:["*"],
principals: [
new ArnPrincipal(`arn:${Aws.PARTITION}:iam::${AccountA}:root`),
new ArnPrincipal(`arn:${Aws.PARTITION}:iam::${AccountB}:root`),
]
}
)
]
}
)
}
)
// S3 Bucket
const sagemakerArtifactBucket = new Bucket(
this,
"SagemakerArtifactBucket",
{
bucketName:`mlops-${projectName}-${Aws.REGION}`,
encryptionKey:sagemakerKmsKey,
versioned:false,
removalPolicy: RemovalPolicy.DESTROY
}
)
sagemakerArtifactBucket.addToResourcePolicy(
new PolicyStatement(
{
actions: [
"s3:*",
],
resources: [
sagemakerArtifactBucket.bucketArn,
`${sagemakerArtifactBucket.bucketArn}/*`
],
principals: [
new ArnPrincipal(`arn:${Aws.PARTITION}:iam::${AccountA}:root`),
new ArnPrincipal(`arn:${Aws.PARTITION}:iam::${AccountB}:root`),
]
}
)
)
- CodeDeploy 操作用于在 AccountA 和 AccountB 中部署 Sagemaker 端点。
// Define Code Build Deploy Staging Action
const deployStagingAction = new CloudFormationCreateUpdateStackAction(
{
actionName: "DeployStagingAction",
runOrder: 1,
adminPermissions: false,
stackName: `${projectName}EndpointStaging`,
templatePath: cdKSynthArtifact.atPath("staging.template.json"),
replaceOnFailure: true,
role: Role.fromRoleArn(
this,
"StagingActionRole",
`arn:${Aws.PARTITION}:iam::${AccountB}:role/cdk-hnb659fds-deploy-role-${AccountB}-${Aws.REGION}`,
),
deploymentRole: Role.fromRoleArn(
this,
"StagingDeploymentRole",
`arn:${Aws.PARTITION}:iam::${AccountB}:role/cdk-hnb659fds-cfn-exec-role-${AccountB}-${Aws.REGION}`
),
cfnCapabilities: [
CfnCapabilities.AUTO_EXPAND,
CfnCapabilities.NAMED_IAM
]
}
)
具体来说,创建 Sagemaker 模型和 Sagemaker 端点的角色应该是 cdk-hnb659fds-cfn-exec-role,如 CloudTrail 上所示,但出于测试目的,我已向它们授予管理员权限(错误仍然存在)出现)。
AccountA中的部署执行正确,说明Bucket位置正确。
注意:一切都已正确部署到 Sagemaker 端点。
最佳答案
我设法找到了问题。
问题是,即使存储桶是使用自定义 KMSKey 创建的,存储到存储桶中的工件也是由 Estimator 生成的。 。如果您不指定 output_kms_key 参数,它将使用托管 kms key ,该 key 与用于 s3 存储桶的 key 不同。
尽管该问题与跨帐户权限无关,但我会将其保留在这里,以防有人遇到类似问题。
关于typescript - 部署跨账户 Sagemaker 端点时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73027227/