首页 > 解决方案 > AWS Cloudformation:为 IpAddress 设置多个条件

问题描述

我正在使用 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-servicesamazon-cloudformationaws-api-gatewayamazon-iam

解决方案


作为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 

谢谢!


推荐阅读