Hello everyone,
I'm provisioning ec2 instance under ASG and that's great but in order to modify/update other SG's I need to pull public/private ip addresses from these instances somehow.
Here's the code I'm using for asg module:
resource "aws_launch_configuration" "launch_config" {
name_prefix = "${var.lc_name}"
image_id = "${var.asg_ami_id}"
instance_type = "${var.asg_instance_type}"
security_groups = ["${var.asg_security_groups}"]
user_data = "${var.asg_user_data}"
associate_public_ip_address = "${var.associate_public_ip_address}"
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "main_asg" {
//We want this to explicitly depend on the launch config above
depends_on = ["aws_launch_configuration.launch_config"]
name = "${var.asg_name}"
// Split out the AZs string into an array
// The chosen availability zones *must* match
// the AZs the VPC subnets are tied to.
availability_zones = ["${split(",", var.asg_azs)}"]
// Split out the subnets string into an array
vpc_zone_identifier = ["${split(",", var.asg_subnets)}"]
// Uses the ID from the launch config created above
launch_configuration = "${aws_launch_configuration.launch_config.id}"
max_size = "${var.asg_number_of_instances}"
min_size = "${var.asg_minimum_number_of_instances}"
load_balancers = ["${var.load_balancers}"]
desired_capacity = "${var.asg_number_of_instances}"
health_check_grace_period = "${var.asg_health_check_grace_period}"
health_check_type = "${var.asg_health_check_type}"
tag {
key = "Name"
value = "${var.name}"
propagate_at_launch = true
}
}
Here's the code I have in my main TF file:
module "cache_under_asg" {
source = "git::https://path/to/repo/asg.git"
name = "${var.name}-EC2"
asg_name = "${var.name}-ASG"
lc_name = "${var.name}-LC"
asg_ami_id = "ami-fc8fda9c"
asg_instance_type = "m3.medium"
asg_security_groups = ["${module.cache_sg.cache_sg_id}"]
asg_number_of_instances = 1
asg_minimum_number_of_instances = 1
asg_subnets = "${module.private_subnet.subnet_ids}"
asg_azs = "${lookup(var.azs, var.region)}"
load_balancers = ["${module.cache_elb_http.elb_name}"]
associate_public_ip_address = "true"
}
To get private/public ip's from ec2 instances (NOT under ASG) in ec2 module I used this piece:
output "private_ip" { value = "${aws_instance.ec2_instance.private_ip}" }
output "public_ip" { value = "${aws_instance.ec2_instance.public_ip}" }
that enables me to pass them to different modules (like different SG's) but it seems like there's no alternative under ASG.
Is that even possible or maybe there's other workaround to make it happen?
Any advice really appreciated.
Thank you,
E.G.