English 中文(简体)
如何在其他CDK资源中使用CDK资源的ARN
原标题:How to use ARN of CDK resource in another CDK resource

我有一个AWS CDK堆栈,其中包含一个Step Functions状态机和一个lambda函数。此lambda函数使用StartExecution API,该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]}。这与登录控制台时的结果相同。据我所知,在CDK堆栈的当前“阶段”,ARN不可用,但在lambda函数也被部署之前,我不知道如何将ARN获取到lambda函数。

问题回答

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

如果它在不同的堆栈中,您可能必须使用new CfOutput导出它,并使用cdk.Fn.importValue将其导入堆栈

我相信使用环境变量是正确的。我将使用机密管理器系统管理器参数存储,使其成为依赖项,并将其注入函数环境变量中。

如果不运行代码,我不确定<;资源>;。addDependency(<;other resource>;)是必需的,但如果遇到任何操作的部署顺序问题,请注意这一点。。。

import * as cdk from  aws-cdk-lib 
import * as lambda from  aws-cdk-lib/aws-lambda 
import * as secretsmanager from  aws-cdk-lib/aws-secretsmanager 
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 secret = secretsmanager.Secret(this,  secret , {
      secretObjectValue: {
        machineArn: stateMachine.stateMachineArn,
      },
    })

    // secret.node.addDependency(stateMachine)

    const lambda = new lambda.Function(this,  lambda , {
      environment: {
        STATE_MACHINE_ARN: dbSecret.secretValueFromJson( arn ).toString(),
      },
    })

    // lambda.node.addDependency(secret)
  }
}




相关问题
Mount windows shared drive to MWAA in bootscript

In MWAA startup script sudo yum install samba-client cifs-utils -y sudo mount.cifs //dev/test/drop /mnt/dev/test-o username=testuser,password= pwd ,domain=XX Executing above commonds giving error - ...

How to get Amazon Seller Central orders programmatically?

We have been manually been keying Amazon orders into our system and would like to automate it. However, I can t seem to figure out how to go about it. Their documentation is barely there. There is: ...

Using a CDN like Amazon S3 to control access to media

I want to use Amazon S3/CloudFront to store flash files. These files must be private as they will be accessed by members. This will be done by storing each file with a link to Amazon using a mysql ...

unable to connect to database on AWS

actually I have my website build with Joomla hosted on hostmonster but all Joomla website need a database support to run this database is on AWS configuration files need to be updated for that I ...

Using EC2 Load Balancing with Existing Wordpress Blog

I currently have a virtual dedicated server through Media Temple that I use to run several high traffic Wordpress blogs. Both tend to receive sudden StumbleUpon traffic surges that (I m assuming) ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

热门标签