【问题标题】:Security group sg-0da667222da8a6eb2 does not appear to belong to the same VPC as the input subnets安全组 sg-0da667222da8a6eb2 似乎不属于与输入子网相同的 VPC
【发布时间】:2020-10-23 00:49:57
【问题描述】:

作为 CI/CD Jenkins 管道的一部分,我正在使用 dockerhub 上提供的映像中的 cloudformation 将 springboot 应用程序部署到 AWS EC2/Fargate。我将访问密钥、秘密、区域和子网定义为在运行时传递的秘密。 cloudformation 部署失败,状态为 CREATE_FAILED 并出现以下错误:

Invalid request provided: CreateService error: Security group
sg-0da667222da8a6eb2 does not appear to belong to the same VPC as the
input subnets. (Service: Ecs, Status Code: 400, Request ID:
503ce486-c3db-4d35-bb92-5f4770662c05, Extended Request ID: null)

这是我的 cloudformation yaml 文件内容:

AWSTemplateFormatVersion: "2010-09-09"
Parameters:
  SubnetID:
    Type: String
  ServiceName:
    Type: String
  ServiceVersion:
    Type: String
  DockerHubUsername:
    Type: String
Resources:
  Cluster:
    Type: AWS::ECS::Cluster
    Properties:
      ClusterName: deployment-example-cluster
  ServiceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: ServiceSecurityGroup
      GroupDescription: Security group for service
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 8080
          ToPort: 8080
          CidrIp: 0.0.0.0/0
  TaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: !Sub ${ServiceName}-task
      Cpu: 256
      Memory: 512
      NetworkMode: awsvpc
      ContainerDefinitions:
        - Name: !Sub ${ServiceName}-container
          Image: !Sub ${DockerHubUsername}/${ServiceName}:${ServiceVersion}
          PortMappings:
            - ContainerPort: 8080
      RequiresCompatibilities:
        - EC2
        - FARGATE
  Service:
    Type: AWS::ECS::Service
    Properties:
      ServiceName: !Sub ${ServiceName}-service
      Cluster: !Ref Cluster
      TaskDefinition: !Ref TaskDefinition
      DesiredCount: 1
      LaunchType: FARGATE
      NetworkConfiguration:
        AwsvpcConfiguration:
          AssignPublicIp: ENABLED
          Subnets:
            - !Ref SubnetID
          SecurityGroups:
            - !GetAtt ServiceSecurityGroup.GroupId

这是cloudformation堆栈构建过程的屏幕截图:

令人惊讶的是,sg-0da667222da8a6eb2 不是我的安全组之一。任何帮助将不胜感激。

【问题讨论】:

  • 那里有serverless.yml 文件吗?
  • @ThanhNguyenVan 没有 serverless.yml。

标签: amazon-web-services spring-boot amazon-ec2 amazon-cloudformation


【解决方案1】:

您的ServiceSecurityGroup,按照它的定义,是在默认 VPC 中创建的。但是,您的 SubnetID 可能属于 自定义 VPC。因此,您必须为您的ServiceSecurityGroup 提供VpcId

Parameters:

  VpcId:
    Type: AWS::EC2::VPC::Id

  # others not shown

Resources:

  # only relevant part shown

  ServiceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: ServiceSecurityGroup
      GroupDescription: Security group for service
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 8080
          ToPort: 8080
          CidrIp: 0.0.0.0/0
      VpcId: !Ref VpcId   

  Service:
    Type: AWS::ECS::Service
    Properties:
      ServiceName: !Sub ${ServiceName}-service
      Cluster: !Ref Cluster
      TaskDefinition: !Ref TaskDefinition
      DesiredCount: 1
      LaunchType: FARGATE
      NetworkConfiguration:
        AwsvpcConfiguration:
          AssignPublicIp: ENABLED
          Subnets:
            - !Ref SubnetID
          SecurityGroups:
            - !GetAtt ServiceSecurityGroup.GroupId  

【讨论】:

  • 我确实按照您的建议将 vpcId 添加到了 ServiceSecurityGroup,但我仍然看到同样的问题。我已使用 cloudformation 堆栈中的图像更新了详细信息。不确定这是否会提供任何进一步的线索。
  • @rslj 转到 VPC 控制台,检查您使用的 SubnetID 来自哪个 VPC。您的安全组必须来自同一个 VPC。这些组是特定于 VPC 的。
  • 信不信由你,我正在研究您的建议。一旦我完成了这个,我会及时通知你:-)
【解决方案2】:

我在设置中使用的是自定义 VPC,而不是默认 VPC。当 cloudformation 启动时,它会在没有提供信息的情况下在默认 VPC 中构建资源。在我的情况下,当我将 Jenkins 中的自定义 VPC 作为环境变量传递时,堆栈按预期出现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-17
    • 2021-06-28
    • 2020-10-28
    • 2018-06-23
    • 2018-09-09
    • 2020-02-07
    • 2020-12-29
    • 2015-10-12
    相关资源
    最近更新 更多