Skip to content

Commit

Permalink
Added support for placement group and volume tags (terraform-aws-modu…
Browse files Browse the repository at this point in the history
  • Loading branch information
antonbabenko authored Jun 8, 2019
1 parent af3015d commit 3d9df6f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ data "aws_ami" "ubuntu-xenial" {
| credit\_specification | List of credit specification of instances |
| id | List of IDs of instances |
| key\_name | List of key names of instances |
| placement\_group | List of placement groups of instances |
| primary\_network\_interface\_id | List of IDs of the primary network interface of instances |
| private\_dns | List of private DNS names assigned to the instances. Can only be used inside the Amazon EC2, and only available if you've enabled DNS hostnames for your VPC |
| private\_ip | List of private IP addresses assigned to the instances |
Expand All @@ -143,6 +144,7 @@ data "aws_ami" "ubuntu-xenial" {
| security\_groups | List of associated security groups of instances |
| subnet\_id | List of IDs of VPC subnets of instances |
| tags | List of tags of instances |
| volume\_tags | List of tags of volumes of instances |
| vpc\_security\_group\_ids | List of associated security groups of instances, if running in non-default VPC |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Expand Down
6 changes: 6 additions & 0 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ resource "aws_eip" "this" {
instance = module.ec2.id[0]
}

resource "aws_placement_group" "web" {
name = "hunky-dory-pg"
strategy = "cluster"
}

module "ec2" {
source = "../../"

Expand All @@ -64,6 +69,7 @@ module "ec2" {
subnet_id = tolist(data.aws_subnet_ids.all.ids)[0]
vpc_security_group_ids = [module.security_group.this_security_group_id]
associate_public_ip_address = true
placement_group = aws_placement_group.web.id

root_block_device = [
{
Expand Down
5 changes: 5 additions & 0 deletions examples/basic/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ output "tags" {
value = module.ec2.tags
}

output "placement_group" {
description = "List of placement group"
value = module.ec2.placement_group
}

output "instance_id" {
description = "EC2 instance ID"
value = module.ec2.id[0]
Expand Down
16 changes: 14 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ resource "aws_instance" "this" {
ipv6_addresses = var.ipv6_addresses

ebs_optimized = var.ebs_optimized
volume_tags = var.volume_tags

dynamic "root_block_device" {
for_each = var.root_block_device
Expand Down Expand Up @@ -76,6 +75,13 @@ resource "aws_instance" "this" {
var.tags,
)

volume_tags = merge(
{
"Name" = var.instance_count > 1 || var.use_num_suffix ? format("%s-%d", var.name, count.index + 1) : var.name
},
var.volume_tags,
)

lifecycle {
# Due to several known issues in Terraform AWS provider related to arguments of aws_instance:
# (eg, https://github.com/terraform-providers/terraform-provider-aws/issues/2036)
Expand Down Expand Up @@ -109,7 +115,6 @@ resource "aws_instance" "this_t2" {
ipv6_addresses = var.ipv6_addresses

ebs_optimized = var.ebs_optimized
volume_tags = var.volume_tags

dynamic "root_block_device" {
for_each = var.root_block_device
Expand Down Expand Up @@ -160,6 +165,13 @@ resource "aws_instance" "this_t2" {
var.tags,
)

volume_tags = merge(
{
"Name" = var.instance_count > 1 || var.use_num_suffix ? format("%s-%d", var.name, count.index + 1) : var.name
},
var.volume_tags,
)

lifecycle {
# Due to several known issues in Terraform AWS provider related to arguments of aws_instance:
# (eg, https://github.com/terraform-providers/terraform-provider-aws/issues/2036)
Expand Down
17 changes: 11 additions & 6 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ locals {
this_primary_network_interface_id = compact(coalescelist(aws_instance.this.*.primary_network_interface_id, aws_instance.this_t2.*.primary_network_interface_id, [""]))
this_private_dns = compact(coalescelist(aws_instance.this.*.private_dns, aws_instance.this_t2.*.private_dns, [""]))
this_private_ip = compact(coalescelist(aws_instance.this.*.private_ip, aws_instance.this_t2.*.private_ip, [""]))
this_placement_group = compact(concat(coalescelist(aws_instance.this.*.placement_group, aws_instance.this_t2.*.placement_group), [""]))
this_security_groups = coalescelist(aws_instance.this.*.security_groups, aws_instance.this_t2.*.security_groups, [""])
this_vpc_security_group_ids = coalescelist(flatten(aws_instance.this.*.vpc_security_group_ids), flatten(aws_instance.this_t2.*.vpc_security_group_ids), [""])
this_subnet_id = compact(coalescelist(aws_instance.this.*.subnet_id, aws_instance.this_t2.*.subnet_id, [""]))
this_credit_specification = aws_instance.this_t2.*.credit_specification
this_credit_specification = flatten(aws_instance.this_t2.*.credit_specification)
this_tags = coalescelist(aws_instance.this.*.tags, aws_instance.this_t2.*.tags, [""])
this_volume_tags = coalescelist(aws_instance.this.*.volume_tags, aws_instance.this_t2.*.volume_tags, [""])
}

output "id" {
Expand All @@ -24,11 +26,10 @@ output "availability_zone" {
value = local.this_availability_zone
}

// GH issue: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/issues/8
//output "placement_group" {
// description = "List of placement groups of instances"
// value = ["${element(concat(aws_instance.this.*.placement_group, list("")), 0)}"]
//}
output "placement_group" {
description = "List of placement groups of instances"
value = local.this_placement_group
}

output "key_name" {
description = "List of key names of instances"
Expand Down Expand Up @@ -85,3 +86,7 @@ output "tags" {
value = local.this_tags
}

output "volume_tags" {
description = "List of tags of volumes of instances"
value = local.this_volume_tags
}

0 comments on commit 3d9df6f

Please sign in to comment.