首页 > 解决方案 > aws ressource limit api - 如何在 RestApi 上分离堆栈

问题描述

当我使用 aws-cdk 来定义 aws 堆栈时,我在这里遇到了一些限制,我不知道如何解决它。

首先,每个堆栈有 200 个资源的不可更改限制。这意味着,cloudformation 中使用的每个堆栈限制为 200 个。

长期以来,这不是问题,因为很容易创建一个包含多个不同用途的堆栈(S3、ApiGateway、IoT 等)的堆栈。

现在我遇到了这个问题,APIGatewayStack(用于 RestApi)超过了 200 个资源的限制,我无法再部署了。

我已经将 lambdas 与 api- 端点分开了,所以我没有任何可能在这里获得一些资源。

我的想法是为 RestApi 或其根资源创建一个 CfnOutput,所以我可以创建一个 Restapi 对象

方法 #1 - 从 RestApiId 创建 RestApi

堆栈#1

const outputGm = new CfnOutput(this, 'RestApiGatewayId', {
         description: 'RestApiGatewayId',
         value: api.restApiId,
         // This Name must be unique
         exportName: 'RestApiGatewayId',
      });

堆栈#2

const api = RestApi.fromRestApiId(this, 'rest-api-extension', 'RestApiGatewayId');

这里的问题是,该 api 现在来自 IRestApi 类型,它不能用于进一步需要的修改,例如

const models = new ModelDefinitions.ModelDefinitions(api);
// Argument of type 'IRestApi' is not assignable to parameter of type 'RestApi'.

或者

const notifications = api.root.addResource('notifications');
// Property 'root' does not exist on type 'IRestApi'.

方法 #2 - 创建根资源的输出

不幸的是,创建一个 RestApifromRestApiId并返回一个 IRestApi 对象,它不包含所有必要的属性来使用它来创建资源:

const api = RestApi.fromRestApiId(this, 'rest-api-extension', 'RestApiGatewayId');
const resourceProps: ResourceProps = {
         parent: api,
         /* 
          * Type 'IRestApi' is missing the following properties from type 'IResource': restApi, resourceId, path, resourceForPath, and 5 more.
          */
         pathPart: 'root',
      };
const root = new Resource(apiGWStack, 'root-node', resourceProps);
// apiGWStack is the original stack which i am passing to the extension via:

const apiGatewayExtensionStack = new ApiGatewayExtensionStack(app, 'ApiGatewayExtensionStack', apiGWStack, { env: environment });

我在这里想念什么?我不想使用第二个(或将来第三个)单独的 Rest-Api,因为它有很多端点。

您在这里看到任何解决方案吗?您如何处理 AWS 上的 Big Api 结构?

感谢帮助!

标签: amazon-web-servicesamazon-cloudformationaws-cdk

解决方案


好的,所以 aws 没有增加这个 Cloudformation 资源限制。由于无法连接,因此您也无法进行嵌套的 Rest-Api Stack。

但我无法接受没有任何解决方案的事实。我记得,您可以在 nginx 上使用 ngx_http_proxy_module 来提供来自另一台服务器的内容。所以我检查了这是否也适用于 aws-gateway。

更好的是,它可以用于多个用例:

(请注意,我删除了此 conf 中对 ssl-certificate 的所有引用)

/etc/nginx/sites-available/yourdomain.tld.conf


# server configuration
#
server {

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name yourdomain.tld;

    add_header "Allow" "GET, POST, HEAD, PUT, DELETE" always;
    add_header "Access-Control-Allow-Methods" "GET, POST, PUT, DELETE, OPTIONS" always;

    #
    # Default Location will be mapped to proxy_set_header HOST  
    # API #1 - simple forwarding on aws-gateway

    location / {
        proxy_next_upstream     error timeout invalid_header http_500;                  
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass https://abcdefghij.execute-api.eu-central-1.amazonaws.com/prod/;
        proxy_set_header Host "abcdefghij.execute-api.eu-central-1.amazonaws.com";
        proxy_ssl_server_name on;

    }

    #
    # Location /admin and nested below will be mapped to proxy_set_header HOST  
    # API #2 - speficic slug forwarding on on aws-gateway

    location /admin {
        proxy_next_upstream     error timeout invalid_header http_500;          
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass https://zyxwvutsrq.execute-api.eu-central-1.amazonaws.com/prod/;
        proxy_set_header Host "zyxwvutsrq.execute-api.eu-central-1.amazonaws.com";
        proxy_ssl_server_name on;
    }

    #
    # Location /users and nested below will be mapped to proxy_set_header HOST  
    # API #3 - specific slug mapping to specific slug on aws-gateway

    location /users {
        proxy_next_upstream     error timeout invalid_header http_500;          
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass https://ababababab.execute-api.eu-central-1.amazonaws.com/prod/users;
        proxy_set_header Host "ababababab.execute-api.eu-central-1.amazonaws.com";
        proxy_ssl_server_name on;
    }
}

server {

    if ($host = yourdomain.tld) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name yourdomain.tld;
    listen 80;
    return 404; # managed by Certbot
}

前:

现在:

如果您有

  • 发展
  • 分期
  • 生产服务器

因为当有新端点时您可能需要更新所有 conf 文件,但这样您也可以从使用漂亮 url 提供 REST-Api 中受益

https://yourdomain.tld而不是那些https://ababababab.execute-api.eu-central-1.amazonaws.com/prod/


推荐阅读