Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure hostname in ECS Fargate task definition

We are migrating from ECS to Fargate. In ECS, we could set the hostname in the task definition like this:
"hostname": "%HOST_NAME%"

It fails to create with the error 'hostname is not supported on container when networkMode=awsvpc' Is there any way to set hostname ?

like image 972
Sunil Shakya Avatar asked Feb 15 '18 14:02

Sunil Shakya


People also ask

What are the mandatory parameters in the task definition in ECS?

The family and container definitions are required in a task definition. In contrast, task role, network mode, volumes, task placement constraints, and launch type are optional. You can use these parameters in a JSON file to configure your task definition.

What is family in ECS task definition?

The first property you define in the task definition is the Family property, which establishes the ECS task definition family name, and affects the way CloudFormation will create new instances of your task definition resources whenever you make changes to your task definition.

Can you SSH into fargate task?

Furthermore, ECS users deploying tasks on Fargate did not even have this option because with Fargate there are no EC2 instances you can ssh into.

How can I run an Amazon ECS task on fargate in a private subnet?

To run Fargate tasks in a private subnet without internet access, use VPC endpoints. VPC endpoints allow you to run Fargate tasks without granting the tasks access to the internet. The required endpoints are accessed over a private IP address.


1 Answers

Although documentation is clear that this is not supported, there is a workaround. You can create a bootstrap_ecs.sh file and override the container ENTRYPOINT to reference this at runtime (or else add the below to your own bootstrap script). You can use this when running from ECS. Otherwise, use your standard ENTRYPOINT and COMMAND.

bootstrap_ecs.sh

#!/bin/bash

ifconfig # prints full IP info
echo "Detecting 'eth1' interface..."
DETECTED_IP=$(ifconfig -a | grep -A2 eth1 | grep inet | awk '{print $2}' | sed 's#/.*##g' | grep "\.")
if [[ -z $DETECTED_IP ]]; then
    echo "Detecting 'eth0' interface ('eth1' not found)..."
    DETECTED_IP=$(ifconfig -a | grep -A2 eth0 | grep inet | awk '{print $2}' | sed 's#/.*##g' | grep "\." | head -1)
fi
DETECTED_HOSTNAME=$(hostname)
echo -e "\n\nDETECTED_IP=$DETECTED_IP\nDETECTED_HOSTNAME=$DETECTED_HOSTNAME\n\n"
# Note: newer OS versions us `ip` instead of `ifconfig`

# Echo for debugging. You can comment/delete the 1st and 3rd lines once everything is working.
echo -e "Current file contents:\n $(cat /etc/hosts)"
echo "$DETECTED_IP $DETECTED_HOSTNAME" >> /etc/hosts
echo -e "\n\n\nUpdated file contents:\n $(cat /etc/hosts)"

CMD="$@"
$CMD
like image 161
aaronsteers Avatar answered Sep 21 '22 09:09

aaronsteers