diff --git a/examples/asg_ec2_launch_template/README.md b/examples/asg_ec2_launch_template/README.md new file mode 100644 index 0000000..7b7fae0 --- /dev/null +++ b/examples/asg_ec2_launch_template/README.md @@ -0,0 +1,31 @@ +# Auto Scaling Group without ELB example + +Configuration in this directory creates Launch Configuration and Auto Scaling Group. + +Data sources are used to discover existing VPC resources (VPC, subnet and security group) as well as AMI details. + +## Usage + +To run this example you need to execute: + +```bash +$ terraform init +$ terraform plan +$ terraform apply +``` + +Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources. + + +## Outputs + +| Name | Description | +|------|-------------| +| this\_autoscaling\_group\_availability\_zones | The availability zones of the autoscale group | +| this\_autoscaling\_group\_id | The autoscaling group id | +| this\_autoscaling\_group\_load\_balancers | The load balancer names associated with the autoscaling group | +| this\_autoscaling\_group\_target\_group\_arns | List of Target Group ARNs that apply to this AutoScaling Group | +| this\_autoscaling\_group\_vpc\_zone\_identifier | The VPC zone identifier | +| this\_launch\_configuration\_id | The ID of the launch configuration | + + diff --git a/examples/asg_ec2_launch_template/main.tf b/examples/asg_ec2_launch_template/main.tf new file mode 100644 index 0000000..e0c6f02 --- /dev/null +++ b/examples/asg_ec2_launch_template/main.tf @@ -0,0 +1,124 @@ +provider "aws" { + region = "eu-west-1" + + # Make it faster by skipping something + skip_get_ec2_platforms = true + skip_metadata_api_check = true + skip_region_validation = true + skip_credentials_validation = true + skip_requesting_account_id = true +} + +############################################################## +# Data sources to get VPC, subnets and security group details +############################################################## +data "aws_vpc" "default" { + default = true +} + +data "aws_subnet_ids" "all" { + vpc_id = data.aws_vpc.default.id +} + +data "aws_security_group" "default" { + vpc_id = data.aws_vpc.default.id + name = "default" +} + +data "aws_ami" "amazon_linux" { + most_recent = true + owners = ["137112412989"] # Amazon + + filter { + name = "name" + + values = [ + "amzn-ami-hvm-*-x86_64-gp2", + ] + } + + filter { + name = "owner-alias" + + values = [ + "amazon", + ] + } +} + +resource "aws_iam_service_linked_role" "autoscaling" { + aws_service_name = "autoscaling.amazonaws.com" + description = "A service linked role for autoscaling" + custom_suffix = "something" + + # Sometimes good sleep is required to have some IAM resources created before they can be used + provisioner "local-exec" { + command = "sleep 10" + } +} + +###### +# Launch configuration and autoscaling group +###### +module "example" { + source = "../../" + + name = "example-with-ec2" + + # Launch configuration + # + # launch_configuration = "my-existing-launch-configuration" # Use the existing launch configuration + # create_lc = false # disables creation of launch configuration + lc_name = "example-lc" + + image_id = data.aws_ami.amazon_linux.id + instance_type = "t2.micro" + security_groups = [data.aws_security_group.default.id] + associate_public_ip_address = true + recreate_asg_when_lc_changes = true + + ebs_block_device = [ + { + device_name = "/dev/xvdz" + volume_type = "gp2" + volume_size = "50" + delete_on_termination = true + }, + ] + + root_block_device = [ + { + volume_size = "50" + volume_type = "gp2" + delete_on_termination = true + }, + ] + + # Auto scaling group + asg_name = "example-asg" + vpc_zone_identifier = data.aws_subnet_ids.all.ids + health_check_type = "EC2" + min_size = 0 + max_size = 1 + desired_capacity = 0 + wait_for_capacity_timeout = 0 + service_linked_role_arn = aws_iam_service_linked_role.autoscaling.arn + + tags = [ + { + key = "Environment" + value = "dev" + propagate_at_launch = true + }, + { + key = "Project" + value = "megasecret" + propagate_at_launch = true + }, + ] + + tags_as_map = { + extra_tag1 = "extra_value1" + extra_tag2 = "extra_value2" + } +} diff --git a/examples/asg_ec2_launch_template/outputs.tf b/examples/asg_ec2_launch_template/outputs.tf new file mode 100644 index 0000000..54cd001 --- /dev/null +++ b/examples/asg_ec2_launch_template/outputs.tf @@ -0,0 +1,29 @@ +output "this_launch_configuration_id" { + description = "The ID of the launch configuration" + value = module.example.this_launch_configuration_id +} + +output "this_autoscaling_group_id" { + description = "The autoscaling group id" + value = module.example.this_autoscaling_group_id +} + +output "this_autoscaling_group_availability_zones" { + description = "The availability zones of the autoscale group" + value = module.example.this_autoscaling_group_availability_zones +} + +output "this_autoscaling_group_vpc_zone_identifier" { + description = "The VPC zone identifier" + value = module.example.this_autoscaling_group_vpc_zone_identifier +} + +output "this_autoscaling_group_load_balancers" { + description = "The load balancer names associated with the autoscaling group" + value = module.example.this_autoscaling_group_load_balancers +} + +output "this_autoscaling_group_target_group_arns" { + description = "List of Target Group ARNs that apply to this AutoScaling Group" + value = module.example.this_autoscaling_group_target_group_arns +} diff --git a/main.tf b/main.tf index 7e45722..3b73df2 100644 --- a/main.tf +++ b/main.tf @@ -90,7 +90,7 @@ resource "aws_launch_template" "this" { } iam_instance_profile { - arn = var.iam_instance_profile + name = var.iam_instance_profile } network_interfaces {