Skip to content

fix: #118 CloudFormation YAML support #119

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

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/common-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ jobs:
run: npx vitest --retry 1 test/cdk-basic.test.ts
- name: Test - observability mode
run: OBSERVABLE_MODE=true npx vitest --retry 1 test/cdk-basic.test.ts
- name: Deploy YAML version
run: npm run deploy-yaml
working-directory: test/cdk-basic
- name: Test YAML
run: npx vitest --retry 1 test/cdk-basic.test.ts

test-cdk-esm:
runs-on: ubuntu-latest
Expand Down
13 changes: 12 additions & 1 deletion src/cloudFormation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { AwsCredentials } from './awsCredentials.js';
import { AwsConfiguration } from './types/awsConfiguration.js';
import { Logger } from './logger.js';
import * as yaml from 'yaml';

let cloudFormationClient: CloudFormationClient;

Expand All @@ -30,7 +31,17 @@ async function getCloudFormationStackTemplate(
throw new Error(`No template found for stack ${stackName}`);
}

const cfTemplate = JSON.parse(response.TemplateBody);
let cfTemplate: any;
try {
cfTemplate = JSON.parse(response.TemplateBody);
} catch (parseError: any) {
if (parseError.message.includes('is not valid JSON')) {
// If the template is not JSON, try parsing it as YAML
cfTemplate = yaml.parse(response.TemplateBody);
} else {
throw parseError;
}
}
return cfTemplate;
} catch (error: any) {
if (error.name === 'ValidationError') {
Expand Down
15 changes: 15 additions & 0 deletions test/cdk-basic/deploy-yaml.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This script is used to deploy the CDK stack using a YAML template.
npx cdk synth -c environment=test CdkbasicStack > CdkbasicStack.yaml
# Add a dummy resource to the template to force a change in the stack
awk '
/^Resources:/ && !injected {
print
print " DummyForceResource:"
print " Type: AWS::CloudFormation::WaitConditionHandle"
print " Properties: {}"
injected=1
next
}
{ print }
' CdkbasicStack.yaml > template.patched.yaml && mv template.patched.yaml CdkbasicStack.yaml
aws cloudformation deploy --template-file CdkbasicStack.yaml --stack-name test-lld-cdk-basic --capabilities CAPABILITY_NAMED_IAM
1 change: 1 addition & 0 deletions test/cdk-basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"deploy": "cdk deploy --all -c environment=test --require-approval never --outputs-file cdk-outputs.json",
"build": "cdk synth -c environment=test",
"deploy-yaml": "bash deploy-yaml.sh",
"destroy": "cdk destroy --all -c environment=test --force"
},
"devDependencies": {
Expand Down
Loading