Containers-roadmap: [Fargate] [How to]: Is it possible to add host entry to /etc/hosts file via Fargate 1.3.0 or 1.4.0?

Created on 15 Sep 2020  路  7Comments  路  Source: aws/containers-roadmap

I need to add host entry to /etc/hosts file via Fargate. And, extraHosts via task-definition is not supported with awsvpc. Currently, we are using Fargate 1.3.0 and ready to upgrade to 1.4.0 if the issue can be resolved with 1.4.0.

Also, question asked here - https://github.com/aws/containers-roadmap/issues/886 suggests it is possible to do via 1.3.0 and 1.4 as well. And, I cannot seem to find either ways.

Can somebody suggest how it can be done?

ECS Fargate Proposed

Most helpful comment

@poonamgp14 output your complete /etc/hosts file during runtime after your modifications so you can 馃挴 verify it is correct/as expected.

In my case, at least for 1.4.0, I had to add an extra line-break before adding my stuff.

All 7 comments

Hi @poonamgp14. Can you talk more about your use case and the need to add to /etc/hosts?

I'll go first: I had a situation where my app needed to connect to another service via hostname only (e.g. not via a FQDN). Since ECS service discovery (which is what powers Fargate name resolution) only works with FQDN and you can't configure /etc/resolv.conf at the task level to add a search domain I came up with a workaround to inject the domain as a variable (SEARCH_DOMAIN) and then as part of the image startup script I did:

if [ $SEARCH_DOMAIN ]; then echo "search ${SEARCH_DOMAIN}" >> /etc/resolv.conf; fi 

Could you use a similar workaround to manipulate /etc/hosts?

We are looking at how to solve this more "natively" so if you can talk more about what you are trying to achieve that would help.

Thanks.

This definitely works in Fargate 1.4.0:

if ! getent hosts aws-es-proxy; then
  echo "" | tee -a /etc/hosts # add empty line as /etc/hosts on fargate does not end with one!
  echo "127.0.0.1 aws-es-proxy" | tee -a /etc/hosts
fi

(as part of your entrypoint.sh or so)

This definitely works in Fargate 1.4.0:

if ! getent hosts aws-es-proxy; then
  echo "" | tee -a /etc/hosts # add empty line as /etc/hosts on fargate does not end with one!
  echo "127.0.0.1 aws-es-proxy" | tee -a /etc/hosts
fi

(as part of your entrypoint.sh or so)

This one works just fine, I just confirmed.

Also /etc/hosts is mounted as RW, so there shouldn't be any issues when writing to the file.

Which also confirms why the above works

I have similar startup script running in fargate as mentioned below:

echo "XXX.XX.XX.XXX  $HOSTNAME_SW_NODES" >> /etc/hosts
if [ -n "$(grep $HOSTNAME_SW_NODES /etc/hosts)" ]
    then
        echo "$HOSTNAME_SW_NODES was added succesfully \n $(grep $HOSTNAME_SW_NODES /etc/hosts)";
    else
        echo "Failed to Add $HOSTNAME_SW_NODES, Try again!";

However, I still get the error

<urllib3.connection.HTTPSConnection object at 0x7fca1d205190>: Failed to establish a new connection: [Errno -2] Name or service not known

It appears to me that there could be network connectivity issue since adding host entry through startup script seems to be a valid option.

@poonamgp14 output your complete /etc/hosts file during runtime after your modifications so you can 馃挴 verify it is correct/as expected.

In my case, at least for 1.4.0, I had to add an extra line-break before adding my stuff.

Hi @poonamgp14, do you have any other questions/requests from AWS Fargate at the moment?

Do the workarounds and suggestions posted here work for your use-case?

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mineiro picture mineiro  路  3Comments

AndrewMcFarren picture AndrewMcFarren  路  3Comments

abby-fuller picture abby-fuller  路  3Comments

miztch picture miztch  路  3Comments

ORESoftware picture ORESoftware  路  3Comments