You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于.NET 6与AWS构建部署RESTful API的Serverless模板实践

.NET 6 + AWS 无服务器RESTful API构建部署方案分享

我花了一整天研究如何用.NET 6和AWS构建并部署RESTful API,最头疼的部分是搞懂serverless.template里的变量和各类配置。所以决定把自己整合所有组件的方案分享出来,希望能帮到有同样需求的开发者。

我知道用AWS::Serverless::Api有更简便的实现方式,但它支持的自定义配置有限,满足不了我的需求。以下是我使用的serverless.template内容:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Description": "An AWS Serverless Application that uses the ASP.NET Core framework running in Amazon Lambda.",
  "Parameters": {},
  "Conditions": {},
  "Resources": {
    "MySampleWebApi": {
      "Type": "AWS::ApiGateway::RestApi",
      "Properties": {
        "ApiKeySourceType": "HEADER",
        "Description": "My Sample Web api",
        "EndpointConfiguration": {
          "Types": ["REGIONAL"]
        },
        "Name": "my-sample-webapi"
      }
    },
    "ApiResource": {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "RestApiId": {
          "Ref": "MySampleWebApi"
        },
        "ParentId": {
          "Fn::GetAtt": ["MySampleWebApi", "RootResourceId"]
        },
        "PathPart": "api"
      }
    },
    "UsersResource": {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "RestApiId": {
          "Ref": "MySampleWebApi"
        },
        "ParentId": {
          "Ref": "ApiResource"
        },
        "PathPart": "users"
      }
    },
    "UsersBatchResource": {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "RestApiId": {
          "Ref": "MySampleWebApi"
        },
        "ParentId": {
          "Ref": "UsersResource"
        },
        "PathPart": "batch"
      }
    },
    "UsersBatchUploadMeth": {
      "Type": "AWS::ApiGateway::Method",
      "Properties": {
        "RestApiId": {
          "Ref": "MySampleWebApi"
        },
        "ResourceId": {
          "Ref": "UsersBatchResource"
        },
        "ApiKeyRequired": true,
        "AuthorizationType": "NONE",
        "HttpMethod": "POST",
        "OperationName": "Users Batch Upload",
        "Integration": {
          "Type": "AWS_PROXY",
          "IntegrationHttpMethod": "POST",
          "Uri": {
            "Fn::Join": ["", ["arn:aws:apigateway:", {"Ref": "AWS::Region"}, ":lambda:path/2015-03-31/functions/", {"Fn::GetAtt": ["AspNetCoreFunction", "Arn"]}, "/invocations"]]
          }
        }
      }
    },
    "WebApiDevelopmentDeploy": {
      "Type": "AWS::ApiGateway::Deployment",
      "Properties": {
        "Description": "Deploy Development",
        "RestApiId": {
          "Ref": "MySampleWebApi"
        },
        "StageName": "DEV"
      },
      "DependsOn": ["UsersBatchUploadMeth"]
    },
    "AspNetCoreFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "Handler": "SampleApp.LambdaApp.WebAPI::SampleApp.LambdaApp.WebAPI.LambdaEntryPoint::FunctionHandlerAsync",
        "Runtime": "dotnet6",
        "CodeUri": "",
        "MemorySize": 256,
        "Timeout": 30,
        "Role": null,
        "Environment": {
          "Variables": {
            "ASPNETCORE_ENVIRONMENT": "Development",
            "DOTNET_ENVIRONMENT": "Development"
          }
        },
        "VpcConfig": {
          "SecurityGroupIds": ["sg-#####"],
          "SubnetIds": ["sn-#####", "sn-####"]
        },
        "Policies": ["AWSLambda_FullAccess"],
        "Events": {
          "ApiEvent": {
            "Type": "Api",
            "Properties": {
              "Auth": {
                "ApiKeyRequired": true
              },
              "Path": "/api/users/batch",
              "Method": "POST",
              "RestApiId": {
                "Ref": "MySampleWebApi"
              }
            }
          }
        }
      }
    }
  },
  "Outputs": {
    "ApiURL": {
      "Description": "API endpoint URL for Prod environment",
      "Value": {
        "Fn::Sub": "https://${MySampleWebApi}.execute-api.${AWS::Region}.amazonaws.com/dev"
      }
    }
  }
}

内容的提问来源于stack exchange,提问作者Jay Peterson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 19:40:23