使用AWS API Gateway中的serverless.template将必需的API密钥设置为true

时间:2019-02-27 19:11:42

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

我正在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。

API Key required settings

我期望代理具有一些额外的属性,可以在其中指定此值。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

Use AWS::ApiGateway::UsagePlanKey

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

将DependsOn添加到ApiKey,ApiUsagePlan和ApiUsagePlanKey,以确保它们以正确的顺序创建。这是一个不错的example