How to receive public ipv4 from AWS ECS via Terraform

How to receive public ipv4 from AWS ECS via Terraform

Hello everyone! I want to share with you my Terraform solution which allows to receive public IP address from Elastic Container Service Tasks.

You have simple service like that:

resource "aws_ecs_service" "test" {
  name            = "test"
  cluster         = data.aws_ecs_cluster.test.id
  task_definition = aws_ecs_task_definition.test.id
  launch_type = "FARGATE"
  desired_count   = 1
  network_configuration {
    subnets = ["test"]
    security_groups  = ["test"]
    assign_public_ip = true
  }
}        

Let`s add "enable_ecs_managed_tags = true" , it allows you to see services what connected with Elastic Network Interface (ENI IP), we enabled it by "assign_public_ip = true" line

resource "aws_ecs_service" "test" {
  name            = "test"
  cluster         = data.aws_ecs_cluster.test.id
  task_definition = aws_ecs_task_definition.test.id
  launch_type = "FARGATE"
  desired_count   = 1
  enable_ecs_managed_tags = true
  network_configuration {
    subnets = ["test"]
    security_groups  = ["test"]
    assign_public_ip = true
  }
}        

Now we can find the ENI with "aws:ecs:serviceName" tag using this data source:

data "aws_network_interfaces" "test" {
  tags = {
    "aws:ecs:serviceName" = aws_ecs_service.test.name
  }
}        

We receive the ID with this data source , next step to find our public Ipv4 from this interface:

data "aws_network_interface" "eni-ip" {
  depends_on = [ aws_ecs_service.test ]
  id = data.aws_network_interfaces.test.ids[0]
}        

It`s done. You can receive your IP and get via output:

output "eni_ip" {
  value = data.aws_network_interface.eni-ip.association[0].public_ip
}        

Telegram

Telegram channel


Gokan EKINCI

Expertise Back (Java Spring/NodeJS) Architecture distribuée (Cloud AWS/Google Cloud Platform | Terraform) Fullstack (compétences Angular & React)

11 个月

What do you do if your IP changes, do you re-apply your Terraform?

回复
Roman Antoniuk

IT Infrastructure as a service || DevOps || as a lifestyle

11 个月

Maybe it is now better to share how to not receive public IP from AWS, since now they are all paid :)

要查看或添加评论,请登录

Yehor Salo的更多文章

  • What is WAF?

    What is WAF?

    WAF (Web Application Firewall) - is a set of filtering conditions designed to detect and block attacks on a web…

  • Open source SAST and SCA analysis

    Open source SAST and SCA analysis

    Hello everyone! I will tell you about a free way for detecting possible or real vulnerabilities. What is SAST(Static…

  • Web 3.0

    Web 3.0

    Hello everyone! In this article, I want to discuss the new flow called Web3. But before we dive into that, let's…

    2 条评论
  • Difference between Terraform and Ansible

    Difference between Terraform and Ansible

    Hello everyone , it this article i will discource about theme "What diffrents between Terraform and Ansible tools for…

    10 条评论
  • How to hack SHA3-256

    How to hack SHA3-256

    Hello. In this article i will tell you few methods how you can hack this cryptographic hash function.

  • Создание отчета о тестировании на проникновение

    Создание отчета о тестировании на проникновение

    Многим доступным в настоящее время ресурсам для тестирования на проникновение не хватает написания отчетов. Методология…

  • Development of security policy

    Development of security policy

    Organizationally, the security policy determines the procedure for submitting and using user access rights, as well as…

  • How fast can hackers check compromised passwords?

    How fast can hackers check compromised passwords?

    Cybersecurity researchers from Agari decided to check how long it will take from the moment the password is leaked to…

  • Introducing Red Hat Advanced Cluster Security for Kubernetes

    Introducing Red Hat Advanced Cluster Security for Kubernetes

    Containers and microservices have triggered a tectonic shift in application infrastructure, and Kubernetes technology…

  • 5 developer tools for detecting and fixing security vulnerabilities

    5 developer tools for detecting and fixing security vulnerabilities

    We all know the importance of developing with customer, reliability, or security mindsets. In reality, acquiring a…

社区洞察

其他会员也浏览了