I'm currently trying a fetch a parameter for my pipeline through the CDK SSM Parameter library, however I seem to face a weird issue:
CfnParameter at 'nonProdAccountId.Parameter' should be created in the scope of a Stack, but no Stack found
However, I'm rather confident that I am fetching my Parameter value in the Scope of a Stack (the BackendPipelineStack
).
//cdk.ts aka my entrypoint
const app = new cdk.App()
...
new BackendPipelineStack(app, "BackendPipelineStack", {
nonProdAccountId: StringParameter.fromStringParameterName(app, "nonProdAccountId", "nonProdAccountId").stringValue,
apiStack,
commonInfraStack,
deploymentStack,
})
Am I missing something?
TIA
CodePudding user response:
Right now, you're importing the StringParameter in the scope of the App. The first argument in every Stack
or Construct
is the scope.
You can create stacks in the scope of an App, but you can't create constructs in the scope of an App - they have to be created in the scope of a Stack.
You need to move the import into the stack, and use the stack as the scope for the import (passing this
instead of app
).
So you would change
nonProdAccountId: StringParameter.fromStringParameterName(app, "nonProdAccountId", "nonProdAccountId").stringValue,
to
nonProdAccountIdParamName: "nonProdAccountId",
And import the parameter inside of the stack with
const nonProdAccountId = new StringParameter.fromStringParameterName(this, "nonProdAccountId", nonProdAccountIdParamName).stringValue;