Skip to content

Commit 5125ccb

Browse files
fix: #118 CloudFormation YAML support
1 parent 94738b8 commit 5125ccb

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

.github/workflows/common-test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ jobs:
7777
run: npx vitest --retry 1 test/cdk-basic.test.ts
7878
- name: Test - observability mode
7979
run: OBSERVABLE_MODE=true npx vitest --retry 1 test/cdk-basic.test.ts
80+
- name: Deploy YAML version
81+
run: npm run deploy-yaml
82+
working-directory: test/cdk-basic
83+
- name: Test YAML
84+
run: npx vitest --retry 1 test/cdk-basic.test.ts
8085

8186
test-cdk-esm:
8287
runs-on: ubuntu-latest

src/cloudFormation.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import { AwsCredentials } from './awsCredentials.js';
77
import { AwsConfiguration } from './types/awsConfiguration.js';
88
import { Logger } from './logger.js';
9+
import * as yaml from 'yaml';
910

1011
let cloudFormationClient: CloudFormationClient;
1112

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

33-
const cfTemplate = JSON.parse(response.TemplateBody);
34+
let cfTemplate: any;
35+
try {
36+
cfTemplate = JSON.parse(response.TemplateBody);
37+
} catch (parseError: any) {
38+
if (parseError.message.includes('is not valid JSON')) {
39+
// If the template is not JSON, try parsing it as YAML
40+
cfTemplate = yaml.parse(response.TemplateBody);
41+
} else {
42+
throw parseError;
43+
}
44+
}
3445
return cfTemplate;
3546
} catch (error: any) {
3647
if (error.name === 'ValidationError') {

test/cdk-basic/deploy-yaml.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This script is used to deploy the CDK stack using a YAML template.
2+
npx cdk synth -c environment=test CdkbasicStack > CdkbasicStack.yaml
3+
# Add a dummy resource to the template to force a change in the stack
4+
awk '
5+
/^Resources:/ && !injected {
6+
print
7+
print " DummyForceResource:"
8+
print " Type: AWS::CloudFormation::WaitConditionHandle"
9+
print " Properties: {}"
10+
injected=1
11+
next
12+
}
13+
{ print }
14+
' CdkbasicStack.yaml > template.patched.yaml && mv template.patched.yaml CdkbasicStack.yaml
15+
aws cloudformation deploy --template-file CdkbasicStack.yaml --stack-name test-lld-cdk-basic --capabilities CAPABILITY_NAMED_IAM

test/cdk-basic/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"scripts": {
88
"deploy": "cdk deploy --all -c environment=test --require-approval never --outputs-file cdk-outputs.json",
99
"build": "cdk synth -c environment=test",
10+
"deploy-yaml": "bash deploy-yaml.sh",
1011
"destroy": "cdk destroy --all -c environment=test --force"
1112
},
1213
"devDependencies": {

0 commit comments

Comments
 (0)