Multiple ports and mount points in AWS ECS Fargate Task Definition using Ansible

2.3k Views Asked by At

I went through the documentation provided here

https://docs.ansible.com/ansible/latest/collections/community/aws/ecs_taskdefinition_module.html

It gives me nice examples of setting of Fargate task definition. However it showcases example with only one port mapping and there is no mount point shown here. I want to dynamically add port mappings ( depending on my app) and volume/mount points

For that I am defining my host_var for app as below ( there can be many such apps with different mount points and ports)

---
task_count: 4
task_cpu: 1028
task_memory: 2056
app_port: 8080

My Task definition yaml file looks like below

    - name: Create/Update Task Definition
      ecs_taskdefinition:
        aws_access_key: "{{....}}"
        aws_secret_key: "{{....}}"
        security_token: "{{....}}"
        region: "{{....}}"
        launch_type: FARGATE
        network_mode: awsvpc
        execution_role_arn: "{{ ... }}"
        task_role_arn: "{{ ...}}"
        containers:
          - name: "{{...}}"
            environment: "{{...}}"
            essential: true
            image: "{{ ....}}"
            logConfiguration: "{{....}}"
            portMappings:
              - containerPort: "{{app_port}}"
                hostPort: "{{app_port}}"

        cpu: "{{task_cpu}}"
        memory: "{{task_memory}}"
        state: present

I am able to create/update the task definition.

New requirements are that

  1. Instead of one port, now we can have multiple(or none) port mappings.
  2. We will have multiple (or none) mount points and volumes as well

Here is what I think the modified ansible host_var should look like below for ports [container_port1:host_port1, container_port2:host_port2, container_port3:host_port3]

task_count: 4
task_cpu: 1028
task_memory: 2056
#[container_port1:host_port1, container_port2:host_port2, container_port3:host_port3]
app_ports: [8080:80, 8081:8081, 5703:5703]
 

I am not sure what to do in ansible playbook to run through this list of ports.

Another part of the problem is that, although I was able to achieve creating volume and mouting in container thorough aws console, I was not able to do same using ansible.

here is the snippet of json for the AWS fargate looks like ( for volume part). There can be many such mounts depending on the application. I want to achieve that dynamically by defining mount points and volumes in host_vars

-
-
-

      "mountPoints": [
        {
          "readOnly": null,
          "containerPath": "/mnt/downloads",
          "sourceVolume": "downloads"
        }
-
-
-
-
-
-
"volumes": [
    {
      "efsVolumeConfiguration": {
        "transitEncryptionPort": ENABLED,
        "fileSystemId": "fs-ecdg222d",
        "authorizationConfig": {
          "iam": "ENABLED",
          "accessPointId": null
        },
        "transitEncryption": "ENABLED",
        "rootDirectory": "/vol/downloads"
      },
      "name": "downloads",
      "host": null,
      "dockerVolumeConfiguration": null
    }

I am not sure how to do that. Official documentation offers very little help.

0

There are 0 best solutions below