## 創建一臺主機的需求 ##
1. 2.5 GHz主頻的Intel Xeon E5-2680 v3 1個cpu
2. 1GIB內存
3. 硬盤 40g 高效云盤
4. 網絡和交換機(內網/外網IP)
## 安裝阿里云cli ##
1. 下載 https://s3.amazonaws.com/aws-cli/AWSCLI64.msi并安裝
## 注冊并使用阿里云 ##
1. 可以使用支付寶注冊登錄(暫時省略)
2. 使用系統默認的access key(暫時不考慮安全和權限的問題)
3. 登錄阿里云->點擊頭像->access key管理->繼續使用access key
AccessKey ID | 狀態 | 最后使用時間 | 創建時間 | 操作
-|-|-|-|-
AccessKeyId | 已啟用 | 2021年4月30日17:22:56 | 2021年4月29日11:04:17 | 查看 Secret 禁用 刪除
4. 在cmd 運行aliyun configure --profile course,分別輸入
```
Access Key Id [*********************BVw]:
Access Key Secret [***************************Qht]:
Default Region Id [cn-qingdao]:
Default Output Format [json]: json (Only support json)
Default Language [zh|en] en:
```
## 用一個最簡單的例子創建一個linux主機 ##
1. main.tf
```
provider "alicloud" {
profile = "course"
region = "cn-qingdao"
}
resource "alicloud_instance" "server" {
# image id
image_id = "centos_7_7_x64_20G_alibase_20200329.vhd"
# 根據流量付費
internet_charge_type = "PayByTraffic"
# 主機類型
instance_type = "ecs.n1.tiny"
# 主機名
instance_name = "aliyun-lab-02"
# 密碼
password = "1234Qwer"
# 自帶外網ipv4 配置
# 設置internet_max_bandwidth_out > 0 可以分配一個public IP
internet_max_bandwidth_out = "100"
# 設置專有網絡
vswitch_id = alicloud_vswitch.vswitch.id
# secrity group
security_groups = ["${alicloud_security_group.sg.id}"]
}
# 創建security group
resource "alicloud_security_group" "sg" {
vpc_id = alicloud_vpc.vpc.id
}
# 創建專有網絡
resource "alicloud_vpc" "vpc" {
cidr_block = "172.16.0.0/16"
}
# 創建交換機
resource "alicloud_vswitch" "vswitch" {
vpc_id = alicloud_vpc.vpc.id
cidr_block = "172.16.0.0/24"
zone_id = data.alicloud_zones.default.zones[0].id
}
# 查詢可用區
data "alicloud_zones" "default" {
available_instance_type = data.alicloud_instance_types.instance_type.instance_types[0].id
}
# 查詢能用的主機類型
data "alicloud_instance_types" "instance_type" {
instance_type_family = "ecs.n1"
cpu_core_count = "1"
memory_size = "1"
}
```
2. 運行terraform init /plan /apply 輸入yes
```
D:\course\example\1.02>terraform init
Initializing the backend...
Initializing provider plugins...
- Reusing previous version of hashicorp/alicloud from the dependency lock file
- Installing hashicorp/alicloud v1.122.0...
- Installed hashicorp/alicloud v1.122.0 (self-signed, key ID 34365D9472D7468F)
Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/plugins/signing.html
Warning: Additional provider information from registry
The remote registry returned warnings for
registry.terraform.io/hashicorp/alicloud:
- For users on Terraform 0.13 or greater, this provider has moved to
aliyun/alicloud. Please update your source in required_providers.
Warning: Quoted references are deprecated
on main.tf line 12, in resource "alicloud_instance" "web":
12: provider = "alicloud"
In this context, references are expected literally rather than in quotes.
Terraform 0.11 and earlier required quotes, but quoted references are now
deprecated and will be removed in a future version of Terraform. Remove the
quotes surrounding this reference to silence this warning.
Warning: Interpolation-only expressions are deprecated
on main.tf line 20, in resource "alicloud_instance" "web":
20: security_groups = ["${alicloud_security_group.group.id}"]
Terraform 0.11 and earlier required all non-constant expressions to be
provided via interpolation syntax, but this pattern is now deprecated. To
silence this warning, remove the "${ sequence from the start and the }"
sequence from the end of this expression, leaving just the inner expression.
Template interpolation syntax is still used to construct strings from
expressions when the template includes multiple interpolation sequences or a
mixture of literal strings and interpolations. This deprecation applies only
to templates that consist entirely of a single interpolation sequence.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
D:\course\example\1.02>terraform apply
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# alicloud_instance.web will be created
+ resource "alicloud_instance" "web" {
+ availability_zone = (known after apply)
+ credit_specification = (known after apply)
+ deletion_protection = false
+ dry_run = false
+ host_name = (known after apply)
+ id = (known after apply)
+ image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
+ instance_charge_type = "PostPaid"
+ instance_name = "web"
+ instance_type = "ecs.n1.tiny"
+ internet_max_bandwidth_in = (known after apply)
+ internet_max_bandwidth_out = 0
+ io_optimized = "optimized"
+ key_name = (known after apply)
+ private_ip = (known after apply)
+ public_ip = (known after apply)
+ role_name = (known after apply)
+ security_groups = (known after apply)
+ spot_strategy = "NoSpot"
+ status = "Running"
+ subnet_id = (known after apply)
+ system_disk_category = "cloud_efficiency"
+ system_disk_performance_level = (known after apply)
+ system_disk_size = 40
+ volume_tags = (known after apply)
+ vswitch_id = (known after apply)
}
# alicloud_security_group.group will be created
+ resource "alicloud_security_group" "group" {
+ id = (known after apply)
+ inner_access = (known after apply)
+ inner_access_policy = (known after apply)
+ name = "my_secutiry_group"
+ security_group_type = "normal"
+ vpc_id = (known after apply)
}
# alicloud_vpc.vpc will be created
+ resource "alicloud_vpc" "vpc" {
+ cidr_block = "172.16.0.0/16"
+ enable_ipv6 = false
+ id = (known after apply)
+ ipv6_cidr_block = (known after apply)
+ name = (known after apply)
+ resource_group_id = (known after apply)
+ route_table_id = (known after apply)
+ router_id = (known after apply)
+ router_table_id = (known after apply)
+ status = (known after apply)
+ vpc_name = "my_vpc"
}
# alicloud_vswitch.vswitch will be created
+ resource "alicloud_vswitch" "vswitch" {
+ availability_zone = (known after apply)
+ cidr_block = "172.16.0.0/24"
+ id = (known after apply)
+ name = (known after apply)
+ status = (known after apply)
+ vpc_id = (known after apply)
+ vswitch_name = "my_switch"
+ zone_id = "cn-qingdao-b"
}
Plan: 4 to add, 0 to change, 0 to destroy.
Warning: "io_optimized": [DEPRECATED] Attribute io_optimized has been deprecated on instance resource. All the launched alicloud instances will be IO optimized. Suggest to remove it from your template.
on main.tf line 10, in resource "alicloud_instance" "web":
10: resource "alicloud_instance" "web" {
Warning: Quoted references are deprecated
on main.tf line 12, in resource "alicloud_instance" "web":
12: provider = "alicloud"
In this context, references are expected literally rather than in quotes.
Terraform 0.11 and earlier required quotes, but quoted references are now
deprecated and will be removed in a future version of Terraform. Remove the
quotes surrounding this reference to silence this warning.
Warning: Interpolation-only expressions are deprecated
on main.tf line 20, in resource "alicloud_instance" "web":
20: security_groups = ["${alicloud_security_group.group.id}"]
Terraform 0.11 and earlier required all non-constant expressions to be
provided via interpolation syntax, but this pattern is now deprecated. To
silence this warning, remove the "${ sequence from the start and the }"
sequence from the end of this expression, leaving just the inner expression.
Template interpolation syntax is still used to construct strings from
expressions when the template includes multiple interpolation sequences or a
mixture of literal strings and interpolations. This deprecation applies only
to templates that consist entirely of a single interpolation sequence.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
alicloud_vpc.vpc: Creating...
alicloud_vpc.vpc: Creation complete after 6s [id=vpc-m5enazzdoyz6q50nusgty]
alicloud_security_group.group: Creating...
alicloud_vswitch.vswitch: Creating...
alicloud_security_group.group: Creation complete after 2s [id=sg-m5e5motrcqfyzwavqfl8]
alicloud_vswitch.vswitch: Creation complete after 6s [id=vsw-m5ej840mcxi56a8e2tae0]
alicloud_instance.web: Creating...
alicloud_instance.web: Still creating... [10s elapsed]
alicloud_instance.web: Still creating... [20s elapsed]
alicloud_instance.web: Still creating... [30s elapsed]
alicloud_instance.web: Creation complete after 33s [id=i-m5e5buxy1fal6swuthub]
Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
```
3. 查看阿里云后臺,發現一臺web主機已經被創建
4. 執行terraform destroy 輸入yes 銷毀主機以及關聯資源
實例ID/名稱 | 標簽 | 監控 | 可用區 | IP地址 | 狀態 | 配置 | 付費方式
-|-|-|-|-|-|-|-
i-m5e8nuefjsnx3bhjdkyi web | - |青島 可用區B | 172.16.0.5(私有) |運行中 |1 vCPU 1 GiB (I/O優化) | ecs.n1.tiny 0Mbps (峰值) | 按量 | 2021年5月6日16:23 創建
5. 查看當前terraform生成的文件
```
D:\course\example\1.02>dir
Volume in drive D is UserProfile
Volume Serial Number is 8A16-AC57
Directory of D:\course\example\1.02
2021/05/06 16:29 <DIR> .
2021/05/06 16:29 <DIR> ..
2021/05/06 14:26 <DIR> .terraform
2021/05/06 16:17 1,083 .terraform.lock.hcl
2021/05/06 16:10 1,341 main.tf
2021/05/06 16:29 157 terraform.tfstate
2021/05/06 16:28 7,463 terraform.tfstate.backup
4 File(s) 10,044 bytes
3 Dir(s) 97,182,687,232 bytes free
```
- 【概要簡介】1.001-簡介-本課程體系簡介
- 【概要簡介】1.002-簡介-云的發展趨勢
- 【概要簡介】1.003-簡介-國內主流的云服務平臺
- 【概要簡介】1.004-簡介-Terraform云平臺devops的瑞士軍刀
- 【快速入門】2.000-快速入門-云后臺創建主機
- 【快速入門】2.001-快速入門-創建主機-先查詢資源
- 【快速入門】2.002-快速入門-創建主機-阿里云
- 【快速入門】2.003-快速入門-創建主機-亞馬遜aws
- 【快速入門】2.100-快速入門-官方網站學習資料
- 【快速入門】2.101-快速入門-Terraform主要的文件.
- 【快速入門】2.102-快速入門-創建虛擬網絡
- 【快速入門】1.103-快速入門-創建安全和規則
- 【快速入門】2.104-快速入門-創建云盤
- 【快速入門】2.105-快速入門-創建Ecs主機服務
- 【快速入門】2.200-快速入門-搭建Nginx
- 【快速入門】2.201-快速入門-搭建Nginx-分拆main文件
- 【快速入門】2.203-快速入門-購買固定IP并綁定到主機
- 【快速入門】2.204-快速入門-綁定域名到自己的主頁
- 【快速入門】2.205-快速入門-申請免費的https證書并綁定TODO
- 【快速入門】2.300-快速入門-創建Oss存儲并搭建靜態網站
- 【快速入門】2.300-快速入門-Cdn-TODO
- 【中級進階】3.101-中級進階-用Packer創建Wordpress的主機鏡像
- 【中級進階】3.102-中級進階-利用自己的鏡像創建Ecs主機
- 【中級進階】3.104-中級進階-用自己鏡像搭建負載均衡的Wordpress網站
- 【中級進階】3.105-中級進階-利用彈性伸縮ESS創建SLB對應的動態數量的ECS服務器
- 【中級進階】3.106-中級進階-自定義Module搭建多個負載均衡的Wordpress網站
- 【中級進階】3.200-中級進階-備份多個Wordpress服務器的日志到ossTODO
- 【中級進階】3.300-中級進階-創建Ecs快照服務
- 【中級進階】3.400-中級進階-Datasource查詢
- 【中級進階】3.500-中級進階-保存Terraform的state文件
- 【中級進階】3.600-中級進階-Variables and environment variables
- 【中級進階】3.700-中級進階-Terraform命令解析
- 【高級深入】4.000-高級深入-創建容器鏡像服務
- 【高級深入】4.001-高級深入-編譯自己的docker鏡像并上傳到Aliyun cr
- 【高級深入】4.002-高級深入-Terraform創建Kubernetes集群
- 【高級深入】4.003-高級深入-Terraform在Kubernetes集群中部署Wordpress應用
- 【高級深入】4.100-高級深入-使用云服務平臺提供的module來創建服務資源
- 【高級深入】4.200-高級深入-同步Terraform的狀態.到terraform cloud
- 【高級深入】4.300-高級深入-Terraform的語法
- 【高級深入】4.400-高級深入-Terraform與其Ansible一起使用
- 【高級深入】4.500-高級深入-Terraform的provisioner
- 【高級深入】4.600-高級深入-Snat
- 【高級深入】4.601-高級深入-Dnat
- 4.602-高級深入-Vpc-vpc通信
- 【高級深入】4.603-高級深入-Switch與switch-通信