.net 中带有无服务器注释的 AWS Lambda:如何需要 API 密钥?

问题描述 投票:0回答:1

我启动了一个使用 AWS Lambda Annotations 的空白无服务器应用程序项目。它生成了以下

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.0.0.0).",
  "Resources": {
    "MyAppFunctionsGetGenerated": {
      "Type": "AWS::Serverless::Function",
      "Metadata": {
        "Tool": "Amazon.Lambda.Annotations",
        "SyncedEvents": [
          "RootGet"
        ]
      },
      "Properties": {
        "Runtime": "dotnet6",
        "CodeUri": ".",
        "MemorySize": 256,
        "Timeout": 30,
        "Policies": [
          "AWSLambdaBasicExecutionRole"
        ],
        "PackageType": "Zip",
        "Handler": "MyApp::MyApp.Functions_Get_Generated::Get",
        "Events": {
          "RootGet": {
            "Type": "Api",
            "Properties": {
              "Path": "/",
              "Method": "GET"
            }
          }
        }
      }
    }
  },
  "Outputs": {
    "ApiURL": {
      "Description": "API endpoint URL for Prod environment",
      "Value": {
        "Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
      }
    }
  }
}

我应该如何以及在哪里放置需要 API 密钥的配置内容?我尝试了在这里找到的不同方法,但它们不起作用,它们只适用于没有注释的旧方法。

amazon-web-services aws-cloudformation aws-api-gateway
1个回答
0
投票

回答我自己的问题以便稍后能够接受。

由于注释方法会在后台生成很多无法直接访问的内容,包括 API,因此您必须依靠

Fn::Sub
来获取链接到使用计划所需的 API Id。

然后,您可以使用默认的Usage Plan、API Key、Usage Plan Key路由。

这是我拼凑的:

{
  "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.0.0.0).",
  "Resources": {
    "MyApiKey": {
      "Type": "AWS::ApiGateway::ApiKey",
      "Properties": {
        "Enabled": true
      }
    },
    "MyUsagePlan": {
      "Type": "AWS::ApiGateway::UsagePlan",
      "Properties": {
        "ApiStages": [
          {
            "ApiId": { "Fn::Sub" : "${ServerlessRestApi}" },
            "Stage": "Prod"
          },
          {
            "ApiId": { "Fn::Sub" : "${ServerlessRestApi}" },
            "Stage": "Stage"
          }
        ]
      }
    },
    "MyUsagePlanKey": {
      "Type": "AWS::ApiGateway::UsagePlanKey",
      "Properties": {
        "KeyId": {
          "Ref": "MyApiKey"
        },
        "KeyType": "API_KEY",
        "UsagePlanId": {
          "Ref": "MyUsagePlan"
        }
      }
    },
    "MyAppFunctionsGetGenerated": {
      "Type": "AWS::Serverless::Function",
      "Metadata": {
        "Tool": "Amazon.Lambda.Annotations",
        "SyncedEvents": [
          "RootGet"
        ]
      },
      "Properties": {
        "Runtime": "dotnet6",
        "CodeUri": ".",
        "MemorySize": 256,
        "Timeout": 30,
        "Policies": [
          "AWSLambdaBasicExecutionRole"
        ],
        "PackageType": "Zip",
        "Handler": "MyApp::MyApp.Functions_Get_Generated::Get",
        "Events": {
          "RootGet": {
            "Type": "Api",
            "Properties": {
              "Path": "/",
              "Method": "GET",
              "Auth": {
                "ApiKeyRequired": true
              }
            }
          }
        }
      }
    }
  },
  "Outputs": {
    "ApiURL": {
      "Description": "API endpoint URL for Prod environment",
      "Value": {
        "Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.