AWS服务目录-将参数传递到ProductStack中的堆栈



我有一个服务目录组合,其中部署了一个产品。该产品是从一个基本的AWS CDK构造(S3 Bucket)创建的。我想延长产品的有效性通过添加一个参数,用户可以在消费产品时填充。当我在ProductStack中使用Construct时,这似乎很容易:

export class ScConstructDemoStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const portfolio = new Portfolio(this, 'myportfolio', {
displayName: 'Test Portfolio',
providerName: 'Acme Corp'
});
const bucketConstructCFProduct = new CloudFormationProduct(this, 'SCProduct_construct_bucket', {
productName: 'S3FromConstructWithParam',
owner: 'me',
productVersions: [{
cloudFormationTemplate: CloudFormationTemplate.fromProductStack(new myS3ConstructProduct(this, 'S3FromConstructWithParam')),
productVersionName: 'v1',
description: 'Quick S3 Bucket'
}]
});
portfolio.addProduct(bucketConstructCFProduct);
}
}
class myS3ConstructProduct extends ProductStack {
constructor(scope: Construct, id: string) {
super(scope, id);
const uploadBucketName = new CfnParameter(this, 'bucketNameParam', {
type: 'String',
description: 'Name of S3 Bucket',
});
const bucket = new Bucket(this, 'construct-bucket', {
bucketName: uploadBucketName.valueAsString
});
}
}

这工作。

然而,我也有一个CDK堆栈,我想添加服务目录。这个堆栈是用CfnParameter定义的,我希望在配置产品时可用。

export class ScCdkDemoStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const portfolio = new Portfolio(this, 'myportfolio', {
displayName: 'Test Portfolio',
providerName: 'Acme Corp'
});
const bucketCFProduct = new CloudFormationProduct(this, 'SCProduct_bucket', {
productName: 'S3WithParam',
owner: 'me',
productVersions: [{
cloudFormationTemplate: CloudFormationTemplate.fromProductStack(new myS3StackProduct(this, 'S3WithParam')),
productVersionName: 'v1',
description: 'Quick S3 Bucket'
}]
});
portfolio.addProduct(bucketCFProduct);
}
}
interface bucketProps extends StackProps {
readonly bucketName: string
}
class myS3Stack extends Stack {
constructor(scope: Construct, id: string, props: bucketProps) {
super(scope, id, props);
const bucket = new Bucket(this, 'mybucket', {
bucketName: props.bucketName
});
}
}
class myS3StackProduct extends ProductStack {
constructor(scope: Construct, id: string) {
super(scope, id);
const uploadBucketName = new CfnParameter(this, 'bucketNameParam', {
type: 'String',
description: 'Name of S3 Bucket',
});
const deployStack = new myS3Stack(this, 's3StackProduct', {
bucketName: uploadBucketName.valueAsString
})
}
}

当我合成这个CDK代码时,我得到一个异常,我读取为一些(可能是CfnParameter)在堆栈创建时丢失/未定义。

Error: Artifact StackDemoStackS3WithParams3StackProduct88DBCA37 depends on non-existing artifact StackDemoStackS3WithParamDFB09CA4
at /Users/ben/code/sc_cdk_demo/node_modules/aws-cdk-lib/cx-api/lib/cloud-artifact.js:1:1310
at Array.map (<anonymous>)
at CloudFormationStackArtifact.get dependencies [as dependencies] (/Users/ben/code/sc_cdk_demo/node_modules/aws-cdk-lib/cx-api/lib/cloud-artifact.js:1:1244)
at CloudAssembly.validateDeps (/Users/ben/code/sc_cdk_demo/node_modules/aws-cdk-lib/cx-api/lib/cloud-assembly.js:1:3861)
at new CloudAssembly (/Users/ben/code/sc_cdk_demo/node_modules/aws-cdk-lib/cx-api/lib/cloud-assembly.js:1:1219)
at CloudAssemblyBuilder.buildAssembly (/Users/ben/code/sc_cdk_demo/node_modules/aws-cdk-lib/cx-api/lib/cloud-assembly.js:1:6098)
at Object.synthesize (/Users/ben/code/sc_cdk_demo/node_modules/aws-cdk-lib/core/lib/private/synthesis.js:1:800)
at App.synth (/Users/ben/code/sc_cdk_demo/node_modules/aws-cdk-lib/core/lib/stage.js:1:1866)
at process.<anonymous> (/Users/ben/code/sc_cdk_demo/node_modules/aws-cdk-lib/core/lib/app.js:1:1164)
at Object.onceWrapper (node:events:642:26)
Subprocess exited with error 1

谁能告诉我为什么我不能像这样传递参数给堆栈或指出我愚蠢的错误?

多谢

nb。我是TypeScript新手,这段代码所在的CDK库在这里

这个错误看起来像是堆栈之间的依赖关系有问题。你的myS3StackProduct堆栈不需要依赖于myS3Stack。相反,您可以在产品堆栈中定义S3桶和参数,如下所示:

class myS3StackProduct extends ProductStack {
def __init__(self, scope: Construct, id: str):
super().__init__(scope, id)
upload_bucket_name = CfnParameter(self, "uploadBucketName", type="String",
description="The name of the Amazon S3 bucket where uploaded files will be stored.")
bucket = s3.Bucket(self, "myBucket",
bucket_name=upload_bucket_name.value_as_string)
}

这将移除myS3StackProductmyS3Stack的依赖。

最新更新