在for-in循环中在CDK中创建资源无效,但是直接创建有效吗?

时间:2020-08-06 19:09:14

标签: aws-cdk

我是CDK的新手,在CDK堆栈内创建资源集合时遇到问题。

我注意到在for循环中,似乎还没有运行createRule(),因为我之前在createRule内的CDK中犯了一个错误(在ID中使用了令牌),甚至都没有检测到直到我从循环中调用createRule()。

此循环是否有效?我能获得关于如何解决为什么即使地图上有内容却似乎没有运行或创建资源的任何建议吗?

这是正在发生的事情的简化版本:

export class Stack extends Cdk.Stack {

   construct(scope: ... , id: string, props?: Cdk.StackProps) { 
   
       const somePrefix: string = new Cdk.CfnParameter(this, 'SomePrefix', {...}).valueAsString;
       
       ... // some other resources ... / 
       
       let someMap = new Map<string, string[]>();
       someMap.set("ABC", ["AB", "CD", "EF", "GH", "IJ"]);
       someMap.set("XYZ", ["BLAH"]);
       
       // this runs and creates the resource as expected
       this.createRule(this, somePrefix, "ARGH", "MI");

       for (let x in someMap.keys()) {
           for (let y in someMap.get(x)) {
               // this seems like it doesn't create anything
               this.createRule(this, somePrefix, x, y);
           }
       }
    }

    createRule(scope: Cdk.Construct, somePrefix: string, x: string, y: string): Rule {

        const someLambda: string = Cdk.Fn.importValue(
            `${somePrefix}someLambda`
        );
        const someLambdaHandler = Lambda.Function.fromFunctionArn(scope, "SomeLambdaHandler", someLambda);

        const someEvent = {
            queryStringParameters: {
                ... 
            }
        };

        const someTarget = new Targets.LambdaFunction(someLambdaHandler, {
            event: RuleTargetInput.fromObject(someEvent)
        });

        return new Rule(scope, `lambdarule-${x}-${y}`, {
            enabled: true,
            schedule: Schedule.cron({minute: "30"}),
            targets: [realmCountryFunctionTarget],
            ruleName: `SomeRule-${somePrefix}${x}-${y}`
        })
    }

0 个答案:

没有答案