# 使用Terraform在阿里云创建ECS实例,并通过user_data传递初始化脚本
 
# 配置阿里云访问凭证
provider "alicloud" {
  access_key = "your-access-key"
  secret_key = "your-secret-key"
  region     = "cn-beijing"
}
 
# 创建ECS实例
resource "alicloud_instance" "ecs_instance" {
  image_id           = "your-image-id"
  instance_type      = "ecs.n1.small"
  security_groups    = ["your-security-group-id"]
  vswitch_id         = "your-vswitch-id"
  user_data          = "#!/bin/bash\n/usr/bin/yum install -y nginx\n/usr/sbin/nginx"
}

这段代码演示了如何使用Terraform在阿里云创建一个ECS实例,并通过user_data传递一个简单的初始化脚本来安装和启动nginx服务器。在实际使用时,需要替换your-access-keyyour-secret-keyyour-image-idyour-security-group-id, 和 your-vswitch-id为您自己的阿里云访问凭证和资源配置信息。

04-09 06:54