aws-pdk: [BUG] Error: LogicalId defined outside of stack
Describe the bug
I have encountered a bug that was also reported here: #271.
When trying to generate a graph for a stack that uses cdk-temp-stack, cdk synth fails with error also reported in #271
Expected Behavior
Possible to generate a graph.
Current Behavior
Error: LogicalId defined outside of stack: AutoDestructDeleteStackServiceRoleADD04D27 - Node:CFN_RESOURCE::xxxAutoDestructDeleteStackServiceRole11230D55
at new Node (/Users/tomasztrebski/dev-ay/ay5/node_modules/@aws/pdk/cdk-graph/core/graph.ts:1173:15)
at new CfnResourceNode (/Users/tomasztrebski/dev-ay/ay5/node_modules/@aws/pdk/cdk-graph/core/graph.ts:2051:5)
at visit (/Users/tomasztrebski/dev-ay/ay5/node_modules/@aws/pdk/cdk-graph/core/compute.ts:266:18)
at visit (/Users/tomasztrebski/dev-ay/ay5/node_modules/@aws/pdk/cdk-graph/core/compute.ts:296:9)
at visit (/Users/tomasztrebski/dev-ay/ay5/node_modules/@aws/pdk/cdk-graph/core/compute.ts:296:9)
at visit (/Users/tomasztrebski/dev-ay/ay5/node_modules/@aws/pdk/cdk-graph/core/compute.ts:296:9)
at visit (/Users/tomasztrebski/dev-ay/ay5/node_modules/@aws/pdk/cdk-graph/core/compute.ts:296:9)
at visit (/Users/tomasztrebski/dev-ay/ay5/node_modules/@aws/pdk/cdk-graph/core/compute.ts:296:9)
at computeGraph (/Users/tomasztrebski/dev-ay/ay5/node_modules/@aws/pdk/cdk-graph/core/compute.ts:306:3)
at CdkGraph._synthesize (/Users/tomasztrebski/dev-ay/ay5/node_modules/@aws/pdk/cdk-graph/cdk-graph.ts:284:31)
Reproduction Steps
Originally I thought the problem comes from using TimeToLive from @cloudcomponents/cdk-temp-stack.
However building a short repro with cdk.Stack does not reveal the problem.
Reason why I’m bringing up cdk.Stack is that I have built my custom stack that sets up some defaults like permissionBoundary, synethizer and TimeToLive to match regulations in my company.
Error occurs if I add MyStack to an cdk.App but does not for ordinary cdk.Stack.
I cannot share the full code of my stack however it roughly does this:
#!/usr/bin/env node
import { CdkGraph } from '@aws/pdk/cdk-graph';
import { TimeToLive } from '@cloudcomponents/cdk-temp-stack';
import * as cdk from 'aws-cdk-lib';
import 'source-map-support/register';
import { IConstruct } from 'constructs';
const app = new cdk.App();
const env = {
account:
process.env['CDK_DEPLOY_ACCOUNT'] || process.env['CDK_DEFAULT_ACCOUNT'],
region: process.env['CDK_DEPLOY_REGION'] || process.env['CDK_DEFAULT_REGION'],
};
class Stack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props: cdk.StackProps) {
super(scope, id, {
...props,
terminationProtection: false,
analyticsReporting: false,
permissionsBoundary: cdk.PermissionsBoundary.fromName('test'),
synthesizer: new cdk.DefaultStackSynthesizer({
qualifier: 'xx',
dockerTagPrefix: 'xx',
}),
});
cdk.Aspects.of(this).add(new ApplyDestroyPolicyAspect());
new TimeToLive(this, 'AutoDestruct', {
ttl: cdk.Duration.days(1),
});
}
}
class ApplyDestroyPolicyAspect implements cdk.IAspect {
visit(node: IConstruct): void {
if (node instanceof cdk.CfnResource) {
node.applyRemovalPolicy(cdk.RemovalPolicy.DESTROY);
}
}
}
new Stack(app, 'Test', {
env,
stackName: 'xxx',
});
new CdkGraph(app);
Error is not throw here though. I can share a resulting cloudformation template though:
{
"Description": "[testing] XXX",
"Resources": {
"AutoDestructDeleteStackServiceRoleADD04D27": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
]
}
],
"PermissionsBoundary": "arn:aws:iam::123456789012:policy/ays-cdk-v1-permissions-boundary"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
"Metadata": {
"aws:cdk:path": "xxx/AutoDestruct/DeleteStack/ServiceRole/Resource"
}
},
"AutoDestructDeleteStackServiceRoleDefaultPolicy42286F0B": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": "*",
"Effect": "Allow",
"Resource": "*"
}
],
"Version": "2012-10-17"
},
"PolicyName": "AutoDestructDeleteStackServiceRoleDefaultPolicy42286F0B",
"Roles": [
{
"Ref": "AutoDestructDeleteStackServiceRoleADD04D27"
}
]
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
"Metadata": {
"aws:cdk:path": "xxx/AutoDestruct/DeleteStack/ServiceRole/DefaultPolicy/Resource"
}
},
"AutoDestructDeleteStack239CEEE3": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "cdk-cdk-v1-assets-123456789012-eu-central-1",
"S3Key": "ece017a7d7cfba4a1602f6d267cf5a02781708db95bbf4ff8c2394796f26b7a2.zip"
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"AutoDestructDeleteStackServiceRoleADD04D27",
"Arn"
]
},
"Runtime": "nodejs14.x"
},
"DependsOn": [
"AutoDestructDeleteStackServiceRoleDefaultPolicy42286F0B",
"AutoDestructDeleteStackServiceRoleADD04D27"
],
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
"Metadata": {
"aws:cdk:path": "xxx/AutoDestruct/DeleteStack/Resource",
"aws:asset:path": "asset.ece017a7d7cfba4a1602f6d267cf5a02781708db95bbf4ff8c2394796f26b7a2",
"aws:asset:is-bundled": false,
"aws:asset:property": "Code"
}
},
"AutoDestructTimeToLive6FA1EF2B": {
"Type": "AWS::Events::Rule",
"Properties": {
"ScheduleExpression": "rate(3 hours)",
"State": "ENABLED",
"Targets": [
{
"Arn": {
"Fn::GetAtt": [
"AutoDestructDeleteStack239CEEE3",
"Arn"
]
},
"Id": "Target0",
"Input": {
"Fn::Join": [
"",
[
"{\"stackId\":\"",
{
"Ref": "AWS::StackId"
},
"\"}"
]
]
}
}
]
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
"Metadata": {
"aws:cdk:path": "xxx/AutoDestruct/TimeToLive/Resource"
}
},
"AutoDestructTimeToLiveAllowEventRulexxxAutoDestructDeleteStackC6581343BEFD32FE": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Fn::GetAtt": [
"AutoDestructDeleteStack239CEEE3",
"Arn"
]
},
"Principal": "events.amazonaws.com",
"SourceArn": {
"Fn::GetAtt": [
"AutoDestructTimeToLive6FA1EF2B",
"Arn"
]
}
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete",
"Metadata": {
"aws:cdk:path": "xxx/AutoDestruct/TimeToLive/AllowEventRulexxxAutoDestructDeleteStackC6581343"
}
}
},
"Parameters": {
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/cdk-bootstrap/cdk-v1/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
}
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3",
"4",
"5"
],
{
"Ref": "BootstrapVersion"
}
]
}
]
},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
}
]
}
}
}
Possible Solution
No response
Additional Information/Context
CDK: 2.121.1
PDK version used
0.22.50
What languages are you seeing this issue on?
Typescript
Environment details (OS name and version, etc.)
MacOS Sierra
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 18 (4 by maintainers)
+1, same thing happens simply with EKS L3 construct
`import { Stack, StackProps } from “aws-cdk-lib”; import { Construct } from “constructs”; import { KubectlV29Layer } from ‘@aws-cdk/lambda-layer-kubectl-v29’; import * as eks from ‘aws-cdk-lib/aws-eks’;
export class ApplicationStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props);
} }`
running ‘npm run build’ throws
/Users/andrea.spoldi/architects/pdk/prova/node_modules/@aws/pdk/cdk-graph/core/graph.ts:1173 throw new Error( ^ Error: LogicalId defined outside of stack: Handler886CB40B - Node:CFN_RESOURCE::infradevawscdkawseksKubectlProviderHandler14CEB732@JeremyJonas as to what this stack of mine really is. It is simply speaking a class that extends from
cdk.Stackand set things up according to my taste. The idea was that no matter what my team decides to take for a spin they always adhere to certain regulations I’ve laid out among which you can find:And that’s really all that is. For most of the time my stack does not really create extra resources, just sets values later passed into constructor of
cdk.Stack. The only time something is created is fortestingconfiguration which is exactly what fails.Update: I actually made some more tests. Now I noticed that
cdk synth+MyStackfails to create a graph with this error:So we’re failing now on the output which makes me think that whatever construct gets created in my stack is something…flawed.
I don’t know what that can be… maybe something messed up on
jsiipart and packaging the construct/stack?Update2: I removed the outputs from 1st update and now I get the failure on another resource. Error is exactly the same, just resource ID changes.