如何在另一个CDK资源中使用CDK资源的ARN

huangapple go评论68阅读模式
英文:

How to use ARN of CDK resource in another CDK resource

问题

我有一个包含 Step Functions 状态机和 Lambda 函数的 AWS CDK 堆栈。这个 Lambda 函数使用 StartExecution API,需要状态机的 ARN。我不知道如何获取状态机的 ARN,因为它是不稳定的并且经常变化。

我尝试创建一个与 Lambda 函数的 index.ts 文件相邻的 .env 文件。

const stateMachine = new stepfunctions.StateMachine(this, 'my-state-machine', {
     definition: waitState,
});

然后使用 CDK 堆栈中的 fsstateMachine.stateMachineArn 写入到该 .env 文件中。写入到 .env 文件中的结果是${Token[TOKEN.1056]}。当记录到控制台时,结果也相同。据我了解,ARN 在 CDK 堆栈的当前“阶段”中不可用,但我不知道如何在 Lambda 函数部署之前将 ARN 传递给 Lambda 函数。

英文:

I have an AWS CDK stack containing a Step Functions state machine and a lambda function. This lambda function uses the StartExecution API which requires the ARN of the state machine. I'm am unaware of how to acquire the ARN of the state machine since it is volatile and constantly changes.

I have tried creating a .env file next to the index.ts of the lambda function.

const stateMachine = new stepfunctions.StateMachine(this, 'my-state-machine', {
     definition: waitState,
});

And writing the stateMachine.stateMachineArn to that .env file using fs from the CDK stack. The result written to the .env file is ${Token[TOKEN.1056]}. This is the same result when logging to the console. From my understanding, the ARN is not available during the current "phase" of the CDK stack, but I don't know how to get the ARN to the lambda function before the lambda function is also deployed.

答案1

得分: 2

为什么不直接将 stateMachine.stateMachineArn 传递给 Lambda 函数?如果它是同一堆栈的一部分,您应该能够通过从 Constructs 中公开 ARN 属性并从 Lambda 中引用它来实现。

如果它位于不同的堆栈中,您可能需要使用 new CfnOutput 导出它,然后在您的堆栈中使用 cdk.Fn.importValue 进行导入。

如果要在 Lambda 代码内部使用它,您将不得不将其作为环境变量传递给 Lambda 函数。

英文:

Why don't you pass stateMachine.stateMachineArn directly to the lambda function? You should be able to do it if it is part of the same stack, by exposing the ARN property from one of the Constructs and referencing it from the lambda.

If it is in a different stack you may have to export it using new CfnOutput and import it in your stack using cdk.Fn.importValue

You will have to pass it in as an environment variable to the lambda function if it is to be used inside lambda code.

答案2

得分: 0

我认为你正在正确地使用环境变量。不要在部署过程中动态创建.env文件以传递状态机 ARN 给 Lambda 函数,而是使用 secrets managersystems manager parameter store,将其作为依赖项并注入到函数的 environment 变量中。

如果没有运行代码,我不能确定是否需要 <resource>.addDependency(<other resource>),但如果遇到任何部署顺序问题,了解这一点是很好的。

import * as cdk from 'aws-cdk-lib'
import * as lambda from 'aws-cdk-lib/aws-lambda'
import * as stepfunctions from 'aws-cdk-lib/aws-stepfunctions'

export class Stack extends cdk.Stack implements cdk.Stack {
  constructor(scope, id, props?: cdk.StackProps) {
    super(scope, id, props)

    const stateMachine = new stepfunctions.StateMachine(this, 'state-machine', {
      definition: waitState,
    })

    const lambda = new lambda.Function(this, 'lambda', {
      environment: {
        STATE_MACHINE_ARN: stateMachine.stateMachineArn,
      },
    })

    lambda.node.addDependency(stateMachine)
  }
}
英文:

I believe you are on the right track using an environment variable. Instead of creating a .env file on the fly during deployment as a means to pass the state machine ARN to the lambda function, I would use secrets manager or systems manager parameter store, make that a dependency and inject that into the functions environment variables.

I am not sure without running code if the <resource>.addDependency(<other resource>) is needed but good to be aware of this if you run into any deployment order of operation issues...

import * as cdk from 'aws-cdk-lib'
import * as lambda from 'aws-cdk-lib/aws-lambda'
import * as stepfunctions from 'aws-cdk-lib/aws-stepfunctions'

export class Stack extends cdk.Stack implements cdk.Stack {
  constructor(scope, id, props?: cdk.StackProps) {
    super(scope, id, props)

    const stateMachine = new stepfunctions.StateMachine(this, 'state-machine', {
      definition: waitState,
    })

    const lambda = new lambda.Function(this, 'lambda', {
      environment: {
        STATE_MACHINE_ARN: stateMachine.stateMachineArn,
      },
    })

    lambda.node.addDependency(stateMachine)
  }
}

huangapple
  • 本文由 发表于 2023年7月14日 06:13:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定