[BUG] Error: LogicalId defined outside of stack
- Dominant language
- TypeScript
- Stars
- 451
- Forks
- 84
- Avg merge
- 20h 3m
- Merged PRs (30d)
- 1
Description
### 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](https://www.npmjs.com/package/@cloudcomponents/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:
```typescript
#!/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:
```json
{
"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",
"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
Contributor guide
Assessment
This issue has not been assessed yet.