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.

相关推荐
weixin_307779131 小时前
Azure上基于OpenAI GPT-4模型验证行政区域数据的设计方案
数据仓库·python·云计算·aws
CSDN_PBB3 小时前
[STM32 - 野火] - - - 固件库学习笔记 - - - 十五.设置FLASH的读写保护及解除
笔记·stm32·学习
鸡啄米的时光机6 小时前
vscode的一些实用操作
vscode·学习
Kai HVZ7 小时前
《深度学习》——调整学习率和保存使用最优模型
人工智能·深度学习·学习
守护者1708 小时前
JAVA学习-练习试用Java实现“使用Apache Ignite对大数据进行内存计算和快速筛查”
java·学习
weixin_502539859 小时前
rust学习笔记2-rust的包管理工具Cargo使用
笔记·学习·rust
weixin_3077791310 小时前
AWS上Amazon Redshift用Zoominfo API验证公司基本信息数据正确性检查设计方案
数据仓库·python·云计算·aws
web_1553427465612 小时前
【合集】Java进阶——Java深入学习的笔记汇总 & 再论面向对象、数据结构和算法、JVM底层、多线程、类加载、
java·笔记·学习
伊一大数据&人工智能学习日志12 小时前
深度学习04 数据增强、调整学习率
人工智能·深度学习·学习·机器学习
执念斩长河13 小时前
vue学习笔记
vue.js·笔记·学习