繁体   English   中英

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

[英]How to secure AWS API Gateway with Access and Secret Keys in CloudFormation?

我使用适用于Visual Studio的AWS工具包模板创建了无服务器Lambda应用程序(我使用了教程:使用AWS Lambda构建和测试无服务器应用程序 )。 我选择了“空无服务器项目”,并创建了链接到API Gateway的简单lambda函数

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网关 我已经调查了一下,如果我是正确的话,应该看起来像下一个:

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

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

如何使用CloudFormation中的访问和秘密密钥保护API网关

您可以使用API密钥授权者

以下示例创建一个自定义授权人,该人是一个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线程

在API网关路径中引用授权者定义

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM