Commit f3ab5f36 authored by Anton Babenko's avatar Anton Babenko

Added complete code with example and READMEs

parent 7e7acc99
.terraform
terraform.tfstate
*.tfstate*
terraform.tfvars
Licensed 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.
# terraform-aws-ec2
Terraform module which creates EC2 instance on AWS
AWS EC2 Terraform module
========================
Terraform module which creates EC2 instance(s) on AWS.
These types of resources are supported:
* [VPC](https://www.terraform.io/docs/providers/aws/r/vpc.html)
* [Subnet](https://www.terraform.io/docs/providers/aws/r/aws_subnet.html)
* [Route](https://www.terraform.io/docs/providers/aws/r/route.html)
* [Route table](https://www.terraform.io/docs/providers/aws/r/route_table.html)
* [Internet Gateway](https://www.terraform.io/docs/providers/aws/r/internet_gateway.html)
* [NAT Gateway](https://www.terraform.io/docs/providers/aws/r/nat_gateway.html)
* [VPC Endpoint](https://www.terraform.io/docs/providers/aws/r/vpc_endpoint.html) (S3 and DynamoDB)
* [RDS DB Subnet Group](https://www.terraform.io/docs/providers/aws/r/db_subnet_group.html)
* [ElastiCache Subnet Group](https://www.terraform.io/docs/providers/aws/r/elasticache_subnet_group.html)
Usage
-----
```hcl
module "ec2" {
source = "terraform-aws-modules/ec2/aws"
name = "my-cluster"
count = 5
ami = "ami-ebd02392"
instance_type = "t2.micro"
key_name = "user1"
monitoring = true
vpc_security_group_ids = ["sg-12345678"]
tags = {
Terraform = "true"
Environment = "dev"
}
}
```
Examples
========
* [Basic EC2 instance](examples/basic)
Authors
=======
Module managed by [Anton Babenko](https://github.com/antonbabenko).
License
=======
Apache 2 Licensed. See LICENSE for full details.
\ No newline at end of file
Basic EC2 instance
==================
Configuration in this directory creates single EC2 instance with minimum set of arguments: AMI ID and instance type.
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.
provider "aws" {
region = "eu-west-1"
}
data "aws_ami" "amazon_linux" {
most_recent = true
filter {
name = "name"
values = [
"amzn-ami-hvm-*-x86_64-gp2",
]
}
filter {
name = "owner-alias"
values = [
"amazon",
]
}
}
module "ec2" {
source = "../../"
name = "example"
ami = "${data.aws_ami.amazon_linux.id}"
instance_type = "t2.micro"
}
output "id" {
description = "List of IDs of instances"
value = ["${module.ec2.id}"]
}
output "public_dns" {
description = "List of public DNS names assigned to the instances"
value = ["${module.ec2.public_dns}"]
}
output "instance_id" {
description = "EC2 instance ID"
value = "${module.ec2.id[0]}"
}
output "instance_public_dns" {
description = "Public DNS name assigned to the EC2 instance"
value = "${module.ec2.public_dns[0]}"
}
######
# EC2 instance
######
resource "aws_instance" "this" {
count = "${var.count}"
ami = "${var.ami}"
instance_type = "${var.instance_type}"
user_data = "${var.user_data}"
subnet_id = "${var.subnet_id}"
key_name = "${var.key_name}"
monitoring = "${var.monitoring}"
vpc_security_group_ids = ["${var.vpc_security_group_ids}"]
iam_instance_profile = "${var.iam_instance_profile}"
associate_public_ip_address = "${var.associate_public_ip_address}"
private_ip = "${var.private_ip}"
ipv6_address_count = "${var.ipv6_address_count}"
ipv6_addresses = "${var.ipv6_addresses}"
ebs_optimized = "${var.ebs_optimized}"
volume_tags = "${var.volume_tags}"
root_block_device = "${var.root_block_device}"
ebs_block_device = "${var.ebs_block_device}"
ephemeral_block_device = "${var.ephemeral_block_device}"
source_dest_check = "${var.source_dest_check}"
disable_api_termination = "${var.disable_api_termination}"
instance_initiated_shutdown_behavior = "${var.instance_initiated_shutdown_behavior}"
availability_zone = "${var.availability_zone}"
placement_group = "${var.placement_group}"
tenancy = "${var.tenancy}"
# Note: network_interface can't be specified together with associate_public_ip_address
# network_interface = "${var.network_interface}"
tags = "${merge(var.tags, map("Name", format("%s-%d", var.name, count.index+1)))}"
}
output "id" {
description = "List of IDs of instances"
value = ["${aws_instance.this.*.id}"]
}
output "availability_zone" {
description = "List of availability zones of instances"
value = ["${aws_instance.this.*.availability_zone}"]
}
output "placement_group" {
description = "List of placement groups of instances"
value = ["${aws_instance.this.*.placement_group}"]
}
output "key_name" {
description = "List of key names of instances"
value = ["${aws_instance.this.*.key_name}"]
}
output "public_dns" {
description = "List of public DNS names assigned to the instances. For EC2-VPC, this is only available if you've enabled DNS hostnames for your VPC"
value = ["${aws_instance.this.*.public_dns}"]
}
output "public_ip" {
description = "List of public IP addresses assigned to the instances, if applicable"
value = ["${aws_instance.this.*.public_ip}"]
}
output "network_interface_id" {
description = "List of IDs of the network interface of instances"
value = ["${aws_instance.this.*.network_interface_id}"]
}
output "primary_network_interface_id" {
description = "List of IDs of the primary network interface of instances"
value = ["${aws_instance.this.*.primary_network_interface_id}"]
}
output "private_dns" {
description = "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"
value = ["${aws_instance.this.*.private_dns}"]
}
output "private_ip" {
description = "List of private IP addresses assigned to the instances"
value = ["${aws_instance.this.*.private_ip}"]
}
output "security_groups" {
description = "List of associated security groups of instances"
value = ["${aws_instance.this.*.security_groups}"]
}
output "vpc_security_group_ids" {
description = "List of associated security groups of instances, if running in non-default VPC"
value = ["${aws_instance.this.*.vpc_security_group_ids}"]
}
output "subnet_id" {
description = "List of IDs of VPC subnets of instances"
value = ["${aws_instance.this.*.}"]
}
variable "name" {
description = "Name to be used on all resources as prefix"
}
variable "count" {
description = "Number of instances to launch"
default = 1
}
variable "ami" {
description = "ID of AMI to use for the instance"
}
variable "availability_zone" {
description = "The AZ to start the instance in"
default = ""
}
variable "placement_group" {
description = "The Placement Group to start the instance in"
default = ""
}
variable "tenancy" {
description = "The tenancy of the instance (if the instance is running in a VPC). Available values: default, dedicated, host."
default = "default"
}
variable "ebs_optimized" {
description = "If true, the launched EC2 instance will be EBS-optimized"
default = false
}
variable "disable_api_termination" {
description = "If true, enables EC2 Instance Termination Protection"
default = false
}
variable "instance_initiated_shutdown_behavior" {
description = "Shutdown behavior for the instance" # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/terminating-instances.html#Using_ChangingInstanceInitiatedShutdownBehavior
default = ""
}
variable "instance_type" {
description = "The type of instance to start"
}
variable "key_name" {
description = "The key name to use for the instance"
default = ""
}
variable "monitoring" {
description = "If true, the launched EC2 instance will have detailed monitoring enabled"
default = false
}
variable "vpc_security_group_ids" {
description = "A list of security group IDs to associate with"
default = []
}
variable "subnet_id" {
description = "The VPC Subnet ID to launch in"
default = ""
}
variable "associate_public_ip_address" {
description = "If true, the EC2 instance will have associated public IP address"
default = false
}
variable "private_ip" {
description = "Private IP address to associate with the instance in a VPC"
default = ""
}
variable "source_dest_check" {
description = "Controls if traffic is routed to the instance when the destination address does not match the instance. Used for NAT or VPNs."
default = true
}
variable "user_data" {
description = "The user data to provide when launching the instance"
default = ""
}
variable "iam_instance_profile" {
description = "The IAM Instance Profile to launch the instance with. Specified as the name of the Instance Profile."
default = ""
}
variable "ipv6_address_count" {
description = "A number of IPv6 addresses to associate with the primary network interface. Amazon EC2 chooses the IPv6 addresses from the range of your subnet."
default = 0
}
variable "ipv6_addresses" {
description = "Specify one or more IPv6 addresses from the range of the subnet to associate with the primary network interface"
default = []
}
variable "tags" {
description = "A mapping of tags to assign to the resource"
default = {}
}
variable "volume_tags" {
description = "A mapping of tags to assign to the devices created by the instance at launch time"
default = {}
}
variable "root_block_device" {
description = "Customize details about the root block device of the instance. See Block Devices below for details"
default = []
}
variable "ebs_block_device" {
description = "Additional EBS block devices to attach to the instance"
default = []
}
variable "ephemeral_block_device" {
description = "Customize Ephemeral (also known as Instance Store) volumes on the instance"
default = []
}
variable "network_interface" {
description = "Customize network interfaces to be attached at instance boot time"
default = []
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment