Terraform - Output

Defining Outputs | Learn how to extract and display vital information about your infrastructure using Terraform outputs.

·

6 min read

When we execute terraform apply, it returns a lot of output and that information is stored in the Terraform state file. We can access information from that file directly.

Terraform output is a feature in Terraform that allows you to extract and display information about your infrastructure after it has been created or updated. Outputs are defined in your Terraform configuration files and can be used to provide information to other Terraform configurations, scripts, or users.

Here's how Terraform output works:

  1. Define Outputs: You define outputs in your Terraform configuration using the output block. Each output block specifies a name and a value. For example:

    Elements = Resource-Type.Resource-Name.Attribute-Name

     output "instance_ip" {
       value = aws_instance.my_instance.public_ip
     }
    
  2. Run Terraform Commands: When you run terraform apply, Terraform will create or update your infrastructure and evaluate the output blocks. The values of the outputs are stored in the Terraform state file.

  3. View Outputs: You can view the outputs by running the terraform output command. This command will display the values of all the outputs defined in your configuration. For example:

     $ terraform output
     instance_ip = "54.210.123.45"
    
  4. Sensitive Outputs: If an output contains sensitive information, mark it as sensitive to prevent it from being displayed in the CLI output. For example:

     output "db_password" {
       value     = aws_db_instance.my_db.password
       sensitive = true
     }
    

By using Terraform outputs, you can easily extract and share important information about your infrastructure, making it easier to integrate with other tools and processes.

ssh-keygen is a command-line utility used to generate, manage, and convert authentication keys for SSH (Secure Shell). It creates a pair of public and private keys that can be used for secure communication between a client and a server.

To generate a new SSH key pair, you can use the following command

ssh-keygen

var.tf create this terraform file or a similar one.

  1. REGION: This variable sets the default AWS region to "us-east-2".

  2. ZONE1: This variable sets the default availability zone to "us-east-2a".

  3. AMIS: This variable is a map associating AWS regions with specific Amazon Machine Images (AMIs). For example, "us-east-2" is associated with the AMI "ami-03657b56516ab7912".

  4. USER: This variable sets the default user to "ec2-user".

variable "REGION" {
  default = "us-east-2"  # Default AWS region
}

variable "ZONE1" {
  default = "us-east-2a"  # Default availability zone
}

variable "AMIS" {
  type = map  # Type of the variable is a map
  default = {
    us-east-2 = "ami-03657b56516ab7912"  # AMI for us-east-2 region
    us-east-1 = "ami-0947d2ba12ee1ff75"  # AMI for us-east-1 region
  }
}

variable "USER" {
  default = "ec2-user"  # Default user
}

This setup allows you to reference these variables in your Terraform configuration, making it easier to manage and change values centrally.

provider.tf defines an AWS provider in Terraform and sets the region using a variable.

provider "aws" {
  region = var.REGION
}
  • provider "aws": This block configures the AWS provider, which is necessary for Terraform to interact with AWS services.

  • region = var.REGION: This line sets the AWS region for the provider using the value of the REGION variable defined earlier. This allows for flexible and centralized management of the region setting.

This setup ensures that the AWS provider will use the region specified in the REGION variable, making it easier to change the region without modifying multiple configuration files.

instance.tf Defines an AWS key pair and an EC2 instance and it uses provisioners to set up the instance.

variable "REGION" {
  default = "us-east-2"
}

variable "ZONE1" {
  default = "us-east-2a"
}

variable "AMIS" {
  type = map
  default = {
    us-east-2 = "ami-03657b56516ab7912"
    us-east-1 = "ami-0947d2ba12ee1ff75"
  }
}

variable "USER" {
  default = "ec2-user"
}

provider "aws" {
  region = var.REGION
}

resource "aws_key_pair" "terraform-key" {
  key_name   = "terraform-key"
  public_key = file("terraform-key.pub")
}

resource "aws_instance" "exercise3-inst" {
  ami                    = var.AMIS[var.REGION]
  instance_type          = "t2.micro"
  availability_zone      = var.ZONE1
  key_name               = aws_key_pair.terraform-key.key_name
  vpc_security_group_ids = ["your security group"]
  tags = {
    Name    = "exercise3-Instance"
    Project = "exercise3"
  }

  connection {
    user        = var.USER
    private_key = file("terraform-key")
    host        = self.public_ip
  }
}
output "PublicIP" {
  value = aws_instance.exercise3-instance.public_ip
}

output "PrivateIP" {
  value = aws_instance.exercise3-instance.private_ip
}

Explanation:

  1. Variables:

    • REGION: Sets the default AWS region to "us-east-2".

    • ZONE1: Sets the default availability zone to "us-east-2a".

    • AMIS: A map associating AWS regions with specific AMIs.

    • USER: Sets the default user to "ec2-user".

  2. Provider:

    • Configures the AWS provider to use the region specified by the REGION variable.
  3. AWS Key Pair:

    • Creates an SSH key pair named "terraform-key" using the public key file "terraform-key.pub".
  4. AWS Instance:

    • Creates an EC2 instance with the following properties:

      • ami: Uses the AMI specified in the AMIS map for the selected region.

      • instance_type: Sets the instance type to "t2.micro".

      • availability_zone: Uses the availability zone specified by the ZONE1 variable.

      • key_name: Associates the instance with the SSH key pair "terraform-key".

      • vpc_security_group_ids: Associates the instance with the specified security group.

      • tags: Adds tags to the instance for identification.

  5. Provisioners:

    • file: Uploads the web.sh script to the instance at /tmp/web.sh.

    • remote-exec: Executes commands on the instance to make the script executable and run it.

  6. Connection:

    • Defines the connection details for SSH:

      • user: Uses the user specified by the USER variable.

      • private_key: Uses the private key file "terraform-key".

      • host: Connects to the instance's public IP address.

Run these Terraform Commands

terraform init

This command initializes a Terraform working directory. It prepares the directory by downloading and installing the necessary provider plugins and setting up the backend configuration. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control.

terraform init

terraform fmt

This command formats the Terraform configuration files in the current directory to a canonical format and style. It ensures that the code is properly indented and organized, making it easier to read and maintain.

terraform fmt

terraform validate

This command checks the syntax and validity of the Terraform configuration files in the current directory. It ensures that the configuration is syntactically correct and that all required arguments are specified. This is a useful step to catch errors before applying the configuration.

terraform validate

terraform plan

This command creates an execution plan, showing what actions Terraform will take to achieve the desired state defined in the configuration files. It compares the current state with the desired state and lists the changes that will be made such as creating, updating, or deleting resources. This allows you to review the proposed changes before applying them.

terraform plan

terraform apply

This command applies the changes required to reach the desired state of the configuration as defined by the Terraform files. It creates, updates or deletes infrastructure resources to match the configuration. Before making any changes, Terraform will show a plan of the actions it will take and ask for your confirmation.

terraform apply