惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

M
MIT News - Artificial intelligence
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
博客园 - Franky
腾讯CDC
T
Tailwind CSS Blog
Recent Announcements
Recent Announcements
V
V2EX
N
Netflix TechBlog - Medium
量子位
Jina AI
Jina AI
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
爱范儿
爱范儿
博客园 - 叶小钗
D
Docker
MongoDB | Blog
MongoDB | Blog
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss

mafeifan 的编程技术分享

mafengwo-mp3-downloader | mafeifan 的编程技术分享 示例页面 | mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 查看 default namespace 下的 default service account名称 mafeifan 的编程技术分享 检查日志 | mafeifan 的编程技术分享 bridge fdb show dev flannel.1 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 数据收集原理分析 | mafeifan 的编程技术分享
mafeifan 的编程技术分享
2026-01-16 · via mafeifan 的编程技术分享

配置别名 ​

bash

alias tf="terraform"
alias tfa="terraform apply"
alias tfp="terraform plan"

加速 tf init ​

执行 tf init 时,让 provider 从本地目录搜索安装,解决因为联网超时,导致init失败

  1. 创建TF配置文件,参数说明

Windows 系统: C:\Users\your_user_name\AppData\Roaming\terraform.rc MacOS: ~/.terraformrc

disable_checkpoint = true
disable_checkpoint_signature = true
plugin_cache_dir = "D:/terraform-providers"
provider_installation {
    filesystem_mirror {
        path = "D:/terraform-providers"
        include = ["registry.terraform.io/*/*"]
    }
}
  1. 创建缓存目录 mkdir -p ~/.terraform.d/plugin-cache
	.terraform.d
	├── checkpoint_cache
	├── checkpoint_signature
	├── credentials.tfrc.json
	└── plugin-cache
	    └── registry.terraform.io
	        └── hashicorp
	            ├── archive
	            │   └── 2.2.0
	            │       └── darwin_amd64
	            │           └── terraform-provider-archive_v2.2.0_x5
	            ├── aws
	            │   ├── 4.35.0
	            │   │   └── darwin_amd64
	            │   │       └── terraform-provider-aws_v4.35.0_x5
	            │   ├── 4.38.0
	            │   │   └── darwin_amd64
	            │   │       └── terraform-provider-aws_v4.38.0_x5
	            │   ├── 4.40.0
	            │   │   ├── 4.40.0.json
	            │   │   └── darwin_amd64
	            │   │       └── terraform-provider-aws_v4.40.0_x5
	            │   ├── 4.44.0
	            │   │   └── darwin_amd64
	            │   │       └── terraform-provider-aws_v4.44.0_x5
            │   └── index.json
  1. 这样 terraform init 就会使用本地目录,或者 显式指定 terraform init -plugin-dir=~/.terraform.d/plugin-cache

修改 module 名字 ​

需求: 要改代码中 module 的名字, module-demo-1 为 module-demo-2

hcl

module "module-demo-1" {
// ....
}
  1. 指令先执行 tf init
  2. 再执行 tf state mv module.module-demo-1 module.module-demo-2
  3. 这样 plan 后不会发生变化

根据变量动态创建 resource ​

hcl

resource "aws_route53_record" "gov-apigw-inout-dns-record" {
  count   = var.env_name == "prod" ? 1 : 0
  zone_id = data.aws_route53_zone.primary.id
  name    = var.acm_domain_name
  type    = "CNAME"
  ttl     = 300
  records = [aws_api_gateway_domain_name.gov-apigw-inout-domain.regional_domain_name]
}

关于 aws 的 tags 和 tags_all ​

Tag属性表示在 Terraform 状态文件中的特定 resource 的 tag,而tag_all是在 provider 上指定的 resource tag 和 default tag 的总和。

举个例子,如果我们想给asg创建出来的ec2添加默认tag,需要这么写

hcl

provider "aws" {
  profile = "default"
  region  = "us-east-2"

  default_tags {
    tags = {
      Environment     = "Test"
      Service         = "Example"
      HashiCorp-Learn = "aws-default-tags"
    }
  }
}

data "aws_default_tags" "current" {}

resource "aws_autoscaling_group" "example" {
  availability_zones = data.aws_availability_zones.available.names
  desired_capacity   = 1
  max_size           = 1
  min_size           = 1

  launch_template {
    id      = aws_launch_template.example.id
    version = "$Latest"
  }
  # 这里使用dynamic关键字动态给resource添加tag
  dynamic "tag" {
    for_each = data.aws_default_tags.current.tags
    content {
      key                 = tag.key
      value               = tag.value
      propagate_at_launch = true
    }
  }
}

使用 terraform console 调试函数 ​

$ terraform console
> concat(["a"], ["b"])
[
  "a",
  "b",
]
> max(4,12,7)
12

有用的站点 ​