Error : Network Configuration must be provided when networkMode 'awsvpc' is specified

Here's the run-task command with network-configuration included

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

You will need something like the below (snapshot from what I have at work)

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

Please refer to this and this for more details


I think @user2014363's answer is correct, and I was able to use it to run my task. If anyone else is trying to do this in CI, you might not know your subnet/security group IDs ahead of time. I was able to use AWS CLI to grab these (you need to have some way of identifying the subnets/security groups you need, such as filtering by tags):

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