首先,我们要先安装Terraform,
Terraform的安装很简单,以Mac 为例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
$brew install terraform $terraform --help 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. Common commands: apply Builds or changes infrastructure console Interactive console for Terraform interpolations destroy Destroy Terraform-managed infrastructure env Workspace management fmt Rewrites config files to canonical format get Download and install modules for the configuration graph Create a visual graph of Terraform resources import Import existing infrastructure into Terraform init Initialize a Terraform working directory output Read an output from a state file plan Generate and show an execution plan providers Prints a tree of the providers used in the configuration refresh Update local state file against real resources show Inspect Terraform state or plan taint Manually mark a resource for recreation untaint Manually unmark a resource as tainted validate Validates the Terraform files version Prints the Terraform version workspace Workspace management All other commands: 0.12upgrade Rewrites pre-0.12 module source code for v0.12 debug Debug output management (experimental) force-unlock Manually unlock the terraform state push Obsolete command for Terraform Enterprise legacy (v1) state Advanced state management |
安装完成后我们来使用Terraform在aws中创建一个instance
先看代码 example.tf:
1 2 3 4 5 6 7 8 9 |
provider "aws" { profile = "default" region = "us-east-1" } resource "aws_instance" "example" { ami = "ami-2757f631" instance_type = "t2.micro" } |
注意:例子中的ami 是us-east-1 region的,如果region发生改变,ami需要修改
其实代码真的是通俗易懂
provider 部分定义了你是使用那个云,上边的例子中使用的是aws,
resource部分定义的是具体的哪一个资源,因为aws上有很多资源,例如ec2,ecs,eks,elb,因为我们需要创建instance所以,资源部分我们需要说明,我们要创建的aws资源的类型是aws_instance 然后我们给这个instance的名字叫:example
所有的代码的意思是:
在aws的us-east-1 region中,使用ami-2757f631 创建一个t2.micro类型的ec2主机
但是,这样我们并没有办法真正的创建主机,因为我们没有定义我们的aws的账号,也就是代码不知道也没有权限去aws创建任何东西,那么我们该如何设置我们的aws的认证信息呢?
我们可以将我们的账号信息存储到: ~/.aws/credentials 文件,这就是aws默认的存放认证信息的地方
账号也设置好了,我们来看一下如何创建主机,在创建之前,我们需要初始化一下,让Terraform来下载必要的依赖包,只有下载好了所有的依赖包,代码才能正常运行(本例子中主要是下载aws相关的依赖包)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
$ terraform init Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "aws" (terraform-providers/aws) 2.10.0... 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.aws: version = "~> 2.10" 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. |
初始化完成后,我们想先模拟运行一下,因为很多时候我们想确认脚本是否将要做的修改是否真的是我们想要的,如果是,我们继续,如果不是,那我们就停止,不会造成任何伤害,但是,如何执行呢?
1 |
terraform plan |
plan这条命令不会做任何实质性操作,但是会展示给你即将发生的改变
那我们plan完成后确认是我们想要的修改后,我们该如何真正做出修改呢?使用terraform apply
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
$ 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: # aws_instance.example will be created + resource "aws_instance" "example" { + ami = "ami-2757f631" + arn = (known after apply) + associate_public_ip_address = (known after apply) + availability_zone = (known after apply) + cpu_core_count = (known after apply) + cpu_threads_per_core = (known after apply) + get_password_data = false + host_id = (known after apply) + id = (known after apply) + instance_state = (known after apply) + instance_type = "t2.micro" + ipv6_address_count = (known after apply) + ipv6_addresses = (known after apply) + key_name = (known after apply) + network_interface_id = (known after apply) + password_data = (known after apply) + placement_group = (known after apply) + primary_network_interface_id = (known after apply) + private_dns = (known after apply) + private_ip = (known after apply) + public_dns = (known after apply) + public_ip = (known after apply) + security_groups = (known after apply) + source_dest_check = true + subnet_id = (known after apply) + tenancy = (known after apply) + volume_tags = (known after apply) + vpc_security_group_ids = (known after apply) + ebs_block_device { + delete_on_termination = (known after apply) + device_name = (known after apply) + encrypted = (known after apply) + iops = (known after apply) + snapshot_id = (known after apply) + volume_id = (known after apply) + volume_size = (known after apply) + volume_type = (known after apply) } + ephemeral_block_device { + device_name = (known after apply) + no_device = (known after apply) + virtual_name = (known after apply) } + network_interface { + delete_on_termination = (known after apply) + device_index = (known after apply) + network_interface_id = (known after apply) } + root_block_device { + delete_on_termination = (known after apply) + iops = (known after apply) + volume_id = (known after apply) + volume_size = (known after apply) + volume_type = (known after apply) } } Plan: 1 to add, 0 to change, 0 to destroy. |
其实这个时候也不会做出修改,Terraform会让你再次确认,并输入yes后才会真正执行,要不怎么说安全呢
输入yes后
1 2 3 4 5 |
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 1m50s [id=i-0bbf06244e44211d1] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. |
主机顺利创建了,我们可以到aws的页面确认主机是否真的创建了
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021
本文共 1 个回复
Comments are closed.