起程拓殖然后一键部署ECS

  介绍

这篇文章主要讲解了“起程拓殖然后一键部署ECS”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习起程拓殖然”后一键部署ECS”吧!

起程拓殖简介

HashiCorp起程拓殖是一个它基础架构自动化编排工具,可以用代码来管理维护它资源.Terraform的命令行接口(CLI)提供一种简单机制,用于将配置文件部署到阿里云或其他任意支持的云上,并对其进行版本控制。它编写了描述云资源拓扑的配置文件中的基础结构,例如虚拟机,存储帐户和网络接口。

起程拓殖是一个高度可扩展的工具,通过提供者来支持新的基础架构.Terraform能够让您在阿里云上轻松使用简单模板语言来定义,预览和部署云基础结构。您可以使用起程拓殖来创建,修改,删除ECS, VPC, RDS, SLB等多种资源。

安装和配置起程拓殖

在云壳中使用起程拓殖

阿里云云Shell是一款帮助您运维的免费产品,预装了起程拓殖的组件,并配置好身份凭证(凭证),因此您可直接在云壳中运行起程拓殖的命令。

打开浏览器,访问云壳的地址https://shell.aliyun.com。起程拓殖然后一键部署ECS

在本地安装和配置起程拓殖

登录起程拓殖官网下载并安装适用于您的操作系统的程序包。

命令运行后将显示可用起程拓殖选的项的列表,如下所示,表示安装完成。

用户名:~ $起程拓殖
  用法:,terraform  [- version], [-], & lt; command>, (args) 

创建环境变量,用于存放身份认证信息。

 export  ALICLOUD_ACCESS_KEY=癓TAIUrZCw3 * * * * * * * *”;
  export  ALICLOUD_SECRET_KEY=皕fwwWAMWIAiooj14GQ2 * * * * * * * * * * * * *”;
  export  ALICLOUD_REGION=癱n-beijing" 

编写起程拓殖脚本

这里选择在云壳中使用起程拓殖,创建相关目录:

 mkdir /home/壳/terraform_ecs
  cd /home/壳/terraform_ecs起程拓殖脚

本如下:

 variable “profile", {
  default =,才能“default"
  }
  
  #地区
  variable “region", {
  default =,才能“cn-shanghai"
  }
  
  #将公钥拷贝到ECS上
  locals  {
  user_data_ecs 才能=& lt; & lt; TEOF
  # !/bin/bash
  cp  ~/. ssh/authorized_keys /root/. ssh
  TEOF
  }
  
  provider “alicloud", {
  ,,region =var.region
  profile 才能=var.profile
  }
  
  # VPC
  module “vpc", {
  source 才能;,=,“阿里巴巴/vpc/alicloud"
  ,,region =var.region
  profile 才能=var.profile
  vpc_name =,才能“ecs_terraform"
  vpc_cidr 才能,,,,,,,,,,=,“10.10.0.0/16"
  availability_zones 才能=,(“cn-shanghai-b")
  vswitch_cidrs 才能,,,,,,=,(“10.10.1.0/24")
  }
  
  #安全组
  module “security_group", {
  source 才能;,=,“阿里巴巴/安全组/alicloud"
  profile 才能=var.profile
  ,,region =var.region
  ,,vpc_id =module.vpc.this_vpc_id
  
  ingress_cidr_blocks 才能=,(“0.0.0.0/0")
  时间=ingress_ports 才能;[22]
  
  ingress_with_cidr_blocks_and_ports 才能=,(
  ,,,{
  ,,,,,protocol ,,,=,“tcp"
  ,,,,,priority ,,=, 1
  ,,,,,description =,“ingress  for  ssh"
  ,,,}
  ,,)
  }
  
  # ECS
  module “ecs", {
  source 才能;,=,“阿里巴巴/ecs-instance/alicloud"
  profile 才能=var.profile
  ,,region =var.region
  internet_max_bandwidth_out 才能=,1
  associate_public_ip_address 才能=,真的
  
  name 才能,,,,,,,,,,,,,,,,,,,,,,,,=,“terraform_ecs"
  image_id 才能,,,,,,,,,,,,,,,,,,,,=,“centos_7_9_x64_20G_alibase_20201228.vhd"
  instance_type 才能,,,,,,,,,,,,,,,=,“ecs.t5-c1m2.xlarge",, #实例规格
  vswitch_id 才能,,,,,,,,,,,,,,,,,,=module.vpc.this_vswitch_ids.0
  security_group_ids 才能,,,,,,,,,,=,(module.security_group.this_security_group_id)
  
  system_disk_size 才能,,,,,=,30岁
  时间=number_of_instances 才能;3,,#实例数量
  
  user_data 才能=local.user_data_ecs
  }
  
  #设置本地~/. ssh/config的ssh信息
  resource “local_file",“ssh_config", {
  ,,,content ,,,,=, & lt; & lt; EOF
  % {,for  ip 拷贝module.ecs.this_public_ip }
  Host  ecs ${指数(module.ecs.this_public_ip, ip), +, 1}
  ,,StrictHostKeyChecking 没有
  ,,,HostName  $ {ip}
  ,,,User 起程拓殖
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null

起程拓殖然后一键部署ECS