繁体   English   中英

aws cdk lambda,appconfig typescript 示例好吗?

[英]aws cdk lambda, appconfig typescript example please?

任何人都可以提供或指向一个 AWS-CDK/typescript 示例来配置 lambda 使用的 AWS AppConfig app/env/config 吗? 在网上找不到任何有意义的东西。

这个派对有点晚了,如果有人来这个问题,寻找快速答案,这里是 go:

AppConfig 没有任何正式的 L2 构造

  • 这意味着必须使用当前在此处出售的 L1 结构
  • 这对于编写 L2/L3 结构的人来说已经成熟了(我正在努力尽快销售这个自定义结构——所以请留意这里的更新)。
  • 这并不意味着很难在 CDK 中使用 AppConfig,只是与 L2/L3 结构不同,我们必须更深入地研究设置 AppConfig 来阅读他们的文档。

一个非常简单的例子 (YMMV)

这是我必须设置 AppConfig 的自定义构造:

import {
  CfnApplication,
  CfnConfigurationProfile,
  CfnDeployment,
  CfnDeploymentStrategy,
  CfnEnvironment,
  CfnHostedConfigurationVersion,
} from "aws-cdk-lib/aws-appconfig";
import { Construct } from "constructs";

// 1. https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_appconfig-readme.html - there are no L2 constructs for AppConfig.
// 2. https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-working.html- this the CDK code from this console setup guide.
export class AppConfig extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id);
    // create a new app config application.
    const testAppConfigApp: CfnApplication = new CfnApplication(
      this,
      "TestAppConfigApp",
      {
        name: "TestAppConfigApp",
      }
    );

    // you can customize this as per your needs.
    const immediateDeploymentStrategy = new CfnDeploymentStrategy(
      this,
      "DeployStrategy",
      {
        name: "ImmediateDeployment",
        deploymentDurationInMinutes: 0,
        growthFactor: 100,
        replicateTo: "NONE",
        finalBakeTimeInMinutes: 0,
      }
    );

    // setup an app config env
    const appConfigEnv: CfnEnvironment = new CfnEnvironment(
      this,
      "AppConfigEnv",
      {
        applicationId: testAppConfigApp.ref,
        // can be anything that makes sense for your use case.
        name: "Production",
      }
    );

    // setup config profile
    const appConfigProfile: CfnConfigurationProfile = new CfnConfigurationProfile(
      this,
      "ConfigurationProfile",
      {
        name: "TestAppConfigProfile",
        applicationId: testAppConfigApp.ref,
        // we want AppConfig to manage the configuration profile, unless we need from SSM or S3.
        locationUri: "hosted",
        // This can also be "AWS.AppConfig.FeatureFlags"
        type: "AWS.Freeform",
      }
    );

    // Update AppConfig
    const configVersion: CfnHostedConfigurationVersion = new CfnHostedConfigurationVersion(
      this,
      "HostedConfigurationVersion",
      {
        applicationId: testAppConfigApp.ref,
        configurationProfileId: appConfigProfile.ref,
        content: JSON.stringify({
          someAppConfigResource: "SomeAppConfigResourceValue",
          //... add more as needed.
        }),
        // https://www.rfc-editor.org/rfc/rfc9110.html#name-content-type
        contentType: "application/json",
      }
    );

    // Perform deployment.
    new CfnDeployment(this, "Deployment", {
      applicationId: testAppConfigApp.ref,
      configurationProfileId: appConfigProfile.ref,
      configurationVersion: configVersion.ref,
      deploymentStrategyId: immediateDeploymentStrategy.ref,
      environmentId: appConfigEnv.ref,
    });
  }
}

这是我的 lambda 处理程序中的内容(请注意,您应该为 AppConfig 扩展启用 lambda 层,请在此处查看更多信息):

const http = require('http');

exports.handler = async (event) => {
  
  const res = await new Promise((resolve, reject) => {
    http.get(
      "http://localhost:2772/applications/TestAppConfigApp/environments/Production/configurations/TestAppConfigProfile", 
      resolve
    );
  });
  let configData = await new Promise((resolve, reject) => {
    let data = '';
    res.on('data', chunk => data += chunk);
    res.on('error', err => reject(err));
    res.on('end', () => resolve(data));
  });
  const parsedConfigData = JSON.parse(configData);    
  return parsedConfigData;
};

我没有使用 AWS AppConfig 的经验。 但我会从这里开始

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM