diff --git a/.github/workflows/common-test.yml b/.github/workflows/common-test.yml index fee70cc..59fc90f 100644 --- a/.github/workflows/common-test.yml +++ b/.github/workflows/common-test.yml @@ -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 diff --git a/src/cloudFormation.ts b/src/cloudFormation.ts index ddba4bd..a6e0229 100644 --- a/src/cloudFormation.ts +++ b/src/cloudFormation.ts @@ -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; @@ -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') { diff --git a/test/cdk-basic/deploy-yaml.sh b/test/cdk-basic/deploy-yaml.sh new file mode 100644 index 0000000..0dea01d --- /dev/null +++ b/test/cdk-basic/deploy-yaml.sh @@ -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 \ No newline at end of file diff --git a/test/cdk-basic/package.json b/test/cdk-basic/package.json index 76bfdf5..1bfc0f8 100644 --- a/test/cdk-basic/package.json +++ b/test/cdk-basic/package.json @@ -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": {