fortissimo1997's diary

備忘録的な使い方をする予定

API GatewayのリソースポリシーでのRefの使い方

fortissimo1997.hatenadiary.jp

前回のメモを書いた後、そういえばCustomStatementsで自由にPolicyが書けるようになっていたのを思い出した。

作り直したテンプレート(抜粋)

Resource:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: hoge
      EndpointConfiguration: PRIVATE
      Auth:
        ResourcePolicy:
          CustomStatements:
            - Effect: Deny
              Principal: "*"
              Action: execute-api:Invoke
              Condition:
                StringNotEquals:
                  aws:SourceVpce:
                    Ref: MyEndpoint
  MyEndpoint:
    Type: AWS::EC2::VPCEndpoint
    Properties:
      VpcId: vpc-1234
      VpcEndpointType: Interface
      SubnetIds:
        - subnet-1234
      ServiceName:
        Fn::Sub: com.amazonaws.${AWS::Region}.execute-api
      SecurityGroup:
        - sg-1234

これで無事通りますね👍