Search code examples
terraform

Ec2 instance creation using terraform assigns public IP


I am super new to terraform, I tried creating a ec2 instance, a elb and cname record using terraform. There is main.tf file with config supporting this and a separate variable file.

I haven't defined to create a public ip for ec2 instance anywhere in the config but it still creates one. Kindly advice how to prevent this.


Solution

  • There is an Boolean option to disable to assign public ip address with an instance.

    https://www.terraform.io/docs/providers/aws/r/instance.html#associate_public_ip_address

    associate_public_ip_address - (Optional) Associate a public ip address with an instance in a VPC. Boolean value.

    So your code can be updated to

    resource "aws_instance" "foo" {
      ...
      ecs_associate_public_ip_address = "false"
    }