Terraform学习日记-AWS-EC2

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"
    }

测试效果:拉起成功

明日继续 See you.

相关推荐
西岸行者4 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意5 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码5 天前
嵌入式学习路线
学习
毛小茛5 天前
计算机系统概论——校验码
学习
babe小鑫5 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms5 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下5 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。5 天前
2026.2.25监控学习
学习
im_AMBER5 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
CodeJourney_J5 天前
从“Hello World“ 开始 C++
c语言·c++·学习