【问题标题】:How to create a private AWS Api Gateway using cloudformation?如何使用 cloudformation 创建私有 AWS Api 网关?
【发布时间】:2023-03-06 19:03:02
【问题描述】:

我正在尝试创建 PRIVATE 类型的 AWS API 网关,
这需要一个资源策略,因为我能够从 AWS 控制台创建网关,所以我拥有该策略,
我想知道如何通过 CF 模板添加资源策略 -

以下是资源策略的swagger定义-

x-amazon-apigateway-policy:
  Version: "2012-10-17"
  Statement:
  - Effect: "Deny"
    Principal: "*"
    Action: "execute-api:Invoke"
    Resource: "arn:aws:execute-api:us-east-1:awsAccountId:xxxx/*/*/*"
    Condition:
      StringNotEquals:
        aws:sourceVpc: "vpc-xxxxx"
  - Effect: "Allow"
    Principal: "*"
    Action: "execute-api:Invoke"
    Resource: "arn:aws:execute-api:us-east-1:xxxx:xxxx/*/*/*"

如何在CF模板中配置-

AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::Serverless-2016-10-31'
Description: G2G Api Template Stack

Resources:
   g2gPrivate:
    Type: 'AWS::ApiGateway::RestApi'
    Properties:
      Name: 'private-gw'
      EndpointConfiguration:
        Types:
          - PRIVATE

参考 -
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-restapi.html

https://medium.com/@cathmgarcia/conditional-resource-policy-on-aws-sam-with-inline-swagger-816ce946dbb

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation aws-api-gateway


    【解决方案1】:

    您需要在密钥下提供策略(称为Policy,与Name 处于同一级别。

    https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-restapi.html#cfn-apigateway-restapi-policy

    这需要以 JSON 格式提供。

    类似...

    AWSTemplateFormatVersion: 2010-09-09
    Transform: 'AWS::Serverless-2016-10-31'
    Description: G2G Api Template Stack
    
    Resources:
       g2gPrivate:
        Type: 'AWS::ApiGateway::RestApi'
        Properties:
          Name: 'private-gw'
          EndpointConfiguration:
            Types:
              - PRIVATE
          Policy: !Sub |
            {
              "Version": "2012-10-17",
              "Statement": [
                {
                  "Effect": "Deny",
                  "Principal": "*",
                  "Action": "execute-api:Invoke",
                  "Resource": "arn:aws:execute-api:us-east-1:${AWS::AccountId}:*/*/*/*",
                  "Condition": {
                    "StringNotEquals": {
                      "aws:sourceVpc": "vpc-xxxxx"
                    }
                  }
                },
                {
                  "Effect": "Allow",
                  "Principal": "*",
                  "Action": "execute-api:Invoke",
                  "Resource": "arn:aws:execute-api:us-east-1:${AWS::AccountId}:*/*/*/*"
                }
              ]
            }
    
    

    【讨论】:

    • 如何在策略 JSON 中引用 AWS AccountId 和 APIGateway?有没有办法使用 Sub!获取 AWSAccountId 以及如何在 JSON 中引用相同的网关 ID?
    • 是的,您可以使用 !Sub。我已将示例更新为可以正常工作。
    • 这里有个小问题,在策略里面Resource属性需要ApiId,xxxx部分是API ID,所以在这种情况下我需要先创建网关,然后修改与您共享的策略示例相同的资源?
    • 您可以使用通配符“*”而不是在 Arn 中提供 ApiID,例如 arn:aws:execute-api:us-east-1:${AWS::AccountId}:*/*/*/* 这将是安全的,因为该策略与此 API 网关明确关联。
    • 不用担心。是的,一段时间以来,我一直在使用 CloudFormation 和 API Gateway 做很多事情。它大多非常好,但有时,相当令人沮丧!
    猜你喜欢
    • 2016-12-19
    • 2016-12-17
    • 1970-01-01
    • 2018-11-14
    • 2021-10-14
    • 2019-07-30
    • 2020-01-06
    • 2021-02-22
    • 2012-04-23
    相关资源
    最近更新 更多