简体   繁体   English

在 AWS CDK 中为多个堆栈创建可预测的堆栈名称

[英]Create predicatable stack names for multiple stacks in AWS CDK

How can I get "nice" stack names when executing npx cdk synth in an AWS CDK application that is made up of multiple stacks that I would like to deploy to multiple environments ?在由我想部署到 多个环境多个堆栈组成的 AWS CDK 应用程序中执行npx cdk synth时,如何获得“漂亮”的堆栈名称?

#!/usr/bin/env node
import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';
import * as lambda from '@aws-cdk/aws-lambda';


class PersistenceStack extends cdk.Stack {
    public readonly bucket: s3.Bucket;
    constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
        super(scope, id, props);
        this.bucket = new s3.Bucket(this, 'bucket');
    }
}


interface ApplicationStackProps extends cdk.StackProps {
    bucket: s3.Bucket;
}


class ApplicationStack extends cdk.Stack {
    constructor(scope: cdk.Construct, id: string, props: ApplicationStackProps) {
        super(scope, id, props);
        const myLambda = new lambda.Function(this, 'my-lambda', {
            runtime: lambda.Runtime.NODEJS_12_X,
            code: new lambda.AssetCode('my-lambda'),
            handler: 'index.handler'
        });
        props.bucket.grantReadWrite(myLambda);
    }
}


class MyApp extends cdk.Construct {

    constructor(scope: cdk.Construct, id: string, props: cdk.StackProps) {
        super(scope, id);

        const persistenceStack = new PersistenceStack(this, 'persistence-stack', {
            ...props,
            description: 'persistence stack',
            stackName: `${id}-persistence-stack`,
        });

        const applicationStack = new ApplicationStack(this, 'application-stack', {
            ...props,
            description: 'application stack',
            stackName: `${id}-application-stack`,
            bucket: persistenceStack.bucket,
        });
        applicationStack.addDependency(persistenceStack);
    }
}


const app = new cdk.App();

new MyApp(app, `test`, { env: { account: '111111111111', region: 'eu-west-1' } });
new MyApp(app, `prod`, { env: { account: '222222222222', region: 'eu-west-1' } });

The problem that I am facing is that this generates outputs similar to:我面临的问题是这会生成类似于以下内容的输出:

Successfully synthesized to [...]/my-app/cdk.out
Supply a stack id (prodpersistencestackFE36DF49, testpersistencestack6C35C777, prodapplicationstackA0A96586, testapplicationstackE19450AB) to display its template.

What I expected to see is "nice" stack names (since I have specified the stackName properties when calling the constructors):我希望看到的是“漂亮”的堆栈名称(因为我在调用构造函数时指定了stackName属性):

Successfully synthesized to [...]/my-app/cdk.out
Supply a stack id (prodpersistencestack, testpersistencestack, prodapplicationstack, testapplicationstack) to display its template.

Motivation: I need "nice" (or at least predictable) stack names to feed into the next step our CI/CD pipeline so that the build server can deploy the CDK app.动机:我需要“漂亮”(或至少是可预测的)堆栈名称来提供给下一步我们的 CI/CD 管道,以便构建服务器可以部署 CDK 应用程序。

AWS CDK version: 1.21.1 AWS CDK 版本:1.21.1

This AWS CDK issue addresses this concern.这个AWS CDK 问题解决了这个问题。 In its response it is stated that the stack ids are like this by design (since one can have the same stack name in multiple environments).在其响应中,它声明堆栈 ID 是这样设计的(因为在多个环境中可以具有相同的堆栈名称)。

In order to use the stack name as a deployment parameter, one can implement a reverse lookup of the cdk.out/manifest.json file because the mapping information between stack ids and stack names is available there.为了使用堆栈名称作为部署参数,可以实现对cdk.out/manifest.json文件的反向查找,因为在那里可以获得堆栈 ID 和堆栈名称之间的映射信息。 Below is an excerpt of how such a lookup can be implemented using jq :以下是如何使用jq实现此类查找的摘录:

# get stack name from somewhere, e.g. test-persistence-stack, prod-persistence-stack, test-application-stack or prod-application-stack from the question above
stackName=    

# extract stack id from ./cdk.out/manifest.json
stackId=$(jq -r --arg stackName "$stackName" \
   '.artifacts
   | to_entries[]
   | select(.value.properties.stackName == $stackName)
   | .key
   ' \
   < "./cdk.out/manifest.json" \
   )

# call cdk deploy with stack id as parameter
npx cdk deploy \
  --app cdk.out \
  --require-approval never \
  "$stackId"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM