Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 -->

Expand Down
32 changes: 32 additions & 0 deletions examples/volume-attachment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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 -->
70 changes: 70 additions & 0 deletions examples/volume-attachment/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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
}
19 changes: 19 additions & 0 deletions examples/volume-attachment/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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}"
}