Skip to content

Commit 2aa1cf3

Browse files
author
Steven Nemetz
committed
Add example for replacing an existing module
1 parent 1a2ddae commit 2aa1cf3

File tree

4 files changed

+220
-0
lines changed

4 files changed

+220
-0
lines changed

examples/wrapper/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Auto Scaling Group wrapper sample
2+
======================================
3+
4+
This is a sample setup for replacing an existing module
5+
6+
Configuration in this directory creates Launch Configuration and Auto Scaling Group.
7+
8+
Data sources are used to discover existing VPC resources (VPC, subnet and security group) as well as AMI details.
9+
10+
Usage
11+
=====
12+
13+
To run this example you need to execute:
14+
15+
```bash
16+
$ terraform init
17+
$ terraform plan
18+
$ terraform apply
19+
```
20+
21+
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.

examples/wrapper/main.tf

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
provider "aws" {
2+
region = "${var.region}"
3+
4+
# Make it faster by skipping something
5+
skip_get_ec2_platforms = true
6+
skip_metadata_api_check = true
7+
skip_region_validation = true
8+
skip_credentials_validation = true
9+
skip_requesting_account_id = true
10+
}
11+
12+
##############################################################
13+
# Data sources to get VPC, subnets and security group details
14+
##############################################################
15+
data "aws_vpc" "vpc" {
16+
tags {
17+
Env = "${var.environment}"
18+
}
19+
}
20+
21+
data "aws_subnet_ids" "all" {
22+
vpc_id = "${data.aws_vpc.vpc.id}"
23+
}
24+
25+
data "aws_security_group" "default" {
26+
vpc_id = "${data.aws_vpc.vpc.id}"
27+
name = "default"
28+
}
29+
30+
data "aws_ami" "amazon_linux" {
31+
most_recent = true
32+
33+
filter {
34+
name = "name"
35+
36+
values = [
37+
"amzn-ami-hvm-*-x86_64-gp2",
38+
]
39+
}
40+
41+
filter {
42+
name = "owner-alias"
43+
44+
values = [
45+
"amazon",
46+
]
47+
}
48+
}
49+
50+
/*
51+
resource "aws_launch_configuration" "this" {
52+
name_prefix = "${var.stack}_lc"
53+
}
54+
resource "aws_autoscaling_group" "this" {
55+
launch_configuration = "${aws_launch_configuration.this.name}"
56+
# v0.11.2: lifecycle ignore_changes cannot contain interpolations
57+
lifecycle {
58+
ignore_changes = ["desired_capacity"]
59+
}
60+
}
61+
/**/
62+
######
63+
# Launch configuration and autoscaling group
64+
######
65+
module "example" {
66+
source = "../../"
67+
name = "${var.stack}"
68+
associate_public_ip_address = "${var.associate_public_ip_address}"
69+
environment = "${var.environment}"
70+
iam_instance_profile = "${var.instance_profile_id}"
71+
image_id = "${data.aws_ami.amazon_linux.id}"
72+
instance_type = "${var.instance_type}"
73+
key_name = "${var.key_name}"
74+
security_groups = ["${data.aws_security_group.default.id}"]
75+
spot_price = "${var.spot_price}"
76+
target_group_arns = ["${var.target_group_arns}"]
77+
user_data = "${var.user_data}"
78+
79+
enabled_metrics = [
80+
"GroupInServiceInstances",
81+
"GroupMaxSize",
82+
"GroupDesiredCapacity",
83+
"GroupTotalInstances",
84+
]
85+
86+
root_block_device = [
87+
{
88+
volume_size = "${var.root_volume_size}"
89+
volume_type = "gp2"
90+
delete_on_termination = true
91+
},
92+
]
93+
94+
# Auto scaling group
95+
asg_name = "${var.stack}_asg"
96+
desired_capacity = "${var.desired_capacity}"
97+
health_check_grace_period = "${var.health_check_grace_period}"
98+
health_check_type = "${var.health_check_type}"
99+
max_size = "${var.max_size}"
100+
min_size = "${var.min_size}"
101+
vpc_zone_identifier = ["${data.aws_subnet_ids.all.ids}"]
102+
wait_for_capacity_timeout = 0
103+
104+
tags = {
105+
Name = "${var.stack} ASG"
106+
Project = "megasecret"
107+
Stack = "${var.stack}"
108+
}
109+
}

examples/wrapper/outputs.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Launch configuration
2+
output "launch_configuration_id" {
3+
description = "The ID of the launch configuration"
4+
value = "${module.example.launch_configuration_id}"
5+
}
6+
output "launch_configuration_name" {
7+
description = "The name of the launch configuration"
8+
value = "${module.example.launch_configuration_name}"
9+
}
10+
11+
# Autoscaling group
12+
output "autoscaling_group_id" {
13+
description = "The autoscaling group id"
14+
value = "${module.example.autoscaling_group_id}"
15+
}
16+
output "autoscaling_group_name" {
17+
description = "The autoscaling group name"
18+
value = "${module.example.autoscaling_group_name}"
19+
}

examples/wrapper/variables.tf

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
variable "environment" {
2+
default = "one"
3+
}
4+
5+
variable "region" {
6+
default = "us-west-2"
7+
}
8+
9+
variable "associate_public_ip_address" {
10+
description = "Add a public IP address"
11+
default = false
12+
}
13+
variable "desired_capacity" {
14+
description = "Desired number of instances"
15+
default = 1
16+
}
17+
18+
variable "health_check_grace_period" {
19+
description = "Auto Scaling Group health check grace period in seconds. Time allowed before starting to test the health check"
20+
default = "300"
21+
}
22+
variable "health_check_type" {
23+
description = "Healthcheck type"
24+
default = "ELB"
25+
}
26+
variable "instance_profile_id" {
27+
description = "Instance profile ID"
28+
default = ""
29+
}
30+
variable "instance_type" {
31+
description = "Instance type"
32+
default = "t2.micro"
33+
}
34+
variable "key_name" {
35+
description = "SSH key name to use"
36+
default = "devops20170606"
37+
}
38+
variable "max_size" {
39+
description = "Maximum number of instances"
40+
default = 2
41+
}
42+
variable "min_size" {
43+
description = "Minimum number of instances"
44+
default = 1
45+
}
46+
variable "root_volume_size" {
47+
description = "Size of the root volume"
48+
default = 8
49+
}
50+
variable "spot_price" {
51+
description = "Spot price. Set to false to not use spot pricing"
52+
default = false
53+
}
54+
variable "stack" {
55+
description = "Name of the service stack"
56+
default = "wrapper-test"
57+
}
58+
variable "tags" {
59+
description = "Map of tags"
60+
type = "map"
61+
default = {}
62+
}
63+
variable "target_group_arns" {
64+
description = "List of target group ARNs"
65+
type = "list"
66+
default = []
67+
}
68+
variable "user_data" {
69+
description = "User data script"
70+
default = " "
71+
}

0 commit comments

Comments
 (0)