错误:指定networkMode'awsvpc'时必须提供网络配置

时间:2019-08-02 06:52:27

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

我有此任务定义代码,但有问题:

{
  "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时必须提供网络配置   已指定“ awsvpc”。


在我的生命中,我无法弄清楚如何解决它。.我尝试为networkConfiguration放置最合理的值,我发现...没有骰子。 有人知道我该如何解决吗?

3 个答案:

答案 0 :(得分:2)

您将需要以下类似内容(我工作时的快照)

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

有关更多详细信息,请参见thisthis

答案 1 :(得分: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 :(得分:0)

这是带有网络配置的运行任务命令

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]}"