使用CDK V1创建AWS AppConfig特性标志时遇Content格式错误
AWS AppConfig托管配置版本Content格式错误修复
错误提示
MyCfnHostedConfigurationVersion Error invoking extension AppConfig Feature Flags Helper: Malformed 'Content' provided (Service: AmazonAppConfig; Status Code: 400; Error Code: BadRequestException; Request ID: f9496e9f-db25-438b-a7bb-f030351eb251; Proxy: null)
问题场景
用Node.js版CDK V1搭建AWS AppConfig全链路资源(应用、环境、配置文件、托管配置、部署策略、部署)时,因CfnHostedConfigurationVersion的Content格式不符合Feature Flags规范,导致部署失败,尝试现有方案后问题未解决。
当前代码
// 创建应用 const cfnAppConfigApplication = new appconfig.CfnApplication(this, 'MyCfnApplication', { name: 'document-vault-app-config-app', description: 'App to maintain environment variables for Document Vault.', }); // 创建环境 const cfnEnvironment = new appconfig.CfnEnvironment(this, 'MyCfnEnvironment', { applicationId: cfnAppConfigApplication.ref, name: props.environmentName, description: 'Environment to deploy the configuration to.', }); // 创建配置文件 const cfnConfigurationProfile = new appconfig.CfnConfigurationProfile(this, 'MyCfnConfigurationProfile', { applicationId: cfnAppConfigApplication.ref, locationUri: 'hosted', name: 'cp-migration-feature-flag-app-config-profile', // 可选属性 description: 'Profile to store CP migration feature flags.', type: 'AWS.AppConfig.FeatureFlags' }); const configVersion = Date.now(); const base64FeatureFlagsContent = Buffer.from(JSON.stringify( { "flags": { "flagkey": { "name": "cp_migration_feature_flags" } }, "values": { "flagkey": { "enabled": false } }, "version": "1" } )).toString('base64'); const cfnHostedConfigurationVersion = new appconfig.CfnHostedConfigurationVersion(this, 'MyCfnHostedConfigurationVersion', { applicationId: cfnAppConfigApplication.ref, configurationProfileId: cfnConfigurationProfile.ref, content: base64FeatureFlagsContent, contentType: 'application/json', // 可选属性 description: 'Hosted configuration for CP migration feature flags.', latestVersionNumber: 1, }); // 创建部署策略 const cfnDeploymentStrategy = new appconfig.CfnDeploymentStrategy(this, 'MyCfnDeploymentStrategy', { deploymentDurationInMinutes: 3, growthFactor: 100, name: 'document-vault-app-deployment-strategy', replicateTo: 'NONE', description: 'Strategy to deploy document vault configuration', finalBakeTimeInMinutes: 4, growthType: 'LINEAR' }); const cfnDeployment = new appconfig.CfnDeployment(this, 'MyCfnDeployment', { applicationId: cfnAppConfigApplication.ref, configurationProfileId: cfnConfigurationProfile.ref, configurationVersion: "1", deploymentStrategyId: cfnDeploymentStrategy.ref, environmentId: cfnEnvironment.ref, // 可选属性 description: 'Deployment of document vault configuration', });
修复方案
问题核心是Feature Flags的JSON结构不符合AWS AppConfig的校验规则,需做以下调整:
flags对象的key必须与对应flag的name字段完全一致version字段需为数字类型,不能是字符串- 保持Base64编码正确(CDK要求Content为Base64格式)
修改后的Content生成代码:
const base64FeatureFlagsContent = Buffer.from(JSON.stringify( { "flags": { "cp_migration_feature_flags": { "name": "cp_migration_feature_flags", "description": "控制CP迁移流程的功能开关" // 可选,增强可读性 } }, "values": { "cp_migration_feature_flags": { "enabled": false } }, "version": 1 // 改为数字类型 } )).toString('base64');
额外注意事项:
- 配置文件的
type已正确设置为AWS.AppConfig.FeatureFlags,无需修改 - 部署资源的
configurationVersion需与托管配置的latestVersionNumber保持一致(当前均为1,符合要求) contentType保持application/json即可
修改后重新执行cdk deploy即可解决格式错误问题。
内容的提问来源于stack exchange,提问作者kpajbell
相关产品推荐
相关产品推荐

