1. 预备环境
安装了terraform 的软件的操作系统(windows linux mac 均可)
具有阿里云账户的 access_key secret_key
2. 配置
// terraform 的配置相对比较随意,但是有几个必须注意的,文件后缀 tf 文件名不需要进行特殊说明
// 以下为我使用aliyun 提供的provider 进行的使用
说明:
app.tf // 资源创建的定义
va.tf // 变量的定义 app.tf # Configure the Alicloud Provider
provider "alicloud" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
} # Create a web server
resource "alicloud_instance" "web" {
# cn-beijing
provider = "alicloud"
availability_zone = "cn-beijing-b"
image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" # instance_network_type = "Classic"
internet_charge_type = "PayByBandwidth" instance_type = "ecs.n1.medium"
io_optimized = "optimized"
system_disk_category = "cloud_efficiency"
security_groups = ["${alicloud_security_group.default.id}"]
instance_name = "web"
} # Create security group
resource "alicloud_security_group" "default" {
name = "default"
provider = "alicloud"
description = "default"
} 上面的代码比较简单,就是声明了,需要创建的esc 实例的信息 包括区域、网络类型、镜像id、默认的安全策略 va.tf
variable "access_key" { default = "xxxxxxxxxxxxxxxxxxxxxxx"}
variable "secret_key" { default = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
variable "region" {
default = "cn-beijing"
} 变量的定义,主要是key、区域
3. 使用
// 初始化 init
terraform init
// 查看信息 plan
terraform plan
// 进行资源创建
terraform apply
// 释放资源
terraform destroy 每个步骤的信息类似如下:
The following providers do not have any version constraints in configuration,
so the latest version was installed. To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below. * provider.alicloud: version = "~> 0.1" 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. Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage. ------------------------------------------------------------------------ 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
id: <computed>
allocate_public_ip: "false"
availability_zone: "cn-beijing-b"
host_name: <computed>
image_id: "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
instance_name: "web"
instance_type: "ecs.n1.medium"
internet_charge_type: "PayByBandwidth"
io_optimized: "optimized"
private_ip: <computed>
public_ip: <computed>
security_groups.#: <computed>
status: <computed>
subnet_id: <computed>
system_disk_category: "cloud_efficiency"
system_disk_size: <computed> + alicloud_security_group.default
id: <computed>
description: "default"
name: "default" Plan: 2 to add, 0 to change, 0 to destroy. ------------------------------------------------------------------------ Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run. alicloud_security_group.default: Creating...
description: "" => "default"
name: "" => "default"
alicloud_security_group.default: Creation complete after 1s (ID: sg-2ze0dkuqzl6tfr1pfuul)
alicloud_instance.web: Creating...
allocate_public_ip: "" => "false"
availability_zone: "" => "cn-beijing-b"
host_name: "" => "<computed>"
image_id: "" => "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
instance_name: "" => "web"
instance_type: "" => "ecs.n1.medium"
internet_charge_type: "" => "PayByBandwidth"
io_optimized: "" => "optimized"
private_ip: "" => "<computed>"
public_ip: "" => "<computed>"
security_groups.#: "" => "1"
security_groups.3325292532: "" => "sg-2ze0dkuqzl6tfr1pfuul"
status: "" => "<computed>"
subnet_id: "" => "<computed>"
system_disk_category: "" => "cloud_efficiency"
system_disk_size: "" => "<computed>"
alicloud_instance.web: Still creating... (10s elapsed)
alicloud_instance.web: Still creating... (20s elapsed)
alicloud_instance.web: Still creating... (30s elapsed)
alicloud_instance.web: Still creating... (40s elapsed)
alicloud_instance.web: Still creating... (50s elapsed)
alicloud_instance.web: Still creating... (1m0s elapsed)
alicloud_instance.web: Creation complete after 1m2s (ID: i-2ze64rcu238frkzql1sf) Apply complete! Resources: 2 added, 0 changed, 0 destroyed. alicloud_security_group.default: Refreshing state... (ID: sg-2ze0dkuqzl6tfr1pfuul)
alicloud_instance.web: Refreshing state... (ID: i-2ze64rcu238frkzql1sf) An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
- destroy Terraform will perform the following actions: - alicloud_instance.web - alicloud_security_group.default Plan: 0 to add, 0 to change, 2 to destroy. Do you really want to destroy?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes alicloud_instance.web: Destroying... (ID: i-2ze64rcu238frkzql1sf)
alicloud_instance.web: Still destroying... (ID: i-2ze64rcu238frkzql1sf, 10s elapsed)
alicloud_instance.web: Destruction complete after 11s
alicloud_security_group.default: Destroying... (ID: sg-2ze0dkuqzl6tfr1pfuul)
alicloud_security_group.default: Destruction complete after 1s Destroy complete! Resources: 2 destroyed.
4. 阿里云管理界面的显示效果
 
 
5. 总结
总的来说还是比较简单的,使用起来也比较方便,对于多云管理方便了好多,同时结合每个云厂商的实际
最佳实践,可以子啊我们的实际产品开发商,降低对于云厂商的较大依赖,同时可以方便的进行管理
目前发现的一些问题:
文档上有些还不是很全、部分文档更新不及时。
同时在积极适应国际化市场,看齐国际厂商上,给予阿里一个大大的赞,希望国内的一些搞云的公司和学习下。
6. 参考资料
https://www.terraform.io/intro/getting-started/build.html
https://www.terraform.io/docs/providers/alicloud/r/instance.html#internet_charge_type
http://www.infoq.com/cn/news/2015/05/hashimoto-modern-datacenter
http://www.infoq.com/cn/news/2017/10/terraform-multicloud-advances
 
 
 
 
 

terraform 阿里云基本使用的更多相关文章

  1. 持续优化云原生体验,阿里云在Serverless容器与多云上的探索

    近日,阿里云宣布推出Serverless Kubernetes服务此举意在降低容器技术的使用门槛.简化容器平台运维.并同时发布阿里云服务对Open Service Broker API标准支持,通过一 ...

  2. 阿里云发布CloudOps白皮书,ECS自动化运维套件新升级

    12月10 日,2021云上架构与运维峰会上,阿里云发布业界首部<云上自动化运维白皮书>(简称CloudOps白皮书),并在其中提出了CloudOps成熟度模型.同时,阿里云还宣布了ECS ...

  3. [linux]阿里云主机的免登陆安全SSH配置与思考

    公司服务器使用的第三方云端服务,即阿里云,而本地需要经常去登录到服务器做相应的配置工作,鉴于此,每次登录都要使用密码是比较烦躁的,本着极速思想,我们需要配置我们的免登陆. 一 理论概述 SSH介绍 S ...

  4. 阿里云直播 C# SDK 如何使用

    阿里云直播SDK的坑 1.直播云没有单独的SDK,直播部分被封装在CDN的相关SDK当中. 2.针对SDK,没有相关Demo. 3.针对SDK,没有相关的文档说明. 4.针对SDK的说明,官网上的说明 ...

  5. 8.仿阿里云虚拟云服务器的FTP(包括FTP文件夹大小限制)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#iis 原文:http://dnt.dkill.net/Ar ...

  6. 阿里云服务器上配置并使用: PHP + Redis + Mysql 从配置到使用

    (原创出处为本博客,http://www.cnblogs.com/linguanh/) 目录: 一,下载 二,解压 三,配置与启动 四,测试 Redis 五,配置 phpRedis 扩展 六,综合测试 ...

  7. 阿里云学生优惠Windows Server 2012 R2安装IIS,ftp等组件,绑定服务器域名,域名解析到服务器,域名备案,以及安装期间错误的解决方案

     前言: 这几天终于还是按耐不住买了一个月阿里云的学生优惠.只要是学生,在学信网上注册过,并且支付宝实名认证,就可以用9块9的价格买阿里云的云服务ECS.确实是相当的优惠. 我买的是Windows S ...

  8. 【初码干货】使用阿里云对Web开发中的资源文件进行CDN加速的深入研究和实践

    提示:阅读本文需提前了解的相关知识 1.阿里云(https://www.aliyun.com) 2.阿里云CDN(https://www.aliyun.com/product/cdn) 3.阿里云OS ...

  9. FineReport如何用JDBC连接阿里云ADS数据库

    在使用FineReport连接阿里云的ADS(AnalyticDB)数据库,很多时候在测试连接时就失败了.此时,该如何连接ADS数据库呢? 我们只需要手动将连接ads数据库需要使用到的jar放置到%F ...

随机推荐

  1. float 为什么可以表示很大的整数

    1.float型:单精度浮点数在机内占4个字节,用32位二进制描述(注意:计算机中1个字节=8位). 2.浮点数在机内用指数型式表示,分解为:数符,尾数,指数符,指数四部分. 3.可以算出float型 ...

  2. 单片机、嵌入式CAN通信原理

    工作原理: 单片机里内置了一个FIFO(先进先出)芯片,需要发送什么报文,就往这个芯片里写.比如有两个单片机作为CAN节点,A节点往自己的FIFO中写CAN报文,B节点往自己的FIFO中写CAN报文. ...

  3. 伸展树基础(Splay)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3948  Solved: 1627 [Submit][St ...

  4. Introspector内省和反射的区别.

    Introspector 是一个专门处理bean的工具类.用来获取Bean体系里的 propertiesDescriptor,methodDescriptor. 要理解这个,就要理解下面几个议题.   ...

  5. ubuntu 16.04 vscode + php debug

    1.vscode 安装PHP Debug扩展: 2.php环境配置: 1.安装xdebug扩展: sudo apt-get install php-xdebug 2.找到扩展的路径: chq@chq- ...

  6. forever让nodejs后台运行

    nodejs一般是当成一条用户命令执行的,当用户断开客户连接,运用也就停了,很烦人.如何让nodejs应用当成服务,在后台执行呢? 最简单的办法: $ nohup node app.js 但是,for ...

  7. 使用springmvc的时候报错NoSuchBeanDefinitionException: No qualifying bean of type

    NoSuchBeanDefinitionException: No qualifying bean of type 其实我至今都不知道错误的根源在哪里,<context:component-sc ...

  8. python爬虫scrapy框架——爬取伯乐在线网站文章

    一.前言  1. scrapy依赖包: 二.创建工程 1. 创建scrapy工程: scrapy staratproject ArticleSpider 2. 开始(创建)新的爬虫: cd Artic ...

  9. 初试Orchard Core CMS

    关于Orchard Core CMS,这是一套内容管理系统(Content Management System),看一下来自官方文档的解释,什么是Orchard CMS. Orchard is a f ...

  10. 三十三 Python分布式爬虫打造搜索引擎Scrapy精讲—数据收集(Stats Collection)

    Scrapy提供了方便的收集数据的机制.数据以key/value方式存储,值大多是计数值. 该机制叫做数据收集器(Stats Collector),可以通过 Crawler API 的属性 stats ...