Using Outputs to Expose Terraform Provisioned Data
In this tutorial, we will cover how to use outputs in Terraform to expose data about the resources you have provisioned. Outputs are a way to make information about your infrastructure available after the apply phase.
Defining Outputs
Outputs are defined using the output block. Each output can have a name, a value, and an optional description. Outputs can reference resource attributes, data sources, and even other outputs.
Create a file named outputs.tf and define some outputs:
output "instance_id" {
description = "The ID of the EC2 instance"
value = aws_instance.example.id
}
output "instance_public_ip" {
description = "The public IP address of the EC2 instance"
value = aws_instance.example.public_ip
}
output "instance_arn" {
description = "The ARN of the EC2 instance"
value = aws_instance.example.arn
}
In this configuration:
instance_id: Exposes the ID of the EC2 instance.instance_public_ip: Exposes the public IP address of the EC2 instance.instance_arn: Exposes the Amazon Resource Name (ARN) of the EC2 instance.
Using Outputs
After defining outputs, you can use them in several ways:
1. Viewing Outputs in the CLI
After applying your Terraform configuration, you can view the outputs using the terraform output command:
terraform apply
terraform output
This will display the values of the defined outputs:
instance_id = "i-0abcd1234efgh5678"
instance_public_ip = "203.0.113.42"
instance_arn = "arn:aws:ec2:us-west-2:123456789012:instance/i-0abcd1234efgh5678"
2. Referencing Outputs in Other Configurations
You can reference outputs from one Terraform configuration in another configuration by using the terraform_remote_state data source. This is useful for creating modular Terraform configurations.
Example: Assuming you have another Terraform configuration in a different directory, you can reference the outputs like this:
data "terraform_remote_state" "example" {
backend = "local"
config = {
path = "../path_to_your_state_file/terraform.tfstate"
}
}
resource "aws_security_group" "example" {
name_prefix = "example-sg"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [data.terraform_remote_state.example.outputs.instance_public_ip]
}
}
3. Using Outputs in Automation Scripts
Outputs can be used in automation scripts or CI/CD pipelines to retrieve information about your infrastructure and perform further actions. For example, you can use a script to retrieve the public IP of an instance and SSH into it.
INSTANCE_PUBLIC_IP=$(terraform output -raw instance_public_ip)
ssh -i your_key.pem ec2-user@$INSTANCE_PUBLIC_IP
Applying the Configuration
To apply your configuration and generate the outputs, follow these steps:
Step 1: Initialize Terraform
Initialize your Terraform configuration:
terraform init
Step 2: Apply the Configuration
Apply your configuration to create the resources and generate the outputs:
terraform apply
Review the plan and confirm the apply action. Once completed, you can view the outputs using the terraform output command.
Conclusion
By using outputs in Terraform, you can easily expose and retrieve data about the resources you have provisioned. This tutorial covered how to define outputs, view them in the CLI, reference them in other configurations, and use them in automation scripts.
No comments:
Post a Comment