【问题标题】:Setting API Key required to true using serverless.template in AWS API Gateway在 AWS API Gateway 中使用 serverless.template 将 API Key 设置为 true
【发布时间】:2019-07-21 14:38:12
【问题描述】:

我正在 AWS Lambda 上部署一个 ASP.Net Core 项目,但我正在努力制作所需的 API 密钥。 这是我的 Json 模板:

{
  "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" : {
    "ShouldCreateBucket" : {
      "Type" : "String",        
      "AllowedValues" : ["true", "false"],
      "Description" : "If true then the S3 bucket that will be proxied will be created with the CloudFormation stack."
    },
    "BucketName" : {
        "Type" : "String",
        "Description" : "Name of S3 bucket that will be proxied. If left blank a name will be generated.",
        "MinLength" : "0"
    }
  },

  "Conditions" : {
    "CreateS3Bucket" : {"Fn::Equals" : [{"Ref" : "ShouldCreateBucket"}, "true"]},
    "BucketNameGenerated" : {"Fn::Equals" : [{"Ref" : "BucketName"}, ""]}
  },

  "Resources" : {
    "AspNetCoreFunction" : {
      "Type" : "AWS::Serverless::Function",
      "Properties": {
        "Handler": "SmartClockAPI::SmartClockAPI.LambdaEntryPoint::FunctionHandlerAsync",
        "Runtime": "dotnetcore2.1",
        "CodeUri": "",
        "MemorySize": 256,
        "Timeout": 30,
        "Role": null,
        "Policies": [ "AWSLambdaFullAccess","AmazonCognitoPowerUser","AmazonAPIGatewayAdministrator"],
        "Environment" : {
          "Variables" : {
            "AppS3Bucket" : { "Fn::If" : ["CreateS3Bucket", {"Ref":"Bucket"}, { "Ref" : "BucketName" } ] }
          }       
        },
        "Events": {
          "PutResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/{proxy+}",
              "Method": "ANY"
            }
          }
        }
      }
    },
    "BasicUsagePlan" : {
        "Type" : "AWS::ApiGateway::UsagePlan",
        "Properties" : {
        "UsagePlanName" : "Basic plan",
        "Quota" : {
        "Limit" : 100,
        "Period" : "MONTH"
        },
        "Throttle" : {
        "RateLimit" : 10,
        "BurstLimit" : 10
        }
        }
    },
    "Bucket" : {
        "Type" : "AWS::S3::Bucket",
        "Condition" : "CreateS3Bucket",
        "Properties" : {
            "BucketName" : { "Fn::If" : ["BucketNameGenerated", {"Ref" : "AWS::NoValue" }, { "Ref" : "BucketName" } ] }
        }
    }
  },

  "Outputs" : {
    "ApiURL" : {
        "Description" : "API endpoint URL for Prod environment",
        "Value" : { "Fn::Sub" : "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/" }
    },
    "S3ProxyBucket" : {
        "Value" : { "Fn::If" : ["CreateS3Bucket", {"Ref":"Bucket"}, { "Ref" : "BucketName" } ] }
    }
  }
}

我想要实现的是从 Json 模板中将此值设置为 true。

我期待 Proxy 有一些额外的属性,我可以在其中指定这个值。 有什么想法吗?

【问题讨论】:

    标签: asp.net-core aws-lambda amazon-cloudformation


    【解决方案1】:

    Use AWS::ApiGateway::UsagePlanKey

    {
      "Type" : "AWS::ApiGateway::UsagePlanKey",
      "Properties" : {
        "KeyId" : String,
        "KeyType" : String,
        "UsagePlanId" : String
      }
    }
    

    将 DependsOn 添加到 ApiKey、ApiUsagePlan 和 ApiUsagePlanKey 以确保它们以正确的顺序创建。这是一个很好的工作example

    【讨论】:

      猜你喜欢
      • 2019-09-30
      • 2022-08-02
      • 2020-09-21
      • 1970-01-01
      • 2021-08-14
      • 2020-01-14
      • 2015-11-09
      • 2022-06-10
      • 1970-01-01
      相关资源
      最近更新 更多