Skip to content

Commit dfe0243

Browse files
authored
feature(core): Add additional CDK logging (#698)
* Added additional log statements * Enable CDK debugging and log stack template * Enable CDK_DEBUG with state machine input * Make state machine work without "verbose" setting * Fix unit test
1 parent e5558c8 commit dfe0243

File tree

6 files changed

+30
-8
lines changed

6 files changed

+30
-8
lines changed

src/core/cdk/src/initial-setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ export namespace InitialSetup {
660660
BOOTSTRAP_STACK_NAME: bootStrapStackName,
661661
'SCOPE.$': '$.scope',
662662
'MODE.$': '$.mode',
663+
'CDK_DEBUG.$': '$.verbose',
663664
};
664665

665666
const deployTask = new tasks.StepFunctionsStartExecution(this, `Deploy Phase ${phase}`, {

src/core/runtime/src/load-accounts-step.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { loadAccounts } from './utils/load-accounts';
1010
export interface SMInput {
1111
scope?: 'FULL' | 'NEW-ACCOUNTS' | 'GLOBAL-OPTIONS' | 'ACCOUNT' | 'OU';
1212
mode?: 'APPLY';
13+
verbose?: string | number;
1314
targetOus?: string[];
1415
targetAccounts?: string[];
1516
}
@@ -27,6 +28,7 @@ export interface LoadAccountsOutput {
2728
regions: string[];
2829
scope: 'FULL' | 'NEW-ACCOUNTS' | 'GLOBAL-OPTIONS' | 'ACCOUNT' | 'OU';
2930
mode: 'APPLY';
31+
verbose: string | '1' | '0';
3032
}
3133

3234
const dynamoDB = new DynamoDB();
@@ -47,7 +49,7 @@ export const handler = async (input: LoadAccountsInput): Promise<LoadAccountsOut
4749
smInput,
4850
} = input;
4951

50-
const { targetAccounts, targetOus, mode, scope } = smInput;
52+
const { targetAccounts, targetOus, mode, scope, verbose } = smInput;
5153

5254
// Retrieve Configuration from Code Commit with specific commitId
5355
const config = await loadAcceleratorConfig({
@@ -183,5 +185,6 @@ export const handler = async (input: LoadAccountsInput): Promise<LoadAccountsOut
183185
accounts: accountIds,
184186
scope: scope || 'NEW-ACCOUNTS',
185187
mode: mode || 'APPLY',
188+
verbose: verbose ? `${verbose}` : '0',
186189
};
187190
};

src/deployments/cdk/src/deployments/config/create.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,19 @@ export async function createRule(props: CreateRuleProps) {
7171
}
7272
const ouAwsConfigRuleConfigs = ouConfig['aws-config'];
7373
for (const [accountKey, accountConfig] of config.getAccountConfigsForOu(ouKey)) {
74+
console.warn(`Creating config rules in account ${accountKey}`);
7475
const awsAccountConfigRuleConfig = accountConfig['aws-config'];
7576
for (const awsConfigRuleConfig of ouAwsConfigRuleConfigs) {
7677
for (const ruleName of awsConfigRuleConfig.rules) {
77-
console.log(
78-
`Deploying ${ruleName} in Account ${accountKey} and in regions excluding ${awsConfigRuleConfig['excl-regions']}`,
79-
);
78+
console.log(`Creating config rule ${ruleName}`);
8079
const awsConfigRule = configRules.find(cr => cr.name === ruleName);
8180
if (!awsConfigRule) {
82-
console.warn(`Config Rule ${ruleName} is not found in Accelerator Configuration global-options`);
81+
console.warn(`Config rule ${ruleName} is not found in Accelerator Configuration global-options`);
8382
continue;
8483
}
84+
console.debug(`Config rule configuration`);
85+
console.debug(JSON.stringify(awsConfigRule, null, 2));
86+
8587
const remediation =
8688
awsConfigRule.remediation === undefined ? configRuleDefaults.remediation : awsConfigRule.remediation;
8789
const remediationAttempts =
@@ -91,13 +93,16 @@ export async function createRule(props: CreateRuleProps) {
9193
const remediationConcurrency =
9294
awsConfigRule['remediation-concurrency'] || configRuleDefaults['remediation-concurrency'];
9395
for (const region of config['global-options']['supported-regions']) {
96+
console.warn(`Creating config rule ${ruleName} in region ${region}`);
9497
if (awsConfigRuleConfig['excl-regions'].includes(region)) {
98+
console.warn(`Skipping creation in excluded region ${region}`);
9599
continue;
96100
}
97101
const isRuleIgnored = awsAccountConfigRuleConfig.find(
98102
ac => ac['excl-rules'].includes(ruleName) && ac.regions.includes(region),
99103
);
100104
if (isRuleIgnored) {
105+
console.warn(`Skipping creation as config rule is excluded for region ${region}`);
101106
continue;
102107
}
103108
const accountStack = accountStacks.tryGetOrCreateAccountStack(accountKey, region);
@@ -118,13 +123,15 @@ export async function createRule(props: CreateRuleProps) {
118123
});
119124
let configRule;
120125
if (awsConfigRule.type === 'managed') {
126+
console.warn(`Creating rule as managed rule`);
121127
configRule = new awsConfig.ManagedRule(accountStack, `ConfigRule-${ruleName}`, {
122128
identifier: ruleName,
123129
configRuleName,
124130
description: configRuleName,
125131
inputParameters: configParams,
126132
});
127133
} else {
134+
console.warn(`Creating rule as custom resource`);
128135
if (!configRuleArtifact) {
129136
console.error('ConfigRuleArtifact is not found to create Custom ConfigRule');
130137
continue;

src/deployments/cdk/toolkit.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Configuration, Command } from 'aws-cdk/lib/settings';
99
import { SdkProvider } from 'aws-cdk/lib/api/aws-auth';
1010
import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments';
1111
import { PluginHost } from 'aws-cdk/lib/plugin';
12+
import { debugModeEnabled } from '@aws-cdk/core/lib/debug';
1213
import { AssumeProfilePlugin } from '@aws-accelerator/cdk-plugin-assume-role/src/assume-role-plugin';
1314
import { fulfillAll } from './promise';
1415

@@ -160,8 +161,13 @@ export class CdkToolkit {
160161
}
161162

162163
async deployStack(stack: CloudFormationStackArtifact): Promise<StackOutput[]> {
164+
console.log(`Deploying stack ${stack.displayName}`);
165+
if (debugModeEnabled()) {
166+
console.debug(JSON.stringify(stack.template, null, 2));
167+
}
168+
163169
const stackExists = await this.cloudFormation.stackExists({ stack });
164-
console.log(`Is ${stack.displayName} stack exists`, stackExists);
170+
console.log(`Stack ${stack.displayName} exists`, stackExists);
165171

166172
const resources = Object.keys(stack.template.Resources || {});
167173
if (resources.length === 0) {
@@ -174,6 +180,10 @@ export class CdkToolkit {
174180
} else if (stackExists) {
175181
const sdk = await this.props.sdkProvider.forEnvironment(stack.environment, Mode.ForWriting);
176182
const cfn = sdk.cloudFormation();
183+
if (debugModeEnabled()) {
184+
cfn.config.logger = console;
185+
}
186+
177187
console.log(`Calling describeStacks API for ${stack.displayName} stack`);
178188
const existingStack = await cfn
179189
.describeStacks({

src/installer/cdk/assets/start-execution.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ exports.handler = async function (event, context) {
2020
const smInput = {
2121
scope: 'FULL',
2222
mode: 'APPLY',
23-
}
23+
verbose: '0',
24+
};
2425

2526
await sfn
2627
.startExecution({

src/installer/cdk/test/start-execution.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test('the State Machine execution should be started', async () => {
3838
await handler(event);
3939

4040
expect(startExecution).toBeCalledWith({
41-
input: '{"scope":"FULL","mode":"APPLY"}',
41+
input: '{"scope":"FULL","mode":"APPLY","verbose":"0"}',
4242
stateMachineArn: 'arn:state-machine',
4343
});
4444
expect(putJobSuccessResult).toBeCalledWith({

0 commit comments

Comments
 (0)