Open y4h2 opened 1 year ago
kubernetes copy secret to other namespace: source
#!/bin/bash
set -e
/bin/echo -n '{ "token": "'
kubectl get -n consul secrets/hashicorp-consul-bootstrap-acl-token --template={{.data.token}}
/bin/echo -n '"}'
data "external" "token" {
program = ["sh", "${path.module}/consul-token.sh"]
}
resource "kubernetes_secret" "consul-token" {
depends_on = [data.external.token]
metadata {
name = "consul-token"
namespace = "app"
}
data = {
token = base64decode(data.external.token.result.token)
}
}
terraform {
required_providers {
external = {
source = "hashicorp/external"
version = ">= 2.0.0"
}
}
}
variable "project-sapphire-users" {
type = list(string)
default = [ "mary", "jack", "jill", "mack", "buzz", "mater"]
}
resource "aws_iam_user" "users" {
name = var.project-sapphire-users[count.index]
count = length(var.project-sapphire-users)
}
通过.tfvars传值
variable "region" {
}
.tfvars文件
region = "us-east-1"
State file
.tfstate文件
state locking: 防止其他人同时deploy
!!!千万不要用git存state
example:
check state
terraform state show aws_s3_bucket.finance
variable interpolation
${..}
example
resource "local_file" "state" {
filename = "/root/${var.local-state}"
content = "This configuration uses ${var.local-state} state"
}
How to set Terraform backend configuration dynamically
main idea: use .hcl file
backend.tf
terraform {
backend "remote" {}
}
backend.hcl
hostname = "app.terraform.io"
organization = "ministry-of-magic"
workspaces { Name = "sorting-hat-api-prod" }
command
terraform init -backend-config=backend.hcl
terraform workspace不能改变backend,只是在同一个backend内部切换
terrafrom environment variables: https://www.terraform.io/cli/config/environment-variables
TF_DATA_DIR change .terraform location
terraform output sensitive data
output "token_value" {
value = tfe_team_token.test.token
sensitive = true
}
terraform output -raw token_value
provisioner
Terraform可以在launch VM的时候运行脚本
taint: 需要手动升级时,可以先taint resource再untaint
debug:
import resource: terraform import command, 把资源导入到terraform的state中
public module example
module "iam_iam-user" {
source = "terraform-aws-modules/iam/aws//modules/iam-user"
version = "3.4.0"
# insert the 1 required variable here
}
need to set up credential in TF_CLI_CONFIG_FILE
example
functions
check with terraform console
Current course: KodeKloud: Terraform Basics Traning Course
理解resource,datasource和variable:
useful providers: