Commit fcf0c6e1 authored by Anton Babenko's avatar Anton Babenko Committed by GitHub

Added example of EBS volume attachment (related to #46) (#47)

parent 403ddf4b
......@@ -32,8 +32,9 @@ module "ec2_cluster" {
## Examples
* [Basic EC2 instance](https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/tree/master/examples/basic)
* [EC2 instance with EBS volume attachment](https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/tree/master/examples/volume-attachment)
## Make an encrypted ami for use
## Make an encrypted AMI for use
This module does not sopport encrypted AMI's out of the box however it is easy enough for you to generate one for use
......@@ -76,10 +77,11 @@ data "aws_ami" "ubuntu-xenial" {
```
## Limitations
## Notes
* `network_interface` can't be specified together with `associate_public_ip_address`, which makes `network_interface`
not configurable using this module at the moment
* Changes in `ebs_block_device` argument will be ignored. Use [aws_volume_attachment](https://www.terraform.io/docs/providers/aws/r/volume_attachment.html) resource to attach and detach volumes from AWS EC2 instances. See [this example](https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/tree/master/examples/volume-attachment).
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
......
# EC2 instance with EBS volume attachment
Configuration in this directory creates EC2 instances, EBS volume and attach it together.
Unspecified arguments for security group id and subnet are inherited from the default VPC.
This example outputs instance id and EBS volume id.
## 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.
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Outputs
| Name | Description |
|------|-------------|
| ebs_volume_attachment_id | The volume ID |
| ebs_volume_attachment_instance_id | The instance ID |
| instance_id | EC2 instance ID |
| instance_public_dns | Public DNS name assigned to the EC2 instance |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
provider "aws" {
region = "eu-west-1"
}
##################################################################
# 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
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"
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"]
}
module "ec2" {
source = "../../"
instance_count = 1
name = "example-with-ebs"
ami = "${data.aws_ami.amazon_linux.id}"
instance_type = "m4.large"
subnet_id = "${element(data.aws_subnet_ids.all.ids, 0)}"
vpc_security_group_ids = ["${module.security_group.this_security_group_id}"]
associate_public_ip_address = true
}
resource "aws_volume_attachment" "this_ec2" {
device_name = "/dev/sdh"
volume_id = "${aws_ebs_volume.this.id}"
instance_id = "${module.ec2.id[0]}"
}
resource "aws_ebs_volume" "this" {
availability_zone = "${module.ec2.availability_zone[0]}"
size = 1
}
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]}"
}
output "ebs_volume_attachment_id" {
description = "The volume ID"
value = "${aws_volume_attachment.this_ec2.volume_id}"
}
output "ebs_volume_attachment_instance_id" {
description = "The instance ID"
value = "${aws_volume_attachment.this_ec2.instance_id}"
}
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