unrecognizable hacker with smartphone typing on laptop at desk
AWS Terraform

Terraform Output Examples for AWS

In Terraform, an output is a way to display the values of resources or data sources that are created by your infrastructure code. There are several types of outputs that you can define in Terraform, and each serves a specific purpose. Here are the possible output types in Terraform and their explanations:

  1. string: This output type represents a string value. You can use it to display any string value, such as a URL or a message and a resource name.
  2. number: This output type represents a numerical value. You can use it to display any numerical value, such as a port number or a count of resources or a value in a resource, for example, if you wanted to output the size of a disk.
  3. bool: This output type represents a boolean value. You can use it to display true or false values, such as whether a resource was created or not.
  4. list: This output type represents a list of values. You can use it to display a list of resources, such as the IP addresses of a group of instances or a list of instance names.
  5. map: This output type represents a key-value pair. You can use it to display a map of values, such as the tags assigned to a resource.
  6. set: This output type represents a set of unique values. You can use it to display a set of resources, such as a list of security groups that a resource belongs to.
  7. object: This output type represents a complex object with multiple attributes. You can use it to display a structured set of data, such as the attributes of an AWS S3 bucket.

When defining an output in Terraform, you need to specify the name of the output and its type. For example, the following code defines an output called “my_string_output” with a string value:

output "my_string_output" {
  value = "Hello, world!"
}

In this example, Terraform will display the string “Hello, world!” when you run the terraform output command. You can also use interpolation to reference the values of other resources or data sources in your outputs, making them more dynamic and useful.

  1. string:
output "my_string_output" {
  value = "Hello, world!"
}

This will output the string “Hello, world!” when you run terraform output.

  1. number:
output "my_number_output" {  value = 1234
}

This will output the number 1234 when you run terraform output.

  1. bool:
output "my_bool_output" {
  value = true
}

This will output the boolean value true when you run terraform output.

  1. list:
    output "my_list_output" {
      value = ["10.0.0.1", "10.0.0.2", "10.0.0.3"]
    }
    

    This will output a list of IP addresses when you run terraform output.

    1. map:
    output "my_map_output" {
      value = {
        name  = "My EC2 instance"
        type  = "t2.micro"
        count = 2
      }
    }

    This will output a map of attributes when you run terraform output.

    1. set:
    output "my_set_output" {
      value = ["sg-12345678", "sg-87654321"]}

    This will output a set of security group IDs when you run terraform output.

    1. object:
    output "my_object_output" {
      value = {
        id   = aws_instance.my_instance.id
        name = aws_instance.my_instance.tags["Name"]  }
    }

    Example of getting the Name, Instance ID and the IP address of an instance in AWS

    To get the name, instance ID, and IP address of an instance, you can use the following code:

    output "instance_name" {
      value = aws_instance.example.tags.Name
    }
    
    output "instance_id" {
      value = aws_instance.example.id
    }
    
    output "instance_ip" {
      value = aws_instance.example.private_ip
    }
    

    This assumes that you have an AWS instance resource defined in your Terraform code with the name example. You can replace example with the name of your actual instance resource.

    The output block defines the output values that you want to display when you run terraform apply or terraform output.

    The aws_instance.example.tags.Name expression gets the value of the Name tag on the instance, which is assumed to contain the instance name.

    The aws_instance.example.id expression gets the instance ID.

    The aws_instance.example.private_ip expression gets the private IP address of the instance.

    You can adjust the output names to your preference.

    Hope this helps


    Discover more from Real World IT.Net

    Subscribe to get the latest posts to your email.