我有一个 .NET 8 Core Minimal API,通过将
builder.Services.AddAWSLambdaHosting(LambdaEventSource.RestApi);
添加到我的 Program.cs
Main
方法中的初始化代码中,我已将其转换为 Lambda。
我还创建了一些 CDK 代码来引导/部署基础设施到我的 AWS 实例(在我的例子中,我在 Docker 中使用 LocalStack,而不是实时 AWS 环境)。我为此使用 Typescript,因为这是我组织的 DevOps 团队更喜欢的。
我的
\lib\*cdk-stack.ts
文件如下所示(我已将组织特定的应用程序名称/命名空间替换为通用名称,以保护我的组织的隐私):
import * as cdk from 'aws-cdk-lib';
import { Lambda } from 'aws-cdk-lib/aws-ses-actions';
import * as lambda from "aws-cdk-lib/aws-lambda";
import { Construct } from 'constructs';
export class MyFunctionCdkStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const functionProps: lambda.FunctionProps = {
runtime: lambda.Runtime.DOTNET_8,
code: lambda.Code.fromAsset('../MyFunctionAssemblyName/bin/Release/net8.0/publish'),
handler: 'MyFunctionAssemblyName',
timeout: cdk.Duration.seconds(600)
};
new lambda.Function(this, "MyFunction", functionProps);
}
}
我的
\bin\*cdk.ts
文件定义如下:
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { MyFunctionCdkStack } from '../lib/my-app-name.cdk-stack';
const app = new cdk.App();
new MyFunctionCdkStack(app, 'MyFunctionCdkStack', {
/* If you don't specify 'env', this stack will be environment-agnostic.
* Account/Region-dependent features and context lookups will not work,
* but a single synthesized template can be deployed anywhere. */
/* Uncomment the next line to specialize this stack for the AWS Account
* and Region that are implied by the current CLI configuration. */
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
/* Uncomment the next line if you know exactly what Account and Region you
* want to deploy the stack to. */
// env: { account: '123456789012', region: 'us-east-1' },
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
});
我能够将此堆栈部署到 LocalStack,但仅如果我的主项目包含正确填充的
serverless.template
文件。但是,据我了解,如果配置正确,CDK 就不需要 serverless.template
。这是真的?还是我还需要serverless.template
?
如果我确实能够取消
serverless.template
并在我的 CDK 代码中定义所有基础设施,我该怎么做?换句话说,CDK Typescript 与 serverless.template
中定义的属性等效是什么?
作为参考,我在这里添加了我的
serverless.template
(再次混淆了识别信息):
{
"AWSTemplateFormatVersion": "2010-09-09",
"Transform": "AWS::Serverless-2016-10-31",
"Description": "An AWS Serverless Application. This template is partially managed by Amazon.Lambda.Annotations (v1.2.0.0).",
"Resources": {
"MyFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "MyFunctionAssemblyName",
"Runtime": "dotnet8",
"MemorySize": 256,
"Timeout": 600,
"Role": null,
"Policies": [
"AWSLambda_FullAccess"
],
"Environment": {
"Variables": {
"POWERTOOLS_SERVICE_NAME": "MyService",
"POWERTOOLS_LOG_LEVEL": "Info",
"POWERTOOLS_LOGGER_CASE": "PascalCase",
"POWERTOOLS_TRACER_CAPTURE_RESPONSE": true,
"POWERTOOLS_TRACER_CAPTURE_ERROR": true,
"POWERTOOLS_METRICS_NAMESPACE": "MyServiceFunction"
}
},
"Events": {
"ProxyResource": {
"Type": "Api",
"Properties": {
"Path": "/{proxy+}/path",
"Method": "ANY"
}
},
"RootResource": {
"Type": "Api",
"Properties": {
"Path": "/path",
"Method": "GET"
}
}
}
}
}
},
"Outputs": {
"ApiURL": {
"Description": "API endpoint URL for Prod environment",
"Value": {
"Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
}
}
}
}
重申一下:我的问题是是否可以通过在我的 CDK Typescript 文件中定义所有内容来消除对
serverless.template
的需求。
serverless.template
适用于 AWS SAM 框架,与 AWS CDK 无关。
您应该使用 AWS SAM 或 AWS CDK。所以,如果你使用 CDK,你绝对可以删除
serverless.template
文件。
要部署 API Gateway 以及托管 .NET API 的 Lambda,您可以参考我在 ServerlessLand 上发布的创建 Amazon API Gateway REST API to AWS Lambda 模式,您可以在其中引用相同的 CDK 代码。 CDK 代码位于 .NET 中,但您可以轻松编写等效的 TypeScript。
CDK 部署可以在 AWS 云上运行,但我尚未针对 LocalStack 进行测试。