AWS Cloudformation:为 IpAddress

问题描述

我使用 AWS SAM 来部署 lambda 并与 API 网关端点集成。

对于 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 添加到其中并在我的 {{1 }} 配置,因为如果主文件中的某些更改CloudFormation 可能会添加/删除 CIDR 块,我必须过于频繁地更新克隆副本。

解决方法

由于 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 

谢谢!