【问题标题】:How to secure AWS API Gateway with Access and Secret Keys in CloudFormation?如何使用 CloudFormation 中的访问密钥和密钥保护 AWS API Gateway?
【发布时间】:2018-10-21 06:52:25
【问题描述】:

我使用 AWS Toolkit for Visual Studio 模板创建了无服务器 Lambda 应用程序(我使用了Tutorial: Build and Test a Serverless Application with AWS Lambda)。我选择了“空无服务器项目”并创建了简单的lambda function 链接到API Gateway

CloudFormation 模板如下所示:

{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Transform" : "AWS::Serverless-2016-10-31",
  "Description" : "An AWS Serverless Application.",

  "Resources" : {

    "Get" : {
      "Type" : "AWS::Serverless::Function",
      "Properties": {
        "Handler": "AWSServerless::AWSServerless.Functions::Get",
        "Runtime": "dotnetcore2.0",
        "CodeUri": "",
        "MemorySize": 256,
        "Timeout": 30,
        "Role": null,
        "Policies": [ "AWSLambdaBasicExecutionRole" ],
        "Events": {
          "PutResource": {
            "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 Gateway。我进行了一些调查,如果我是正确的,它应该如下所示:

"security":[{"sigv4":[]}]

但我仍然不清楚我应该在哪里应用它?可能我错了,可以用另一种方式完成。所以我的问题是:

如何使用 CloudFormation 中的访问密钥和密钥保护 API Gateway

【问题讨论】:

    标签: amazon-web-services aws-lambda amazon-cloudformation serverless aws-serverless


    【解决方案1】:

    您可以使用API keyAuthorizers

    以下示例创建一个自定义授权方,它是一个 AWS Lambda 函数。

    "Authorizer": {
      "Type": "AWS::ApiGateway::Authorizer",
      "Properties": {
        "AuthorizerCredentials": { "Fn::GetAtt": ["LambdaInvocationRole", "Arn"] },
        "AuthorizerResultTtlInSeconds": "300",
        "AuthorizerUri" : {"Fn::Join" : ["", [
          "arn:aws:apigateway:",
          {"Ref" : "AWS::Region"},
          ":lambda:path/2015-03-31/functions/",
          {"Fn::GetAtt" : ["LambdaAuthorizer", "Arn"]}, "/invocations"
        ]]},
        "Type": "TOKEN",
        "IdentitySource": "method.request.header.Auth",
        "Name": "DefaultAuthorizer",
        "RestApiId": {
          "Ref": "RestApi"
        }
      }
    }
    

    (更新)
    关于如何在模板中使用授权者的 SO 线程

    Reference an Authorizer definition in an API Gateway path

    【讨论】:

    • 好点。我看到了这个,但坦率地说,我不知道我必须在模板的哪个部分应用它。
    • 用“如何在 cloudformation 模板中使用 api-gateway 授权方”的信息更新了我的答案:) 希望对您有所帮助
    猜你喜欢
    • 2020-09-25
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 2019-11-29
    相关资源
    最近更新 更多