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.

相关推荐
HC1825808583213 分钟前
“倒时差”用英语怎么说?生活英语口语学习柯桥外语培训
学习·生活
学习路上_write17 分钟前
FPGA/Verilog,Quartus环境下if-else语句和case语句RT视图对比/学习记录
单片机·嵌入式硬件·qt·学习·fpga开发·github·硬件工程
非概念23 分钟前
stm32学习笔记----51单片机和stm32单片机的区别
笔记·stm32·单片机·学习·51单片机
无敌最俊朗@2 小时前
stm32学习之路——八种GPIO口工作模式
c语言·stm32·单片机·学习
EterNity_TiMe_2 小时前
【论文复现】STM32设计的物联网智能鱼缸
stm32·单片机·嵌入式硬件·物联网·学习·性能优化
L_cl3 小时前
Python学习从0到1 day28 Python 高阶技巧 ⑤ 多线程
学习
前端SkyRain3 小时前
后端Node学习项目-用户管理-增删改查
后端·学习·node.js
提笔惊蚂蚁3 小时前
结构化(经典)软件开发方法: 需求分析阶段+设计阶段
后端·学习·需求分析
DDDiccc3 小时前
JAVA学习日记(十五) 数据结构
数据结构·学习
腾科张老师4 小时前
为什么要使用Ansible实现Linux管理自动化?
linux·网络·学习·自动化·ansible