我有一个 CDK 应用程序,在其中使用 CfnEndpoint
创建 Sagemaker 端点。当我省略名称参数时,CDK 会为我生成一个唯一的参数,这很棒。
const endpoint = new CfnEndpoint(this, "Endpoint", {
endpointConfigName: endpointConfig.attrEndpointConfigName,
});
当我想授予某些权限来调用另一个堆栈中的端点时,问题就出现了。我想授予 lambda 函数权限以仅调用该端点而不调用其他端点,因此我通常会执行以下操作:
myFunction.addToRolePolicy(new PolicyStatement({
effect: Effect.ALLOW,
actions: ['sagemaker:InvokeEndpoint'],
resources: [Stack.of(this).formatArn({
service: 'sagemaker',
resource: 'endpoint',
resourceName: endpoint.attrEndpointName, // <- reference the endpoint name from another stack
})],
}));
问题是,当我这样做时,arn 会使用大小写混合的端点名称进行解析:
arn:aws:sagemaker:ap-southeast-2:012345678910:endpoint/Endpoint3015B993-FcX1jFxPQKeE
并且我的 lambda 没有被授予适当的权限,因为名称需要完全小写才能执行此操作。
我还尝试使用 Fn::Transform
CloudFormation 宏,如下所示:
myFunction.addToRolePolicy(new PolicyStatement({
effect: Effect.ALLOW,
actions: ['sagemaker:InvokeEndpoint'],
resources: [Stack.of(this).formatArn({
service: 'sagemaker',
resource: 'endpoint',
resourceName: Fn.transform('String', {
Operation: "Lower",
InputString: endpoint.attrEndpointName,
}),
})],
}));
但是当我尝试部署时,这会引发错误:
Error: Failed to create ChangeSet cdk-deploy-change-set on MyStack: FAILED, The value of parameter InputString under transform String must resolve to a string, number, boolean or a list of any of these.
听起来该转换仅适用于静态值或云信息输入。
有人知道如何用小写端点名称构造 arn 吗?
我真的不想为我的端点硬编码一个名称,并且自定义资源 lambda 感觉有点矫枉过正,但我想其中任何一个都可以对其进行排序。
最佳答案
这里有一些可行的方法,但使用 lambda 函数感觉有点大材小用:
const endpointArnCustomResource = new CustomResource(this, "EndpointArnCustomResource", {
properties: {
endpointArn: Stack.of(this).formatArn({
service: "sagemaker",
resource: "endpoint",
resourceName: endpoint.attrEndpointName,
}),
},
serviceToken: new Provider(this, "EndpointArnProvider", {
onEventHandler: new Function(this, "EndpointArnFunction", {
runtime: Runtime.NODEJS_18_X,
handler: "index.handler",
code: Code.fromInline(`exports.handler = async (event) => {
return {
PhysicalResourceId: event.PhysicalResourceId,
Data: {
endpointArn: event.ResourceProperties.endpointArn.toLowerCase(),
},
};
}`)
}),
}).serviceToken,
});
// This has the endpoint arn in lowercase
const endpointArn = endpointArnCustomResource.getAttString("endpointArn");
关于aws-cloudformation - CDK 小写字符串标记跨堆栈引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77074941/