Terraform 语法 resource

resource是我们的资源,一般在terraform里面定义的都是资源,是个非常重要的角色。

ECS


 关键字resource+资源类型(比如dns,ecs,vpc,交换机)+资源的名称。模块下资源的名称要保证唯一,不要冲突。

块里面就是所有resource的配置,也就是它的主体部分。

对于资源我们只需要了解它的主体,它的结构,接下来就可以开始定义ecs。

阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台

首先你得知道在哪个区域创建ecs,创建的时候选择镜像,操作系统,实例的数量是几个,然后是系统盘多大(要不要加盘),cpu,内存这些规格大小(实例规格)。

接下来为你配置网络 

分配公网IP,带宽,安全组,弹性网卡,ipv6。

最后就是系统的配置,账号密码这些。实例名称,主机名称。

cloud-init其实就是每个云产品的机制,实例创建完之后想要执行一些任务,就可以借用这种机制去执行。里面可以写一段脚本。

下面就要去定义ecs资源了,resource是从provider里面来的。

第一个是可用区,第二个是安全组,之前创建过安全组,然后加了两条安全组的规则,80和22。

再往下面就是实例的类型,规格。

  • instance_type - (Required) The type of instance to start. When it is changed, the instance will reboot to make the change take effect.

再后面就是云盘。

  • system_disk_category - (Optional,ForceNew) Valid values are ephemeral_ssdcloud_efficiencycloud_ssdcloud_essdcloudcloud_auto. only is used to some none I/O optimized instance. Default to cloud_efficiency. Valid values cloud_auto Available in 1.184.0+.

镜像的id可以通过data source去拿到。

这个就是我资源的配置

  • internet_charge_type - (Optional) Internet charge type of the instance, Valid values are PayByBandwidthPayByTraffic. Default is PayByTraffic. At present, 'PrePaid' instance cannot change the value to "PayByBandwidth" from "PayByTraffic".

  • password - (Optional, Sensitive) Password to an instance is a string of 8 to 30 characters. It must contain uppercase/lowercase letters and numerals, but cannot contain special symbols. When it is changed, the instance will reboot to make the change take effect.

 ecs创建之后可能会去搭建一些服务,那么就需要通过ecs的公网IP,公网IP就可以访问这台服务器了(公网ip和ecs关联上,而不是在这里写上去)。

这个密码其实就是在配置文件里面写的密码(password= "root@123")记住这里需要创建22端口访问规则,在安全组里面。

 这就是我要创建的实例,然后创建ecs之后,会去上面搭建一些服务。

访问我们部署的服务。 

DNS


当你在上面部署了一台ecs,并且在ecs上面部署了你的web应用,那么就需要配置域名解析了。DNS是有zone区分的, 

 阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台

 这个域名其实就是我的zone,然后进去添加解析。

可以看到这个域名解析到这个IP了。

上面演示就是加了一条DNS的A记录,指向了我们ecs的IP。

Terraform Registry

dns_record = {
  "dev" = "dev"
}

variable "dns_record" {
  type = map(string)
}

# Create a new Domain record
resource "alicloud_dns_record" "record" {
  name        = "nginx.com"
  host_record = var.dns_record["dev"]
  type        = "A"
  value       = alicloud_instance.instance.public_ip
}

 type=A表示添加一条A记录,A记录一般指定的一台主机IP。name是zone的名称,然后是记录。

如果访问就是host_record.name就行了。value就是IP地址。

这里引用的是我们的公网IP,引用的是我们定义的ecs资源的公网IP。

value       = alicloud_instance.instance.public_ip

---------------------------------------------------------------------------------------------------------------------------------

Error: [ERROR] terraform-provider-alicloud/alicloud/resource_alicloud_instance.go:766: Resource alicloud_instance RunInstances Failed!!! [SDK alibaba-cloud-sdk-go ERROR]:
│ SDKError:
│    StatusCode: 400
│    Code: OperationDenied
│    Message: code: 400, The specified instanceType or zone is not available or not authorized. request id: F1637088-B1A8-534C-BA05-940ACC9A7571
│    Data: {"Code":"OperationDenied","HostId":"ecs-unit-share.cn-hangzhou.aliyuncs.com","Message":"The specified instanceType or zone is not available or not authorized.","Recommend":"https://next.api.aliyun.com/troubleshoot?q=OperationDenied
resource "alicloud_instance" "instance" {
  # cn-beijing
  availability_zone = "cn-hangzhou-b"
  security_groups   = alicloud_security_group.group.*.id

  # series III
  instance_type              = "ecs.n1.tiny"
  system_disk_category       = "cloud_efficiency"
  system_disk_name           = "test_foo_system_disk_name"
  system_disk_description    = "test_foo_system_disk_description"
  image_id                   = "ubuntu_18_04_64_20G_alibase_20190624.vhd"
  instance_name              = "terraform_test"
  vswitch_id                 = alicloud_vswitch.vsw.id
  internet_max_bandwidth_out = 10
  internet_charge_type       = "PayByTraffic"
  password                   = "root@123"
}
# Declare the data source
data "alicloud_instance_types" "types_ds" {
  availability_zone = "cn-hangzhou-b"
  cpu_core_count    = 1
  memory_size       = 1
}

data "alicloud_images" "images_ds" {
  owners       = "system"
  name_regex   = "^centos_7"
  os_type      = "linux"
  architecture = "x86_64"
}

  availability_zone 注意,这里一定要具体到某个区域,直接写cn-hangzhou会报错。

output "instance_types" {
  value = "${data.alicloud_instance_types.types_ds.instance_types.*.id}"
}

output "images_id" {
  value = "${data.alicloud_images.images_ds.images.*.id}"
}

猜你喜欢

转载自blog.csdn.net/qq_34556414/article/details/127533743