gpt4 book ai didi

amazon-web-services - 使用 AWS CDK 解决 AWS Cloudformation 中的资源相互依赖性

转载 作者:行者123 更新时间:2023-12-03 07:48:20 25 4
gpt4 key购买 nike

我遇到的情况是,两个 AWS 资源是相互依赖的。对于下面提到的情况,我们该如何解决或者最好的解决方案是什么?

有两种构造,一种用于创建 SNS 主题,另一种用于 Lambda 函数。 SNS 需要 lambda 函数 ARN 来添加订阅,而 lambda 函数需要 SNS 主题 ARN 才能将其添加到环境变量中。如何使用 CDK(最好是在 .NET 中)解决这种依赖性?

代码:

public class AllStacks : Stack
{
public AllStacks(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
{
var lambdaFn = new LambdaFnConstruct(this, "LambdaFunction");
new SNSConstruct(this, "SnsTopic", lambdaFn.lambdaARN);
}
}

Lambda 构造:

internal LambdaFnConstruct(Construct scope, string id, Role role) : base(scope, id)
{
var lambdaFn = new Function(this, "LambdaCDK", new FunctionProps()
{
//Some code here...
Environment = new Dictionary<string, string>()
{
{"SNS_ARN", /* Need to provide SNS ARN here */ },
},
});
}

SNS构建:

public SNSConstruct(Construct scope, string id, string lambdaARN) : base(scope, id)
{
Topic topic = new Topic(this, "Messaging", new TopicProps()
{
TopicName = "Messaging"
});
Subscription subscription = new Subscription(this, "Topic subscription", new SubscriptionProps()
{
Topic = topic,
Protocol = SubscriptionProtocol.LAMBDA,
Endpoint = lambdaARN, //<----Lambda ARN goes here
});
}

Lambda 函数将消息流式传输到 AWS Connect:

public async Task EnableMessageStreamingAsync(string channel, string contactID)
{
try
{
StartContactStreamingRequest startContactStreamingRequest = new StartContactStreamingRequest()
{
ContactId = contactID,
InstanceId = instanceID,
ChatStreamingConfiguration = new ChatStreamingConfiguration()
{
StreamingEndpointArn = streamEndpointARN //<--- Here goes the SNS ARN
}
};

await connectClient.StartContactStreamingAsync(startContactStreamingRequest);
}
catch (Exception ex)
{
LambdaLogger.Log($"Error messgae: {ex.Message}");
LambdaLogger.Log($"Stack trace: {ex.StackTrace}");
}
}

最佳答案

找到了一个解决方案,我可以使用此方法在创建 Lambda 后添加环境变量:

public virtual Function AddEnvironment (string key, string value, IEnvironmentOptions? options = null)

因此,一旦创建了 SNS 主题(取决于 Lambda),我就可以稍后添加变量。

关于amazon-web-services - 使用 AWS CDK 解决 AWS Cloudformation 中的资源相互依赖性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74943974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com