【问题标题】:Error : Network Configuration must be provided when networkMode 'awsvpc' is specified错误:指定 networkMode 'awsvpc' 时必须提供网络配置
【发布时间】:2019-12-10 18:37:59
【问题描述】:

我有这个任务定义代码,有问题:

{
  "family": "ikg-api",
  "taskRoleArn": "",
  "executionRoleArn": "arn:aws:iam::913xxxx371:role/ecsTaskExecutionRole",
  "networkMode": "awsvpc",
  "containerDefinitions": [
    {
      "name": "ikg-api",
      "image": "913xxxx371.dkr.ecr.us-west-2.amazonaws.com/ikg_api:fda0b49f8",
      "cpu": 512,
      "memory": 1024,
      "memoryReservation": 1024,
      "portMappings": [
        {
          "containerPort": 80,
          "hostPort": 80,
          "protocol": "tcp"
        }
      ],
      "essential": true,
      "environment": [
        {
          "name": "is_docker",
          "value": "yes"
        }
      ],
      "secrets": [
        {
          "name": "bitbucket_password",
          "valueFrom": "arn:aws:ssm:us-west-1:913xxxx0371:parameter/bitbucket_pwd"
        }
      ],
      "startTimeout": 10,
      "stopTimeout": 19,
      "user": "root",
      "workingDirectory": "/apps",
      "disableNetworking": false,
      "privileged": false,
      "readonlyRootFilesystem": false,
      "interactive": false,
      "pseudoTerminal": false,
      "healthCheck": {
        "command": [
          "curl",
          "localhost"
        ],
        "interval": 30,
        "timeout": 20,
        "retries": 1,
        "startPeriod": 50
      }
    }
  ],
  "networkConfiguration": {
    "awsvpcConfiguration": {
      "assignPublicIp": "ENABLED",
      "securityGroups": [
        "sg-0a6e7d4a5238fe3c6"
      ],
      "subnets": [
        "subnet-05a6557c"
      ]
    }
  },
  "requiresCompatibilities": [
    "FARGATE"
  ],
  "cpu": "512",
  "memory": "1024",
  "tags": [
    {
      "key": "Project",
      "value": "IKG"
    }
  ]
}

当我使用以下方式上传定义时:


aws ecs run-task --cluster tutorial --task-definition ikg-api:1  --count 1

我收到此错误:

调用 RunTask 时发生错误(InvalidParameterException) 操作:networkMode时必须提供Network Configuration 'awsvpc' 已指定。


对于我的一生,我无法弄清楚如何解决它..我尝试为 networkConfiguration 设置我能找到的最理智的值......没有骰子。 有人知道我如何解决这个问题吗?

【问题讨论】:

  • 您确定要启动包含networkConfiguration 的任务。我猜你添加了一个新的任务定义来解决这个问题,它将获得一个新的“修订号”......例如 ikg-api:2。

标签: amazon-web-services amazon-ecs aws-fargate


【解决方案1】:

我认为@user2014363 的回答是正确的,我能够使用它来运行我的任务。如果其他人尝试在 CI 中执行此操作,您可能不会提前知道您的子网/安全组 ID。我能够使用 AWS CLI 来获取这些(您需要有某种方法来识别您需要的子网/安全组,例如按标签过滤):

mytask: 
  image: python:3.8
  stage: deploy
  only:
    - master
  when: manual
  before_script:
    - pip install awscli
    - apt-get -qq update && apt-get -y install jq
    - |
      subnets=$( \
        aws ec2 describe-subnets \
          --filters \
            Name=tag:aws:cloudformation:stack-name,Values=${ENVIRONMENT}-${APP_NAME}-stack \
            Name=tag:aws-cdk:subnet-type,Values=Public \
        | jq -r '.Subnets | map(.SubnetId) | join(",")')
  script:
    - |
      aws ecs run-task \
        --cluster ${ENVIRONMENT}-${APP_NAME}-cluster \
        --task-definition ${ENVIRONMENT}-${APP_NAME}-collectstatic \
        --network-configuration "awsvpcConfiguration={subnets=[${subnets}],assignPublicIp=ENABLED}" \
        --count 1 \
        --launch-type FARGATE

【讨论】:

    【解决方案2】:

    这是包含网络配置的运行任务命令

    aws ecs run-task --cluster your-cluster --task-definition your-task:1 --count 1 --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets=[subnet-0123456789],securityGroups=[sg-0123456789]}"
    

    【讨论】:

      【解决方案3】:

      你将需要类似下面的东西(我在工作中的快照)

      NetworkConfiguration:
          AwsvpcConfiguration:
            AssignPublicIp: DISABLED
            SecurityGroups:
            - !Ref ECSServicesSecurityGroup
            Subnets:
            - Fn::ImportValue: !Sub ${VPCStack}-SubnetPrivateA
            - Fn::ImportValue: !Sub ${VPCStack}-SubnetPrivateB
            - Fn::ImportValue: !Sub ${VPCStack}-SubnetPrivateC
      

      更多详情请参考thisthis

      【讨论】:

      • 我必须把这个放在哪里?
      • @MobinAlHassan 我必须将它放在 CloudFormation 模板中的 AWS::ECS::Service 资源中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 2018-04-01
      • 2019-01-11
      • 2017-09-13
      • 1970-01-01
      相关资源
      最近更新 更多