5命令部分-查看基础设施.md
Terraform 中本身使用代码定义资源,定义好的资源有一些信息可能需要通过代码再次查看,这个时候有一些命令可以帮助输出
Graph命令
默认情况下生成一个简化的图形,描述resource跟data, 也可以通过type来指定不同的内容,支持的type 有 plan
, plan-refresh-only
, plan-destroy
, or apply
.
这个功能生成的图片格式是DOT,这是一种文字描述的图片,需要用dot命令来进行转换,具体用发如下:
[waooo!!@terraform test]$ terraform graph -type=plan | dot -Tpng >graph.png
[waooo!!@terraform test]$ terraform graph -type=plan |
> ^C
[waooo!!@terraform test]$ terraform graph -type=plan
digraph {
compound = "true"
newrank = "true"
subgraph "root" {
"[root] data.aws_instances.filtered_running_instances (expand)" [label = "data.aws_instances.filtered_running_instances", shape = "box"]
"[root] data.aws_s3_directory_buckets.example (expand)" [label = "data.aws_s3_directory_buckets.example", shape = "box"]
"[root] provider[\"registry.terraform.io/hashicorp/aws\"]" [label = "provider[\"registry.terraform.io/hashicorp/aws\"]", shape = "diamond"]
"[root] data.aws_instances.filtered_running_instances (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/aws\"]"
"[root] data.aws_s3_directory_buckets.example (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/aws\"]"
"[root] output.bucket (expand)" -> "[root] data.aws_s3_directory_buckets.example (expand)"
"[root] output.test (expand)" -> "[root] data.aws_instances.filtered_running_instances (expand)"
"[root] provider[\"registry.terraform.io/hashicorp/aws\"] (close)" -> "[root] data.aws_instances.filtered_running_instances (expand)"
"[root] provider[\"registry.terraform.io/hashicorp/aws\"] (close)" -> "[root] data.aws_s3_directory_buckets.example (expand)"
"[root] root" -> "[root] local.foo (expand)"
"[root] root" -> "[root] output.bucket (expand)"
"[root] root" -> "[root] output.test (expand)"
"[root] root" -> "[root] provider[\"registry.terraform.io/hashicorp/aws\"] (close)"
}
}
output命令
这个命令用于从state文件中再次输出定义的output块的值
-json
这个命令输出的时候,支持一个-json的选项,很方便处理,可以用jq做下一步处理
-raw
这个参数有点没用,,,转换为字符串,但是只支持 string number bool类型,,,复杂数据类型建议用-json
-state-path
不需多说,,指定一个state文件得位置,默认为“terraform.tfstate”。使用远程状态时忽略。
[waooo!!@terraform test]$ terraform output -json |jq -r
{
"test": {
"sensitive": false,
"type": [
"list",
"string"
],
"value": [
[waooo!!@terraform test]$ cat data.tf
data "aws_instances" "filtered_running_instances" {
instance_state_names = ["running"]
# filter {
# name = "tag:Name"
# values = ["tagvalue"]
# }
}
output "test" {
value = data.aws_instances.filtered_running_instances.ids
}
show命令
terraform show
命令用于从状态或计划文件提供人类可读的输出。这可用于检查计划以确保计划的操作是预期的,或者检查 Terraform 看到的当前状态。
参数只有-json
state list命令
这个就比较常用了,当希望列出当前管理的resources的时候,很好用
可以直接给地址, terraform 资源地址
比如
data.aws_instances.filtered_running_instances
也可以通过-id 来指定资源的id 比如你的sg id 啥的
terraform state list -id=sg-1234abcd
module.elb.aws_security_group.sg
这个可以用来反查这个资源在哪儿个资源使用
state show命令
这个命令就是展示上边的命令list出来的结果 比如
[waooo!!@terraform test]$ terraform state show data.aws_instances.filtered_running_instances
# data.aws_instances.filtered_running_instances:
data "aws_instances" "filtered_running_instances" {
id = "cn-north-1"
ids = [