CloudFormationでSecurityGroupを作るときにYou may not define rules between a VPC group and a non-VPC groupとエラーが出るときの対応
解決まで地味に30分程度掛かったのでメモ。
CloudFormationで次のような感じでSecurityGroupを作ろうとすると、You may not define rules between a VPC group and a non-VPC groupとエラーが出てSecurityGroupが作れない。
  DBEC2SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: "Allow access from specific SecurityGroup"
      SecurityGroupIngress:
      - IpProtocol: tcp
        FromPort: '3306'
        ToPort: '3306'
        SourceSecurityGroupId: !Ref 'EC2SecurityGroup'
これはSourceSecurityGroupにVPCのSecurityGroupが指定されているのに、(VpcIdが指定されていないため)EC2 Classic環境にSecurityGroupが作られようとしているため出たエラーである(言葉にするとややこしい)。
ドキュメントにもあるが正しくはこんな感じ。
  DBEC2SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      VpcId: !Ref VPC
      GroupDescription: "Allow access from specific SecurityGroup"
      SecurityGroupIngress:
      - IpProtocol: tcp
        FromPort: '3306'
        ToPort: '3306'
        SourceSecurityGroupId: !Ref 'EC2SecurityGroup'
ドキュメントは見ていたけど眠かったのとエラーメッセージに惑わされて解決まで時間掛かってしまった…。
