25

I have this task definition code, which has a problem:

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

when I upload the definition using:


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

I get this error:

An error occurred (InvalidParameterException) when calling the RunTask operation: Network Configuration must be provided when networkMode 'awsvpc' is specified.


for the life of me I cannot figure out how to resolve it..I tried putting the most sane values for networkConfiguration that I can find...no dice. Anybody know how I resolve this?

  • 1
    Are you sure your are launching the task that contains the networkConfiguration. I'm guessing you added a new task defintion to fix this which will get a new 'revision number'... like ikg-api:2, for example. – Brett Green Aug 09 '19 at 15:12

3 Answers3

26

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]}"
Liam
  • 27,717
  • 28
  • 128
  • 190
container
  • 261
  • 3
  • 3
  • you can get the subnet id from the aws vpc -> subnet section and securityGroups is not a required field – U R Mar 16 '23 at 18:42
  • How can I suppress the confirmation output to console? Say I want to launch N tasks from MS Powershell on Windows, I would like to only submit the jobs, not print task confirmation to stdout? – KingOtto May 05 '23 at 05:29
16

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

sayboras
  • 4,897
  • 2
  • 22
  • 40
8

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
briancaffey
  • 2,339
  • 6
  • 34
  • 62