terraform简单的开始-安装和一些配置

terraform的安装:

官方下载:

浏览器打开terraform官方主页https://www.terraform.io/ 点击Download Terraform 跳转到程序下载页面:
image.png
找到自己对应的操作系统,按照操作系统选择安装terraform的方式:
image.png

linux为例:

我有一台 rocky linux 工作主机也是直接参照官方文档安装的:
image.png

terraform --version

不一样的windows:

官方的方式

注:我的系统是Windows10专业版!
image.png
image.png
but 要配置系统变量…我个人是很嫌弃麻烦。这里参考了ucloud在知乎上面的Chocolate的安装方式:

Chocolate

Terraform初体验(一) windows安装。不求甚解,先跑一遍:
以管理员身份打开powershell:
image.png

安装Chocolatey
  1. 以管理员身份打开PowerShell,输入Get-ExecutionPolicy,返回Bypass则代表目前执行的是绕过策略来安装。如果返回的是Restricted受限制的,则需要运行Set-ExecutionPolicy AllSigned 或者 Set-ExecutionPolicy Bypass -Scope Process。

image.png

  1. 既然是Restricted受限制的,复制下面这条命令即可安装完成,这里没有太多坑。
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::Sec

img_v2_26333acf-b915-4be8-9219-b0e16d11a7bg.jpg

通过Chocolatey安装Terraform

执行命令choco install terraform 按照提示中间输入y,安装完成后输入terraform --version 确认安装成功。
img_v2_64fb57f0-ee2a-4a94-8840-46634d5c096g.jpg
如果有网络或者其他问题可以参考ucloud的知乎笔记:https://zhuanlan.zhihu.com/p/266784852

vscode相关插件的安装

vs code 安装就忽略了 安装一下terraform的组件:
img_v2_dba9422b-21a1-435c-8fd8-c24ba8d6d80g.jpg

vscode terraform 腾讯云

凭证获取

参照腾讯云官方文档https://cloud.tencent.com/document/product/1653/82868
在首次使用 Terraform 之前,请前往 云 API 密钥页面 申请安全凭证 SecretId 和 SecretKey。若已有可使用的安全凭证,则跳过该步骤。

  1. 登录 访问管理控制台,在左侧导航栏,选择访问密钥 > API 密钥管理
  2. 在 API 密钥管理页面,单击新建密钥,即可以创建一对 SecretId/SecretKey。
    image.png
    创建一个项目文件夹,使用vs code打开:
    image.png

创建一个简单demo?

vscode 打开文件夹,以windows为例:
image.png从main.tf开始,创建一个main.tf文件
image.png
参照官方文档:

terraform {
  required_providers {
    tencentcloud = {
      source = "tencentcloudstack/tencentcloud"
      version = "1.81.25"
    }
  }
}

provider "tencentcloud" {

  # Configuration options
}

现在要在provider中引入腾讯云的配置。怎么样合理的引用呢?询问了一下cluda:
image.png
总结一下:

  1. 直接在provider模块中指定,这种应该是最直接的
  2. 环境变量
  3. 创建一个tfvar的文件写入
  4. 使用命令行参数

我个人这里下使用第三种了创建一个tfvar的文件…毕竟展示过程的时候可以不显示我的密钥哈哈哈哈

terraform init

初始化项目:

 terraform init

image.png
当然也会出现下面这种状况
image.png
image.png
参照:https://cloud.tencent.com/document/product/1653/82912,到用户加目录下创建.terraformrc文件。阿里云或者华为云应该也有类似的方法:

provider_installation {
  network_mirror {
    url = "https://mirrors.tencent.com/terraform/"
    // 限制只有腾讯云相关Provider, 从url中指定镜像源下载
    include = ["registry.terraform.io/tencentcloudstack/*"]   
  }
  direct {
    // 声明除了腾讯云相关Provider, 其它Provider依然从默认官方源下载
    exclude = ["registry.terraform.io/tencentcloudstack/*"]
  }
}

image.png
image.png

简单例子:查询区域下可用区列表:

创建一个 变量的文件credentials.tfvars

secret_id  = "xxxxxxxxxxxxxxx"
secret_key = "xxxxxxxxxxx"

image.png
创建main.tf文件:

terraform {
  required_providers {
    tencentcloud = {
      source = "tencentcloudstack/tencentcloud"
      version = "1.81.25"
    }
  }
}
variable "region" {
  description = "腾讯云地域"
  type    = string
  default     = "ap-shanghai"
}
variable "secret_id" {}
variable "secret_key" {}

# 设置腾讯云提供者
provider "tencentcloud" {
  secret_id  =var.secret_id
  secret_key = var.secret_key 
  region = var.region
}
data "tencentcloud_availability_zones" "availability_zones" {}

output "zones" {
  value = data.tencentcloud_availability_zones.availability_zones
}


terraform plan 检查:

 terraform plan -var-file=credentials.tfvars

image.png
terraform apply执行:

terraform apply -var-file=credentials.tfvars

image.png
就是列一个简单例子这里,主要是为了密钥文件与main 分开 当然了这里还有data output这些字段也会逐步展现一下!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先,您需要确保已经创建了ECS集群和服务。然后,您可以使用Terraform编写一个自动扩展组件,以便在需要时自动扩展ECS任务。 以下是一个示例Terraform配置文件,其中包含自动扩展组件的定义: ``` resource "aws_autoscaling_group" "ecs" { name = "ecs-autoscaling-group" launch_configuration = aws_launch_configuration.ecs.id min_size = 1 max_size = 10 desired_capacity = 1 vpc_zone_identifier = [aws_subnet.private.*.id] tag { key = "Name" value = "ecs-autoscaling-group" propagate_at_launch = true } lifecycle { create_before_destroy = true } depends_on = [ aws_security_group_rule.egress, aws_security_group_rule.ingress, ] } resource "aws_launch_configuration" "ecs" { name_prefix = "ecs-launch-config" image_id = data.aws_ami.ecs.id instance_type = "t2.micro" iam_instance_profile = "${aws_iam_instance_profile.ecs.id}" security_groups = [aws_security_group.ecs.id] user_data = <<-EOF #!/bin/bash echo ECS_CLUSTER=${var.ecs_cluster_name} >> /etc/ecs/ecs.config EOF } data "aws_ami" "ecs" { most_recent = true filter { name = "name" values = ["amazon-ecs-optimized"] } owners = ["amazon"] } resource "aws_iam_instance_profile" "ecs" { name = "ecs-instance-profile" role = "${aws_iam_role.ecs.id}" } resource "aws_iam_role" "ecs" { name = "ecs-role" assume_role_policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = "sts:AssumeRole" Effect = "Allow" Principal = { Service = "ec2.amazonaws.com" } } ] }) } resource "aws_security_group" "ecs" { name_prefix = "ecs-security-group" ingress { from_port = 0 to_port = 65535 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 65535 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } } resource "aws_security_group_rule" "ingress" { security_group_id = "${aws_security_group.ecs.id}" type = "ingress" from_port = 0 to_port = 65535 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } resource "aws_security_group_rule" "egress" { security_group_id = "${aws_security_group.ecs.id}" type = "egress" from_port = 0 to_port = 65535 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ``` 这个配置文件会创建一个自动扩展组件,其中包含一个启动配置和一个安全组。它还使用了一个IAM角色和IAM实例配置文件,以便ECS任务可以访问必要的资源。 在这个示例中,自动扩展组件将最小容量设置为1,最大容量设置为10。您可以根据需要调整这些值。 要应用此配置,请使用以下命令: ``` terraform init terraform apply ``` 这将创建自动扩展组件并将其应用于ECS集群。如果您需要更新配置,请使用`terraform apply`命令进行更新。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

对你无可奈何2008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值