aws-cloudformation - CDK 小写字符串标记跨堆栈引用

标签 aws-cloudformation aws-cdk amazon-sagemaker aws-cdk-typescript

我有一个 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/

相关文章:

aws-cloudformation - 使 Fargate 负载均衡器仅接受来自特定 IP 地址的流量

aws-cdk - 有没有办法使用 CDK 部署 Lex 机器人?

amazon-web-services - Sagemaker Studio Pyspark 示例失败

amazon-web-services - AWS SageMaker - 如何加载经过训练的 sklearn 模型以进行推理?

amazon-web-services - API网关 - 通过代理和AWS_IAM,不传递身份

json - 有没有办法从 CloudFormation 模板填充 Amazon DynamoDB 表?

amazon-web-services - 无法在 CDK 中使用来自 AWS CustomResource 的响应(通过 AwsSdkCall)

python - 如何在我的 Jupyter Sagemaker 笔记本实例中导入最新版本的 pandas?

amazon-web-services - 我的 ENI 是如何创建的?

aws-api-gateway - 在 API 网关路径中引用授权者定义