【问题标题】:C# AWS CDK Serverless, publish new version Lambda and create new API resourceC# AWS CDK Serverless,发布新版本 Lambda 并创建新 API 资源
【发布时间】:2021-03-31 08:38:55
【问题描述】:

我是 AWS CDK 的新手,我想要完成的工作(使用 C#)是对我的 lambda 函数进行版本控制,然后创建一个引用该版本的新 API 资源。

例如:程序接受版本参数。

    internal CdkAppStack(Construct scope, string id, IStackProps props, string bucketName, string functionName, string version) : base(scope, id, props)
    {
        var bucket = new Bucket(this, bucketName, new BucketProps { 
            BucketName = bucketName
        });

        var handler = new Function(this, $"{functionName}Handler", new FunctionProps
        {
            Runtime = Runtime.DOTNET_CORE_3_1,
            Code = Code.FromAsset("Lambdas\\src\\Lambdas\\bin\\Debug\\netcoreapp3.1"),
            Handler = "Lambdas::Lambdas.Function::FunctionHandler",
            Environment = new Dictionary<string, string>
            {
                ["BUCKET"] = bucket.BucketName,
            },
            FunctionName = functionName
        });

        string apiName = !string.IsNullOrEmpty(version) ? $"{functionName}-{version}" : functionName;

        bucket.GrantReadWrite(handler);

        var api = new RestApi(this, $"{apiName}-API", new RestApiProps
        {
            RestApiName = $"{apiName}API",
            Description = $"This service the Lambda - {functionName}.",
            RetainDeployments = true
        });

        var getWidgetsIntegration = new LambdaIntegration(handler, new LambdaIntegrationOptions
        {
            RequestTemplates = new Dictionary<string, string>
            {
                ["application/json"] = "{ \"statusCode\": \"200\" }"
            }
        });

        string resource = !string.IsNullOrEmpty(version) ? $"execute-{version}" : "execute";

        var helloWorldResource = api.Root.AddResource(resource);
        var method = helloWorldResource.AddMethod("POST", getWidgetsIntegration);

    }

实际结果:

它覆盖了 lambda 和 api 资源。

预期结果:(版本参数为3)。添加了新资源(execute-3)

AWS Console - Expected result

【问题讨论】:

    标签: c# amazon-web-services aws-cdk aws-serverless


    【解决方案1】:

    对于给定的代码,过程可能如下所示:

    1. 当前配置状态包括execute-2
    2. CDK 合成一个 Cloudformation 模板,其中 execute-3 存在且 execute-2 不存在。
    3. Cloudformation 发现,为了达到所需状态(在模板中描述),它需要delete execute-2provision execute-3

    如果您确实想达到您所描述的内容,则必须添加资源并且不删除以前配置的资源(比如编写一个运行超过 1..version 并添加所有过去版本的循环)

    另一个(不太推荐的)选项是使用deletion policy。通过这种方式,您可以提示 cloudformation 而不是删除 execute-2(这里的缺点是 execute-2 的生命周期不再由任何堆栈管理 --> 您必须手动管理对它的更改)

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 1970-01-01
      • 2021-01-05
      • 2013-01-27
      • 2022-11-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-08
      • 2020-09-25
      相关资源
      最近更新 更多