-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I access IP from aws_ecs_service using assign_public_ip
#3444
Comments
|
I'm trying to create a route53 record, to point at the public IP. So yes, whenever the ECS task changes, it would update the Route53 record |
would be nice to have private_ip of host as well, i need that for setting an environment variable (local host ip) to a container. |
+1 |
I am running into a very similar issue when attempting to create an EDIT:I was missing the fact that with Fargate you don't have to specify As for this original issue:You can have a Fargate Here are a couple of solutions though:
Let me know if you need any help with the above solutions, and I would be happy to assist. |
nice! +1 |
This would be really useful when creating a running task in Fargate via a service. Was hoping that maybe there would be a data source associated with the ECS cluster and the associated tasks in it that would provide this information. But there is not. |
I have a related use case. I am trying to deploy a grafana instance in Fargate and then use the terraform grafana provider to configure it. I need a way to pass the IP address of the instance to the provider so that the provider will know what it's configuring. |
No news about this? |
Not sure it helps but I am looking to do this as well and my workaround plan with ecs-cli ps command to get the ip into terraform https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cmd-ecs-cli-ps.html |
Having the same issue with fargate. Whenever I update ECS or Task Definition (SSM parameters and such) I need to point my routing to this new public ip however as @loivis its on Amazon's side |
Just wanted to add that I would also like to get the public IP from the task. In my case it doesn't matter that the IP changes. Via terraform, I spin up the task, spin up a VM, and then there is an app on the VM that has to connect to the tasks public IP. Both the task and VM are brought up/down at the same time. I want to just pass the public IP directly to my terraform VM code. |
Is there any update on how i can access the private ip? |
Hi, I worked around the issue of obtaining the public and private IPs by:
It is a bit of a faff, you have to make sure you only have the single ECS service using the security group, otherwise you have multiple network interfaces and you cannot determine which one is for the ECS service via terraform. It takes a few seconds for the network interface to be created. Terraform reports the ECS service resource creation is complete even though the network interface will not yet be available. So you need to set a "sleep" step in terraform (I waited 30 secs) before querying the network interface. Also (and I cannot remember the exact issue as it was a while ago I tested this), if your ECS service has issues and keeps going up and down, it will keep getting a new public IP each time (and IIRC it deletes and redefines the network interface each time). If you decide to run a terraform apply/destroy at this point, then depending on timing, terraform can get confused as the interface it thinks is there, will not be. I have the code I used, you are welcome to it, but the code/workaround isn't really suitable for production use due to the issue of changing network interfaces and IPs when the ECS service restarts. But then that's an AWS "feature". Scott |
Thank you @scott-doyland-burrows ! With multiple containers in the same Fargate task, you can use localhost/127.0.0.1 to communicate between containers. |
@scott-doyland-burrows I am new to Terraform, can you put a sample code for pulling he network interface IPs? |
Hi, I have the same requirement. I need to extract the Private Ip of the Fargate task and need to pass the same in the container template as a parameter to the COMMAND so that the application can be run on that IP. I am trying the below command to run consul but it asks for IP. "command": [ Instead of 0.0.0.0 need to pass the private IP address, and couldn't find any solution yet. |
It was a few months ago so I cannot remember exactly how this works - as I have not had to touch it since then... Create a security group (not shown below - but I did this via a module). Create your ecs service (not shown below - but I did this via a module) and put in in the security group. Then wait 30 seconds (required as it takes a few secs for the NIC to be defined). The NIC will be tied to a security group. Then call the first data block below, this will get a list of all NICs in the security group (you must have just ONE NIC per security group for this to work). The second data block gets the attributes for the ID of the NIC from the first data block. The first output block displays the private IP of the NIC. The second output block displays the public IP of the NIC.
The problem you will have is that if the ecs service dies and restarts itself, it will get a different public IP (IIRC) so you would need to manage this by rerunning terraform, so I think you'd need a terraform apply to force terraform to pick up the new IP in terraform output. I sort of gave up with this in the end as it was all a bit messy, it was fine for just some dev work, but I wouldn't suggest you use it for more than that. |
I have this problem and am using @scott-doyland-burrows solution. My only addition is that instead of using a 30 second timeout, I am listing that the I believe this requires that the tasks reach a steady state, and thus be assigned eni's, before this data source is executed. |
Hi Scott,
Woderning what is the issue here! |
Hi, So it looks like it finds two interfaces - and my original code will only work with one. Scott |
Hi... I'm a newbie in terraform, but quickly I run into this issue and this is my recently workaround, that for now is "successfully enough" to me and perhaps could be useful to someone. I have two services (service_1 and service_2) and service_2 depends from service_1 to retrieve some resources, where service_2 is defined like
So, the script get_ip.sh has the following content (I didnt try if its possible return the value to variable service_1_ip in a direct manner):
To update service_2
In resume thats all... and I'm sure that this has a lot of issues and limitations (that I will be discovering time by time, but for now..."works") Regards |
I stumbled across this today and found it disappointing that more than 5 years this is still a thing :( The use case is a single fargate container running so for now #3444 (comment) has given me the IP addresses required. I hope to see this no longer a thing soon... |
I found a better solution using tags:
This is a robust approach as there can't be multiple network interfaces listed for your use-case. |
This is the best approach. To check if the tag was right propagated. |
My solution with ECS Service + Terraform: |
This is almost exactly my solution which is right above your comment. I'm happy that people use it but if you create a blog post about it on LinkedIn, you should tag me there. |
oh, really, our solutions are the same, but I scrolled through your comment and found it manually( |
This won't help if the fargate container crashes and will assign a new ip? |
@tatliHU, Doesn't work when there are multiple tasks and thus multiple interfaces. Is there other solution based on this one? |
Hi @celesteking,
|
Doesn't work:
|
@celesteking use aws_network_interfaces instead of aws_network_interface |
Max useful output it can return is the list of interface names, but I need per-interface data like private and public IPs. I'm new to Terraform. |
data "aws_network_interfaces" "interface_tags" {
filter {
name = "tag:aws:ecs:serviceName"
values = ["my-runner"]
}
}
data "aws_network_interface" "netiface" {
for_each = toset(data.aws_network_interfaces.interface_tags.ids)
id = each.key
}
output "iface_ip_map" {
description = "IP addresses of XX service"
value = { for k,v in data.aws_network_interface.netiface: k => {pub: v.association[0].public_ip, priv: v.private_ip }}
} But that doesn't show which service has what task with what IP[s]. |
I'm creating a ECS service with
launch_type = "FARGATE
" andassign_public_ip = true
.I would like to be able to access the public IP of the service after it's created,
so that I could create a Route53 record. It doesn't look like there is any way currently to accomplish this.
Could we add a
public_ip
attribute to theaws_ecs_service
resource?Thanks!
Terraform Version
Affected Resource(s)
aws_ecs_service
Terraform Configuration Files
Debug Output
N/A
Panic Output
N/A
Expected Behavior
N/A
Actual Behavior
N/A
Steps to Reproduce
N/A
Important Factoids
N/A
References
The text was updated successfully, but these errors were encountered: