英文:
using CDK with existing Stack Templates
问题
我一直在尝试将CDK与我们当前的10个现有堆栈模板一起使用。这将是很好的,因为我可以使用cfninclude加载模板,然后使用代码添加任何新资源或修改模板中的现有资源,而不是手动在所有模板中进行操作。
今天我有一个要求,需要在所有模板中添加一个应用程序负载均衡器监听器规则,因此当我联系AWS支持以了解如何在模板中访问负载均衡器并以编程方式添加规则时,我被告知:
现在,要向模板中定义的监听器添加监听器规则,我们将不得不创建一个新的CfnListenerRule资源,
new elbv2.CfnListenerRule(this, "ListenerRule", {
listenerArn: listner.attrListenerArn,
priority: 123,
conditions: [
{ field: "path-pattern", values: ['/'] }
],
actions: [ { type: "forward", targetGroupArn: <target group ARN> , } ]
});
我尝试在我的端上复制相同的操作,通过设置targetGroupArn: tg.attrTargetGroupArn,但不幸的是,我看到了错误“属性'attrTargetGroupArn'在类型'CfnTargetGroup'上不存在。”
因此,我建议您选择以下其中一种选项,
- 首先创建堆栈,然后硬编码目标组的ARN。
- 在模板中创建监听器规则资源,然后使用cfn-include。
我觉得这些提出的选项对我来说都不合理,我不想为每个部署的堆栈在我的代码中硬编码ARN,也不想在需要进行更改时手动更新10个不同的模板,这违背了CDK为我自动完成工作的初衷。
我正在认真质疑在已经部署并投入生产的现有堆栈模板上使用CDK的价值。有人成功实现了这一点,而不仅仅是增加了更多的工作吗?
英文:
I have been trying to move towards using CDK along with our current 10 existing stack templates. This would be great since I can load the template using cfninclude and then add any new resources or modify existing resources in the template using code instead of manually doing it in all the templates.
I got a requirement today to add an Application Load Balancer Listener rule to all templates, so when I reached out to aws support on how to access the load balancer in the templates and add the rule programmatically I was told:
Now, to add a listener rule to the listener defined in the template, we will have to create a new
CfnListenerRule resource,
new elbv2.CfnListenerRule(this, "ListenerRule", {
listenerArn: listner.attrListenerArn,
priority: 123,
conditions: [
{ field: "path-pattern", values: ['/'] }
],
actions: [ { type: "forward", targetGroupArn: <target group ARN> , } ]
});
I tried to replicate the same at my end by setting targetGroupArn: tg.attrTargetGroupArn , but unfortunately I could see the error “Property 'attrTargetGroupArn' does not exist on type 'CfnTargetGroup'.”
Therefore, I would suggest you to go with either of these options,
- create the stack first and then hardcode the ARN of the target group.
- Create the listener rule as a resource in the template and then use cfn-include.
These proposed options seem absurd to me, I don't want to hardcode ARN's in my code for every deployed stack and I don't want to keep manually updating 10 different templates whenever I need to make a change, this defeats the purpose of CDK doing the work for me.
I'm seriously questioning the value of using a CDK once there are existing stack templates deployed and in production. Has anyone successfully done this without just creating more over head?
答案1
得分: 1
We have decided to just migrate the templates to code, but for anyone who needs to know how to do this, here you go from AWS support:
const template = new cfninc.CfnInclude(this, 'Template', {
templateFile: '/Users/abhithm/Downloads/importks.yaml',
});
const lb = template.getResource('LoadBalancer');
const listener = template.getResource('Listener') as elbv2.CfnListener;
const tg = template.getResource('TargetGroup1') as elbv2.CfnTargetGroup;
const x = tg.ref;
new elbv2.CfnListenerRule(this, "ListenerRule", {
listenerArn: listener.attrListenerArn,
priority: 123,
conditions: [
{ field: "path-pattern", values: ['/'] }
],
actions: [{ type: "forward", targetGroupArn: x }]
});
(Note: I've translated the code portion as requested. If you have any specific questions or need further assistance, feel free to ask.)
英文:
We have decided to just migrate the templates to code, but for anyone who needs to know how to do this, here you go from AWS support:
const template = new cfninc.CfnInclude(this, 'Template', {
templateFile: '/Users/abhithm/Downloads/importks.yaml',
});
const lb = template.getResource('LoadBalancer');
const listner = template.getResource('Listener') as elbv2.CfnListener;
const tg = template.getResource('TargetGroup1') as elbv2.CfnTargetGroup;;
const x = tg.ref;
new elbv2.CfnListenerRule(this, "ListenerRule", {
// listenerArn: listner.getAtt('Arn').toString(),
listenerArn: listner.attrListenerArn,
priority: 123,
conditions: [
{ field: "path-pattern", values: ['/'] }
],
actions: [ { type: "forward", targetGroupArn: x , } ]
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论