【问题标题】:AWS Cloudformation: Setting up more than 1 condition for IpAddressAWS Cloudformation:为 IpAddress 设置多个条件
【发布时间】:2021-05-19 17:16:39
【问题描述】:

我正在使用 AWS SAM,我用它来部署 lambda 并与 API Gateway 端点集成。

对于 API 网关,我有类似的东西:

Api:
   Type: AWS::Serverless::Api
   Properties:
     Cors:
       AllowHeaders: "'Authorization,Content-Type,X-Amz-Date,X-Amz-Security-Token,X-Api-Key,X-Requested-With'"
       AllowMethods: "'GET,HEAD,POST'"
       AllowOrigin: "'*'"
     DefinitionBody:
       swagger: 2.0
       info:
         version: 1.0
         title: !Sub MyAPIGateway-${EnvironmentName}
       paths:
         /{proxy+}: # https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
           x-amazon-apigateway-any-method:
             x-amazon-apigateway-integration:
               httpMethod: POST
               type: aws_proxy
               uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Function.Arn}/invocations
       x-amazon-apigateway-policy:
         Version: 2012-10-17
         Statement:
           - Effect: Allow
             Principal: "*"
             Action: execute-api:Invoke
             Resource: execute-api:/*/*/*
             Condition:
               IpAddress:
                 Fn::Transform:
                   Name: AWS::Include
                   Parameters:
                     Location: s3://foo/bar/latest/cidr.yaml

s3://foo/bar/latest/cidr.yaml 视为包含所有列入白名单的 IP 的文件,我无权更新或编辑它,因为它由安全管理。

假设在这些 CIDR 块之上,我想添加一些 NAT IP,在 Condition 下我可以有一些东西,以便在 IpAddress 元素下我有另一个条目并硬编码其中的 NAT IP 以便当 CloudFormation 运行时,它会将两者合并为一个资源策略,其中包含来自两者的 IP?

现在,我部署并手动转到 API Gateway Web 控制台下的资源策略,添加我的 NAT IP,保存并重新部署它。

我希望避免继续进行此手动更新。

附带说明,我可以拥有自己的 yaml 文件,其中包含所有内容,但我不想克隆 s3://foo/bar/latest/cidr.yaml 文件并将我的 NAT IP 添加到其中并在我的 CloudFormation 配置中使用克隆的文件作为如果主文件发生更改,我必须过于频繁地更新克隆副本,因为 security 可能正在添加/删除 CIDR 块。

【问题讨论】:

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


    【解决方案1】:

    由于Statement 是一个列表,我添加了另一个条目以包含 NAT IP,它起作用了。

    类似:

    Statement:
              - Effect: Allow
                Principal: "*"
                Action: execute-api:Invoke
                Resource: execute-api:/*/*/*
                Condition:
                  IpAddress:
                    Fn::Transform:
                      Name: AWS::Include
                      Parameters:
                        Location: s3://foo/bar/latest/cidr.yaml
                - Effect: Allow
                    Principal: "*"
                    Resource: execute-api:/*/*/*
                    Condition:
                      IpAddress:
                        aws:SourceIp:  
                          - "myNatIp1/32"
                          - "myNatIp2/32"
                          - "myNatIp3/32"                    
                    Action: execute-api:Invoke 
    

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      相关资源
      最近更新 更多