gpt4 book ai didi

amazon-web-services - 如何通过 ASG 使用 Terraform 获取 EC2 实例的私有(private) IP

转载 作者:行者123 更新时间:2023-12-03 19:31:07 26 4
gpt4 key购买 nike

我尝试使用以下代码从 ASG 获取 ip,它是使用 Terraform 创建的?这是一个好习惯还是坏习惯?但我得到了正确的输出,正如我所料。

data "aws_instances" "test" {
instance_tags {
Environment = "${var.environment}",
instance = "${var.db_instance_name}"
}

instance_state_names = ["running"]


depends_on = ["aws_sqs_queue.ansible", "aws_autoscaling_group.sample"]
}

输出.tf
output "privateips" {
value = "${data.aws_instances.test.private_ips}"
}

最佳答案

创建 ASG 时,在末尾添加一个本地配置程序,以执行使用 cli 与 AWS 交互的本地脚本,以便您可以查询 ASG IP:

resource "aws_autoscaling_group" "artifactory" {
name_prefix = "${var.env}-Application-ASG-"
vpc_zone_identifier = ["${var.app_subnets}"]
max_size = "${var.asg_max}"
min_size = "${var.asg_min}"
desired_capacity = "${var.asg_desired}"
force_delete = true
launch_configuration = "${aws_launch_configuration.application.name}"
target_group_arns = ["${aws_alb_target_group.application.arn}"]

provisioner "local-exec" {
command = "./getips.sh"
}
}

脚本:
ips=""
ids=""
while [ "$ids" = "" ]; do
ids=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names $ASG --region $REGION --query AutoScalingGroups[].Instances[].InstanceId --output text)
sleep 1
done
for ID in $ids;
do
IP=$(aws ec2 describe-instances --instance-ids $ID --region $REGION --query Reservations[].Instances[].PrivateIpAddress --output text)
ips="$ips,$IP"
done

关于amazon-web-services - 如何通过 ASG 使用 Terraform 获取 EC2 实例的私有(private) IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54369858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com