当前位置:   article > 正文

Terraform学习日记-AWS-EC2_terraform --install-autocomplete

terraform --install-autocomplete

terraform install

https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli

这里我们使用 aws-linux-2022 作为执行环境

 # 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
  • 1
  • 2
  • 3
## 验证安装
# terraform -help

## 配置自动补全
# touch ~/.bashrc
# terraform -install-autocomplete
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

配置aws aksk 认证,保证有相关权限创建资源
1.aws configure方式

# aws configure
  • 1

2.配置aksk env方式

# export AWS_ACCESS_KEY_ID=
# export AWS_SECRET_ACCESS_KEY=
  • 1
  • 2

开始练习测试

## 每个项目必须有自己工作目录
# mkdir tfm-ec2
# cd tfm-ec2
# touch main.tf
  • 1
  • 2
  • 3
  • 4

粘贴我们ec2示例文件,酌情修改

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"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

初始化工作环境

##此过程会安装所需的组件,每次新的工作时,需要执行
# terraform init
  • 1
  • 2

格式化您的配置

Terraform 将打印出其修改的文件的名称(如果有)。在这种情况下,您的配置文件已正确格式化,因此 Terraform 将不会返回任何文件名

可以测试下,添加多余的空格,这行命令会帮你格式化

# terraform fmt 
main.tf
  • 1
  • 2

检测的你配置文件

这里我故意加一个字母测试

# 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"?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

修改后效果

# terraform validate
Success! The configuration is valid.

[root@ip-172-31-19-57 tfm-ec2]# terraform show
# aws_instance.app_server:

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

应用你配置文件

会让你输入yes ,确认创建动作

# 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.
  • 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

查看你的tfm应用详情

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


  • 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

测试效果:拉起成功
在这里插入图片描述

明日继续 See you.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/612099
推荐阅读
相关标签
  

闽ICP备14008679号