Jamie-BitFlight / terraform-aws-ec2-instance

Terraform Module for providing a general EC2 instance provisioned by Ansible

Home Page:https://cloudposse.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

terraform-aws-ec2-instance Build Status

Terraform Module for providing a general purpose EC2 host.

Included features:

  • Automatically create a Security Group
  • Option to switch EIP attachment
  • CloudWatch monitoring and automatic reboot if instance hangs
  • Assume Role capability

Usage

Note: add ${var.ssh_key_pair} private key to the ssh agent.

Include this repository as a module in your existing terraform code.

Simple example:

module "instance" {
  source                      = "git::https://github.com/cloudposse/terraform-aws-ec2-instance.git?ref=master"
  namespace                   = "${var.namespace}"
  name                        = "${var.name}"
  stage                       = "${var.stage}"
  ssh_key_pair                = "${var.ssh_key_pair}"
  instance_type               = "${var.instance_type}"
  vpc_id                      = "${var.vpc_id}"
  security_groups             = ["${var.security_groups}"]
  subnet                      = "${var.subnet}"
}

Example with additional volumes and EIP

module "kafka_instance" {
  source                      = "git::https://github.com/cloudposse/terraform-aws-ec2-instance.git?ref=master"
  namespace                   = "${var.namespace}"
  name                        = "${var.name}"
  stage                       = "${var.stage}"
  ssh_key_pair                = "${var.ssh_key_pair}"
  vpc_id                      = "${var.vpc_id}"
  security_groups             = ["${var.security_groups}"]
  subnet                      = "${var.subnet}"
  associate_public_ip_address = "true"
  additional_ips_count        = "1"
  ebs_volume_count            = "2"
  allowed_ports               = ["22", "80", "443"]
}

Example with additional EC2 servers and volumes and EIP

module "kafka_instance" {
  source                      = "git::https://github.com/cloudposse/terraform-aws-ec2-instance.git?ref=master"
  namespace                   = "${var.namespace}"
  name                        = "${var.name}"
  stage                       = "${var.stage}"
  ssh_key_pair                = "${var.ssh_key_pair}"
  vpc_id                      = "${var.vpc_id}"
  security_groups             = ["${var.security_groups}"]
  subnet                      = "${var.subnet}"
  associate_public_ip_address = "true"
  additional_ips_count        = "1"
  ebs_volume_count            = "2"
  allowed_ports               = ["22", "80", "443"]
  instance_count              = "3"
}

Additional complete working example with variations of how to use the module

In /examples directory

This module depends on these modules:

It is necessary to run terraform get or terraform init to download this module.

Now reference the label when creating an instance (for example):

resource "aws_ami_from_instance" "example" {
  name               = "terraform-example"
  source_instance_id = "${module.admin_tier.id}"
}

Variables

Name Default Description Required
region `` AWS Region the instance is launched in. Optional. If not provided, the current region will be used No
namespace `` Namespace (e.g. cp or cloudposse) Yes
stage `` Stage (e.g. prod, dev, staging Yes
name `` Name (e.g. bastion or db) Yes
attributes [] Additional attributes (e.g. policy or role) No
tags {} Additional tags (e.g. map("BusinessUnit","XYZ") No
ami `` By default it is the AMI provided by Amazon with Ubuntu 16.04 No
instance_enabled true Flag to control the instance creation. Set to false if it is necessary to skip instance creation No
create_default_security_group true Create default Security Group with only Egress traffic allowed No
ssh_key_pair `` SSH key pair to be provisioned on the instance. If none provided, and generate_ssh_key_pair is true a new keypair is generated No
generate_ssh_key_pair false Should a new SSH key pair be generated. No
ssh_key_pair_path `` Path to where SSH key pair should be generated. Defaults to ${path.cwd} No
instance_type t2.micro The type of the instance (e.g. t2.micro) No
vpc_id `` The ID of the VPC that the instance security group belongs to Yes
security_groups [] List of Security Group IDs allowed to connect to the instance Yes
allowed_ports [] List of allowed ingress ports, e.g. ["22", "80", "443"] No
subnet `` VPC Subnet ID the instance is launched in Yes
associate_public_ip_address true Associate a public IP address with the instance No
assign_eip_address true Assign an Elastic IP address to the instance No
additional_ips_count 0 Count of additional EIPs No
private_ip `` Private IP address to associate with the instance in the VPC No
source_dest_check true Controls if traffic is routed to the instance when the destination address does not match the instance No
ipv6_address_count 0 Number of IPv6 addresses to associate with the primary network interface No
ipv6_addresses [] List of IPv6 addresses from the range of the subnet to associate with the primary network interface No
root_volume_type gp2 Type of the root volume. Can be standard, gp2 or io1 No
root_volume_size 10 Size of the root volume in gigabytes No
root_iops 0 Amount of provisioned IOPS. This must be set with a root_volume_type of io1 No
ebs_device_name [/dev/xvdb] Name of the EBS device to mount No
ebs_volume_type gp2 Type of EBS volume. Can be standard, gp2 or io1 No
ebs_volume_size 10 Size of the EBS volume in gigabytes No
ebs_iops 0 Amount of provisioned IOPS. This must be set if ebs_volume_type is set to io1 No
ebs_volume_count 0 Count of EBS volumes that will be attached to the instance No
delete_on_termination true Whether the volume should be destroyed on instance termination No
comparison_operator GreaterThanOrEqualToThreshold Arithmetic operation to use when comparing the specified Statistic and Threshold No
metric_name StatusCheckFailed_Instance Name for the alarm's associated metric No
evaluation_periods 5 Number of periods over which data is compared to the specified threshold No
metric_namespace AWS/EC2 Namespace for the alarm's associated metric No
applying_period 60 Period in seconds over which the specified statistic is applied No
statistic_level Maximum Statistic to apply to the alarm's associated metric No
metric_threshold 1 Value against which the specified statistic is compared No
default_alarm_action action/actions/AWS_EC2.InstanceId.Reboot/1.0 String of action to execute when this alarm transitions into an ALARM state No
instance_count 1 Number of EC2 instances to create - each will have the same settings No

Outputs

Name Description
id Disambiguated ID
private_dns Private DNS of the instance
private_ip Private IP of the instance
public_ip Public IP of the instance (or EIP )
aws_key_pair Name of AWS key
ssh_key_pem_path Local path to SSH pem key
security_group_id ID of the AWS Security Group associated with the instance
role Name of the AWS IAM Role associated with the instance
alarm CloudWatch Alarm ID
additional_eni_ids Map of ENI to EIP
ebs_ids IDs of EBSs
primary_network_interface_id ID of the instance's primary network interface
network_interface_id ID of the network interface that was created with the instance
public_dns Public DNS of the instance (or DNS of EIP)

License

References

Help

Got a question?

Review the docs, file a GitHub issue, send us an email or reach out to us on Gitter.

Contributing

Bug Reports & Feature Requests

Please use the issue tracker to report any bugs or file feature requests.

Developing

In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.

  1. Fork the repo on GitHub
  2. Clone the project to your own machine
  3. Commit changes to your own branch
  4. Push your work back up to your fork
  5. Submit a Pull request so that we can review your changes

NOTE: Be sure to merge the latest from "upstream" before making a pull request!

License

APACHE 2.0 © 2016-2017 Cloud Posse, LLC

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.

About

This module is maintained and funded by Cloud Posse, LLC. Like it? Please let us know at hello@cloudposse.com

We love Open Source Software!

See our other projects or hire us to help build your next cloud-platform.

Contributors

Erik Osterman
Erik Osterman
Igor Rodionov
Igor Rodionov
Andriy Knysh
Andriy Knysh
Sergey Vasilyev
Sergey Vasilyev
Konstantin B
Konstantin B
Valeriy
Valeriy
Vladimir
Vladimir

About

Terraform Module for providing a general EC2 instance provisioned by Ansible

https://cloudposse.com/

License:Apache License 2.0


Languages

Language:HCL 96.5%Language:Shell 2.4%Language:Makefile 1.1%