terraform install
https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli
这里我们使用 aws-linux-2022 作为执行环境
bash
# sudo yum install -y yum-utils
# sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
# sudo yum -y install terraform
bash
## 验证安装
# terraform -help
## 配置自动补全
# touch ~/.bashrc
# terraform -install-autocomplete
配置aws aksk 认证,保证有相关权限创建资源
1.aws configure方式
bash
# aws configure
2.配置aksk env方式
bash
# export AWS_ACCESS_KEY_ID=
# export AWS_SECRET_ACCESS_KEY=
开始练习测试
bash
## 每个项目必须有自己工作目录
# mkdir tfm-ec2
# cd tfm-ec2
# touch main.tf
粘贴我们ec2示例文件,酌情修改
bash
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "app_server" {
ami = "ami-830c94e3"
instance_type = "t2.micro"
tags = {
Name = "ExampleAppServerInstance"
}
}
初始化工作环境
bash
##此过程会安装所需的组件,每次新的工作时,需要执行
# terraform init
格式化您的配置
Terraform 将打印出其修改的文件的名称(如果有)。在这种情况下,您的配置文件已正确格式化,因此 Terraform 将不会返回任何文件名
可以测试下,添加多余的空格,这行命令会帮你格式化
bash
# terraform fmt
main.tf
检测的你配置文件
这里我故意加一个字母测试
bash
# terraform validate
╷
│ Error: Unsupported block type
│
│ on main.tf line 16:
│ 16: aresource "aws_instance" "app_server" {
│
│ Blocks of type "aresource" are not expected here. Did you mean "resource"?
修改后效果
bash
# terraform validate
Success! The configuration is valid.
[root@ip-172-31-19-57 tfm-ec2]# terraform show
# aws_instance.app_server:
应用你配置文件
会让你输入yes ,确认创建动作
bash
# terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_instance.app_server will be created
+ resource "aws_instance" "app_server" {
+ ami = "ami-00d785f1c099d5a0e"
+ 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)
+ disable_api_stop = (known after apply)
+ disable_api_termination = (known after apply)
+ ebs_optimized = (known after apply)
+ get_password_data = false
+ host_id = (known after apply)
+ host_resource_group_arn = (known after apply)
+ iam_instance_profile = (known after apply)
+ id = (known after apply)
+ instance_initiated_shutdown_behavior = (known after apply)
+ instance_state = (known after apply)
+ instance_type = "t3.micro"
+ ipv6_address_count = (known after apply)
+ ipv6_addresses = (known after apply)
+ key_name = (known after apply)
+ monitoring = (known after apply)
+ outpost_arn = (known after apply)
+ password_data = (known after apply)
+ placement_group = (known after apply)
+ placement_partition_number = (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)
+ secondary_private_ips = (known after apply)
+ security_groups = (known after apply)
+ source_dest_check = true
+ subnet_id = (known after apply)
+ tags = {
+ "Name" = "Tfm-ht01"
}
+ tags_all = {
+ "Name" = "Tfm-ht01"
}
+ tenancy = (known after apply)
+ user_data = (known after apply)
+ user_data_base64 = (known after apply)
+ user_data_replace_on_change = false
+ vpc_security_group_ids = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
### 这里输入yes
Enter a value: yes
aws_instance.app_server: Creating...
aws_instance.app_server: Still creating... [10s elapsed]
aws_instance.app_server: Creation complete after 15s [id=i-00d64aa676c0562fa]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
查看你的tfm应用详情
bash
# terraform show
# aws_instance.app_server:
resource "aws_instance" "app_server" {
ami = "ami-00d785f1c099d5a0e"
arn = "arn:aws:ec2:ap-southeast-1:904115123825:instance/i-00d64aa676c0562fa"
associate_public_ip_address = true
availability_zone = "ap-southeast-1c"
cpu_core_count = 1
cpu_threads_per_core = 2
disable_api_stop = false
disable_api_termination = false
ebs_optimized = false
get_password_data = false
hibernation = false
id = "i-00d64aa676c0562fa"
instance_initiated_shutdown_behavior = "stop"
instance_state = "running"
instance_type = "t3.micro"
ipv6_address_count = 0
ipv6_addresses = []
monitoring = false
placement_partition_number = 0
primary_network_interface_id = "eni-083cf204ed320a037"
private_dns = "ip-172-31-5-27.ap-southeast-1.compute.internal"
private_ip = "172.31.5.27"
public_dns = "ec2-13-214-156-190.ap-southeast-1.compute.amazonaws.com"
public_ip = "13.214.156.190"
secondary_private_ips = []
security_groups = [
"default",
]
source_dest_check = true
subnet_id = "subnet-055c3f7a2a4a5bb44"
tags = {
"Name" = "Tfm-ht01"
}
tags_all = {
"Name" = "Tfm-ht01"
}
tenancy = "default"
user_data_replace_on_change = false
vpc_security_group_ids = [
"sg-0ae3ab2687d1ea1c2",
]
capacity_reservation_specification {
capacity_reservation_preference = "open"
}
测试效果:拉起成功