ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Solved Terraform - Referencing a subnet from tfstate file

    IT Discussion
    terraform
    2
    5
    571
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • IRJI
      IRJ
      last edited by IRJ

      I am trying to create an EC2 instance by referencing a subnet already present in the environment. I would like to use the data from the .tfstate file in S3.

      I found this in the terraform manual, but since I have multiple subnets, I am unsure how to specify a specific one

      https://www.terraform.io/docs/providers/terraform/d/remote_state.html

      resource "aws_instance" "test instance" {
        ami             = "${data.aws_ami.ubuntu.id}"
        instance_type               = var.test.ec2_instance_type
        key_name                    = var.key_pair_name
        subnet_id                   = data.terraform_remote_state.vpc.outputs.subnet_id
      
        tags {
          Name = "test-instance"
        }
      }
      
      1 Reply Last reply Reply Quote 1
      • IRJI
        IRJ
        last edited by

        Ok. I figured it out.

        Using the output from above through trial and error this was my answer.

          subnet_id  = data.terraform_remote_state.network_configuration.outputs.mysubnet_name_id
        

        In this example your subnet is named mysubnet_name

        1 Reply Last reply Reply Quote 0
        • stacksofplatesS
          stacksofplates
          last edited by

          Is any of this in a module?

          1 Reply Last reply Reply Quote 0
          • stacksofplatesS
            stacksofplates
            last edited by

            What does terraform state list show?

            IRJI 1 Reply Last reply Reply Quote 1
            • IRJI
              IRJ @stacksofplates
              last edited by

              @stacksofplates said in Terraform - Referencing a subnet from tfstate file:

              What does terraform state list show?

              data.aws_ami.launch_configuration_ami
              data.terraform_remote_state.network_configuration
              aws_launch_configuration.ec2_private_launch_configuration
              aws_launch_configuration.ec2_public_launch_configuration
              aws_security_group.ec2_private_security_group
              aws_security_group.ec2_public_security_group
              aws_security_group.elb_security_group
              
              1 Reply Last reply Reply Quote 0
              • IRJI
                IRJ
                last edited by

                Ok. I figured it out.

                Using the output from above through trial and error this was my answer.

                  subnet_id  = data.terraform_remote_state.network_configuration.outputs.mysubnet_name_id
                

                In this example your subnet is named mysubnet_name

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post