安装terraform

下载terraform最新版本:

wget https://releases.hashicorp.com/terraform/0.11.5/terraform_0.11.5_linux_amd64.zip

terraform是一个二进制文件,将其放入环境变量目录中即可:

unzip terraform_0.11.5_linux_amd64.zip
cp terraform /usr/local/bin

想验证安装成功?执行terraform命令即可:

[root@ip-172-31-42-166 data]# terraform
Usage: terraform [--version] [--help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

命令执行成功,安装正确。

设置aws凭证

terraform支持多种云平台,我们这里使用aws为例演示,terraform会远程调用aws的API进行工作,我们通过环境变量方式设置其凭证:

export AWS_ACCESS_KEY_ID=(your access key id)
export AWS_SECRET_ACCESS_KEY=(your secret access key)

把相应信息替换为你自己的即可。

注意:咱们这样设置的环境变量是临时生效的,你切换了shell,或者重启电脑就得重新设置了,当然也可以使用其它的方式来做凭证:

你把认证信息写在$ HOME / .aws / credentials文件中也是可以的,细节请自己查阅aws官方文档(Command Line Interface)。

下面我们就可以做些什么了,比如启台服务器玩玩。

启动一台服务器

terraform的主要操作就是写配置文件,它的配置文件以 .tf结尾,并且有它自己的专用语言来书写(HCL),并不难。

配置文件写多了难免乱,我们应该有合适的文本编辑器,很多主流的工具支持它的专用语言语法,你比如说sublime。

好了我们来书写第一个.tf文件吧:

mkdir terraform_workspace
cd terraform_workspace/
touch main.tf

向main.tf中写入文件:

provider "aws" {
  region = "us-west-2"
}

上面代码意思就是我们使用的是aws云服务,然后要在us-west-2区进行操作。

再来一段:

resource "aws_instance" "example" {
  ami           = "ami-0031f978"
  instance_type = "t2.micro"
}

上面这段意思就是用什么类型的实例,基于什么镜像。

如果熟悉aws,上面这些术语就没毛问题了。

行了,代码写完了,咱进工作区执行个命令试试好使不:

cd terraform_workspace/
terraform init
terraform plan

[root@ip-172-31-42-166 terraform_workspace]# terraform plan
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:

  + aws_instance.example
      id:                           <computed>
      ami:                          "ami-0031f978"
      associate_public_ip_address:  <computed>
      availability_zone:            <computed>
      ebs_block_device.#:           <computed>
      ephemeral_block_device.#:     <computed>
      get_password_data:            "false"
      instance_state:               <computed>
      instance_type:                "t2.micro"
      ipv6_address_count:           <computed>
      ipv6_addresses.#:             <computed>
      key_name:                     <computed>
      network_interface.#:          <computed>
      network_interface_id:         <computed>
      password_data:                <computed>
      placement_group:              <computed>
      primary_network_interface_id: <computed>
      private_dns:                  <computed>
      private_ip:                   <computed>
      public_dns:                   <computed>
      public_ip:                    <computed>
      root_block_device.#:          <computed>
      security_groups.#:            <computed>
      source_dest_check:            "true"
      subnet_id:                    <computed>
      tenancy:                      <computed>
      volume_tags.%:                <computed>
      vpc_security_group_ids.#:     <computed>

Plan: 1 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.

terraform plan的意思就是提前检查一下你的配置文件要干啥,但是并不真正去执行。

执行terraform apply就可以真正去执行了,执行以后上控制台看看,主机已经启动了。

如果我们想更改刚才已经建立的实例配置怎么办?直接修改配置文件,重新执行即可,terraform是默认有记忆功能的奥。

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0031f978"
  instance_type = "t2.micro"
  tags {
    Name = "terraform-example"
  }
}

我们给配置文件里加个tags选项,再执行terraform apply。

看,给我们的机器加了一个名字

先这样吧,后续更新更深入的。

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

  1. 如何使用 Docker、ECS、Terraform 重建基础架构?

    早期 Segment 基础架构普遍组合在一起.我们通过 AWS 界面设定实例,使用许多闲散的 AMI,并且采用三种不同的部署方式. 然而随着商业的飞速发展,工程师团队的规模不断扩大,基础架构的复杂度也 ...

  2. 关于terraform的状态管理

    我们想在aws创建3台主机,使用ansible和terraform都是可以实现的. 用ansible可能是这样子的: - ec2: count: 10 image: ami-40d281120 ins ...

  3. ansible+packer+terraform在aws上布署web服务器

    各工具所扮演的角色 ansible: 配合packer生成安装有apache的基础镜像 packer: 生成amazon AMI terraform: 以packer生成的镜像为基础,布署web服务器 ...

  4. terraform 配置github module source

      terraform 支持多种module 的source 配置 以下是一个简单的使用github source的demo 测试项目 项目结构 ├── init.tpl ├── main.tf 代码 ...

  5. Terraform:创建 Azure 虚机

    笔者在前文<Terraform 简介>中简单介绍了 Terraform 相关的概念,本文让我们使用 Terraform 在 Azure 上创建一个虚机,以此来直观体验一下 Terrafor ...

  6. Terraform:简介

    在 DevOps 实践中,基础设施即代码如何落地是一个绕不开的话题.像 Chef,Puppet 等成熟的配置管理工具,都能够满足一定程度的需求,但有没有更友好的工具能够满足我们绝大多数的需求?笔者认为 ...

  7. 创建一个简单的terraform module

      terraform module可以实现代码的复用,同时方便分享,下面创建一个简单的基于localfile && template provider 的module module ...

  8. Writing and playing with custom Terraform Providers

    转自:https://petersouter.xyz/writing-and-playing-with-custom-terraform-providers/ I’ve been digging de ...

  9. Write your own Terraform provider: Part 1

    转自:https://container-solutions.com/write-terraform-provider-part-1/ This is the first part of a seri ...

随机推荐

  1. 学习java第一章

    本人是一名5年工作的人了,出来社会也比较早,工作经验比起刚刚出社会的大学生要和很多了,知道社会的现实与无奈,我为什么选择想学java昵,肯定受到了朋友的影响的,接下来就讲讲我学习java的过程. 1. ...

  2. Vue项目环境搭建(node+webpack)

    安装node.js 下载地址:https://nodejs.org/en/download/ node -v //查看node.js版本 项目环境配置: 安装vue-cli:npm install - ...

  3. [poj1094]Sorting It All Out_拓扑排序

    Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...

  4. 实现Windows数据绑定

    dataSet数据集   dataset驻留于内存临时存储数据简单的理解为一个临时数据库将数据源的数据保存在内存中独立于任何数据库创建dataset对象引入命名空间:system.Datadatase ...

  5. git解决修改代码后无法push的问题failed to push some refs to 'ssh://git@xxx.xxx.xx/xx.git'

    今天在使用git提交代码的时候,犯了个很低级的错误,按照一切流程当我add并commit提交代码,最后使用push到远程仓库, 接下来奇怪的事情发生了,push之后,查看远程仓库代码并没有发现提交记录 ...

  6. [BZOJ 1079][SCOI 2008]着色方案

    1079: [SCOI2008]着色方案 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2237  Solved: 1361[Submit][Stat ...

  7. Spark Job的提交与task本地化分析(源码阅读)

    Spark中任务的处理也要考虑数据的本地性(locality),Spark目前支持PROCESS_LOCAL(本地进程).NODE_LOCAL(本地节点).NODE_PREF.RACK_LOCAL(本 ...

  8. Mysql的内连接,外链接,交叉链接

    内连接:只连接匹配的行  inner join select A.*,B.* from A,B where A.id = B.parent_id 外链接包括左外链接,右外链接,全外链接 左外链接:包含 ...

  9. C语言程序设计(基础)- 第3周作业

    一.PTA编程题目 完成PTA第三周作业中4个题目: 1.7-9 A乘以B 要求:输入的两个整数:A是你学号前两位数字,B是你学号后两位数字 2.7-10 求整数均值 要求:输入的整数是:你的身高.体 ...

  10. Linux kernel 的 sendfile 是如何提高性能的

    Linux kernel 的 sendfile 是如何提高性能的 现在流行的 web 服务器里面都提供 sendfile 选项用来提高服务器性能,那到底 sendfile 是什么,怎么影响性能的呢? ...