Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this_placement_group change to allow 0 instances. #109

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions examples/zero/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Basic EC2 instance

Configuration in this directory creates EC2 instances with minimum set of arguments. It will also assign Elastic IP (EIP) to an instance.

Unspecified arguments for security group id and subnet are inherited from the default VPC.

This example outputs instance id and public DNS name as a single value and as a list.

## Usage

To run this example you need to execute:

```bash
$ terraform init
$ terraform plan
$ terraform apply
```

Note that this example may create resources which can cost money. Run `terraform destroy` when you don't need these resources.

105 changes: 105 additions & 0 deletions examples/zero/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
provider "aws" {
region = "eu-west-1"
}

variable "instance_type" {
default="t3.large"
description="instance type to create / not create"
}

variable "instance_count" {
default=0
description="number of instances to create. test with 0 or 1"
}

variable "suppress_empty_list" {
default=true
description="suppress output when 0 instances"
}

##################################################################
# Data sources to get VPC, subnet, security group and AMI details
##################################################################
data "aws_vpc" "default" {
default = true
}

data "aws_subnet_ids" "all" {
vpc_id = data.aws_vpc.default.id
}

data "aws_ami" "amazon_linux" {
most_recent = true

owners = ["amazon"]

filter {
name = "name"

values = [
"amzn-ami-hvm-*-x86_64-gp2",
]
}

filter {
name = "owner-alias"

values = [
"amazon",
]
}
}

module "security_group" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 3.0"

name = "example"
description = "Security group for example usage with EC2 instance"
vpc_id = data.aws_vpc.default.id

ingress_cidr_blocks = ["0.0.0.0/0"]
ingress_rules = ["http-80-tcp", "all-icmp"]
egress_rules = ["all-all"]
}

resource "aws_eip" "this" {
count = var.instance_count > 0 ? 1 : 0
vpc = true
instance = module.ec2.id[0]
}

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

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

instance_count = var.instance_count

name = "example-normal"
ami = data.aws_ami.amazon_linux.id
instance_type = var.instance_type
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 = [
{
volume_type = "gp2"
volume_size = 10
},
]

tags = {
"Env" = "Private"
"Location" = "Secret"
}
}




45 changes: 45 additions & 0 deletions examples/zero/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
locals {
empty = var.suppress_empty_list && ( length(module.ec2.id) == 0 )
}

output "ids" {
description = "List of IDs of instances"
value = local.empty ? null : module.ec2.id
}

output "public_dns" {
description = "List of public DNS names assigned to the instances"
value = local.empty ? null : module.ec2.public_dns
}

output "vpc_security_group_ids" {
description = "List of VPC security group ids assigned to the instances"
value = local.empty ? null : module.ec2.vpc_security_group_ids
}

output "tags" {
description = "List of tags"
value = local.empty ? null : module.ec2.tags
}

output "placement_group" {
description = "List of placement group"
value = local.empty ? null : module.ec2.placement_group
}

output "instance_id" {
description = "EC2 instance ID"
value = local.empty ? null : module.ec2.id
}


output "instance_public_dns" {
description = "Public DNS name assigned to the EC2 instance"
value = local.empty ? null : module.ec2.id
}

output "credit_specification" {
description = "Credit specification of EC2 instance (empty list for not t2 instance types)"
value = local.empty ? null : module.ec2.credit_specification
}

2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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_placement_group = compact(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, [""]))
Expand Down