Last active
June 5, 2020 15:38
-
-
Save akaron/6016a8ed600d7109341e893be639c450 to your computer and use it in GitHub Desktop.
terraform aws_autoscaling_group get private and public ip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # In terraform, the attribute of autoscaling group does not contain detail information of EC2 instances, | |
| # i.e., cannot get the ip of instances for further usage (such as ansible). Of course it's better to make | |
| # it no need to know the ip by using tools such as user-data of EC2 instance (for bootstrap) and LB and | |
| # route 53. | |
| # | |
| # In case still want to know the ip addresses, one can use `data "aws_instances"` which | |
| # `depends_on = [aws_autoscaling_group.name]`, then use tags to get the information of instances. | |
| # ref: https://github.com/terraform-providers/terraform-provider-aws/issues/511#issuecomment-447934405 | |
| provider "aws" { | |
| profile = "default" | |
| region = "us-east-2" | |
| } | |
| resource "aws_key_pair" "quickstart_key_pair" { | |
| key_name_prefix = "asgtest-" | |
| public_key = file("/vagrant/myRancher.pub") | |
| } | |
| resource "aws_launch_configuration" "asgtest" { | |
| associate_public_ip_address = true | |
| enable_monitoring = false | |
| # iam_instance_profile = aws_iam_instance_profile.masters-k8s-optract-space.id | |
| image_id = "ami-075dbb5513cc94405" | |
| instance_type = "t3a.micro" | |
| key_name = aws_key_pair.quickstart_key_pair.id | |
| lifecycle { | |
| create_before_destroy = true | |
| } | |
| name_prefix = "asgtest-us-east-2" | |
| root_block_device { | |
| delete_on_termination = true | |
| volume_size = 12 | |
| volume_type = "gp2" | |
| } | |
| } | |
| resource "aws_autoscaling_group" "asgtest" { | |
| enabled_metrics = ["GroupDesiredCapacity", "GroupInServiceInstances", "GroupMaxSize", "GroupMinSize", "GroupPendingInstances", "GroupStandbyInstances", "GroupTerminatingInstances", "GroupTotalInstances"] | |
| launch_configuration = aws_launch_configuration.asgtest.id | |
| max_size = 2 | |
| metrics_granularity = "1Minute" | |
| min_size = 2 | |
| name = "asgtest" | |
| vpc_zone_identifier = ["subnet-0414555921a4bff87"] | |
| tag { | |
| key = "role/master" | |
| propagate_at_launch = true | |
| value = "1" | |
| } | |
| } | |
| locals { | |
| mastertag_key = "role/master" | |
| mastertag = map("role/master", "1") | |
| } | |
| data "aws_instances" "workers" { | |
| depends_on = [aws_autoscaling_group.asgtest] | |
| instance_tags = local.mastertag | |
| } | |
| output "private-ips" { | |
| value = "${data.aws_instances.workers.private_ips}" | |
| } | |
| output "public-ips" { | |
| value = "${data.aws_instances.workers.public_ips}" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment