在AWS第1部分中使用Terraform自动缩放组:基本步骤

因此,您想使用Terraform在AWS上创建一个自动缩放组。 以下是达到此目的的最少步骤。

在编写实际代码之前,应指定aws terraform提供程序以及provider.tf文件上的区域。

 provider "aws" { 
   version = "~> 2.0" 
   region = "eu-west-1"  }  terraform { 
   required_version = "~>0.12.0"  } 

然后我们将

第一步是在variables.tf文件上定义一些变量。

 variable "vpc_id" { 
   type = string 
   default = "your-vpc-id"  }  variable "launch_configuration_name" { 
   type = string 
   default = "launch_configuration_name"  }  variable "auto_scalling_group_name" { 
   type = string 
   default = "auto_scalling_group_name"  }  variable "image_id" { 
   type = string 
   default = "image-id-based-on-the-region"  }  variable "instance_type" { 
   type = "string" 
   default = "t2.micro"  } 

然后,我们将在autoscalling_group.tf文件上具有自动缩放组配置。

 data "aws_subnet_ids" "subnets" { 
   vpc_id = var.vpc_id  }  data "aws_subnet" "subnet_values" { 
   for_each = data.aws_subnet_ids.subnets.ids 
   id = each.value  }  resource "aws_launch_configuration" "launch-configuration" { 
   name = var.launch_configuration_name 
   image_id = var.image_id 
   instance_type = var.instance_type  }  resource "aws_autoscaling_group" "autoscalling_group_config" { 
   name = var.auto_scalling_group_name 
   max_size = 3 
   min_size = 2 
   health_check_grace_period = 300 
   health_check_type = "EC2" 
   desired_capacity = 3 
   force_delete = true 
   vpc_zone_identifier = [ for s in data.aws_subnet.subnet_values: s. id ] 
   launch_configuration = aws_launch_configuration.launch-configuration.name 
   lifecycle { 
     create_before_destroy = true 
   }  } 

让我们分解一下。 需要vpc ID才能识别自动伸缩组使用的子网。 因此,值vpc_zone_identifier将从定义的vpc派生子网。

然后,您必须创建启动配置。 启动配置应指定基于您所在地区和实例类型的映像ID。

要执行此操作,前提是您拥有aws凭证,因此必须进行初始化,然后应用

 > terraform init  > terraform apply 

翻译自: https://www.javacodegeeks.com/2020/01/autoscaling-groups-with-terraform-on-aws-part-1-basic-steps.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值