我正在尝试使用 S3 中的模型工件中的 CDK [python] 部署 sagemaker 端点。
Sagemaker模型需要execution_rol_arn。因此,我使用 CDK 创建了一个角色,并将其作为 sagemaker 模型的参数传递。但创建模型时提示角色不存在。 但如果通过这个命令添加对资源的依赖
sagemaker_model.add_depends_on(model_role)
。它给了我这个错误。
type of argument target must be aws_cdk.CfnResource; got aws_cdk.aws_iam.Role instead
我的 sagemaker 模型和 Iam 角色的 cdk 代码
sagemaker_model = aws_sagemaker.CfnModel(
self,
model_name,
execution_role_arn=model_role.role_arn,
model_name=model_name,
primary_container=sagemaker_primary_container_definition,
)
model_role = Role(
self,
f"{construct_id}_role",
assumed_by=ServicePrincipal("sagemaker.amazonaws.com"),
)
model_role.add_to_policy(PolicyStatement(
resources=["*"],
actions= [
"cloudwatch:PutMetricData",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:CreateLogGroup",
"logs:DescribeLogStreams",
"s3:GetObject",
"s3:ListBucket",
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage"
]
))
当您在 L1 (
CfnModel
) 和 L2 (Role
) 抽象级别之间移动时,事情会变得有点混乱。您需要使用所谓的 ecape hatch 语法:
cfnRole = cast(iam.CfnRole, model_role.node.default_child) # cast if using typings
sagemaker_model.add_depends_on(cfnRole)
还有其他方法可以在不强制转换 L1(
CfnModel
) 的情况下做到这一点,只需使用 .node
代替,例如:
sagemaker_model.node.add_dependency(model_role)