Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple StackSets in one Stack with assetBucket #257

Open
boenhoff opened this issue Oct 9, 2023 · 2 comments
Open

Multiple StackSets in one Stack with assetBucket #257

boenhoff opened this issue Oct 9, 2023 · 2 comments

Comments

@boenhoff
Copy link

boenhoff commented Oct 9, 2023

I want to create multiple StackSets in one Stack. I tried to deploy the following CDK Stack:

CDK Stack

import { Stack, StackProps } from 'aws-cdk-lib';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { Bucket } from 'aws-cdk-lib/aws-s3';
import { Capability, DeploymentType, StackSet, StackSetStack, StackSetTarget, StackSetTemplate } from 'cdk-stacksets';
import { Construct } from 'constructs';
import { config } from '../config';

export class TestStack extends Stack {

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

    const assetBucket = Bucket.fromBucketName(this, 'SomeBucketForAssets', config.bucketName);

    const firstStackSet = new StackSetStack(this, 'FirstStackSet', {
      assetBucket: assetBucket,
    });
    new NodejsFunction(firstStackSet, 'FirstStackSetLambda', {
      logRetention: RetentionDays.THREE_DAYS,
      entry: './src/lambda/index.ts',
      depsLockFilePath: './src/lambda/package-lock.json',
    });
    new StackSet(this, 'FirstStackSetDeployment', {
      target: StackSetTarget.fromOrganizationalUnits({
        regions: config.stackSetRegions,
        organizationalUnits: config.stackSetOrganizationalUnits,
        excludeAccounts: [config.cloudformationAccountId],
      }),
      deploymentType: DeploymentType.serviceManaged(),
      capabilities: [Capability.IAM],
      template: StackSetTemplate.fromStackSetStack(firstStackSet),
    });

    const secondStackSet = new StackSetStack(this, 'SecondStackSet', {
      assetBucket: assetBucket,
    });
    new NodejsFunction(secondStackSet, 'SecondStackSetLambda', {
      logRetention: RetentionDays.THREE_DAYS,
      entry: './src/lambda/index.ts',
      depsLockFilePath: './src/lambda/package-lock.json',
    });
    new StackSet(this, 'SecondStackSetDeployment', {
      target: StackSetTarget.fromOrganizationalUnits({
        regions: config.stackSetRegions,
        organizationalUnits: config.stackSetOrganizationalUnits,
        excludeAccounts: [config.cloudformationAccountId],
      }),
      deploymentType: DeploymentType.serviceManaged(),
      capabilities: [Capability.IAM],
      template: StackSetTemplate.fromStackSetStack(secondStackSet),
    });
  }
}

Error

/redacted/app/node_modules/constructs/src/construct.ts:428
      throw new Error(`There is already a Construct with name '${childName}' in ${typeName}${name.length > 0 ? ' [' + name + ']' : ''}`);
            ^
Error: There is already a Construct with name 'StackSetAssetsBucketDeployment' in TestStack [TestStack]
    at Node.addChild (/redacted/app/node_modules/constructs/src/construct.ts:428:13)
    at new Node (/redacted/app/node_modules/constructs/src/construct.ts:71:17)
    at new Construct (/redacted/app/node_modules/constructs/src/construct.ts:480:17)
    at new BucketDeployment (/redacted/app/node_modules/aws-cdk-lib/aws-s3-deployment/lib/bucket-deployment.js:1:803)
    at StackSetStackSynthesizer.addFileAsset (/redacted/app/node_modules/cdk-stacksets/src/stackset-stack.ts:59:32)
    at new Asset (/redacted/app/node_modules/aws-cdk-lib/aws-s3-assets/lib/asset.js:1:1207)
    at AssetCode.bind (/redacted/app/node_modules/aws-cdk-lib/aws-lambda/lib/code.js:1:4628)
    at new Function (/redacted/app/node_modules/aws-cdk-lib/aws-lambda/lib/function.js:1:7510)
    at new NodejsFunction (/redacted/app/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib/function.js:1:1215)
    at new TestStack (/redacted/app/src/Stacks/TestStack.ts:38:5)

If I use one parent stack per StackSet it works of course. I'm doing something wrong or is that at the moment a limitation from this construct?

@mbergkvist
Copy link

Try put each StackSetStack, and it's resources, in separate Constructs. Should make StackSetAssetsBucketDeployment namespaced.

@josh-demuth
Copy link
Contributor

i believe this will be resolved in #325. this causes a breaking change to how the bucket is passed and named though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants