diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2308d6fe..8677870d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,10 +1,26 @@ repos: - repo: git://github.com/antonbabenko/pre-commit-terraform - rev: v1.31.0 + rev: v1.44.0 hooks: - id: terraform_fmt + - id: terraform_validate - id: terraform_docs + - id: terraform_tflint + args: + - '--args=--only=terraform_deprecated_interpolation' + - '--args=--only=terraform_deprecated_index' + - '--args=--only=terraform_unused_declarations' + - '--args=--only=terraform_comment_syntax' + - '--args=--only=terraform_documented_outputs' + - '--args=--only=terraform_documented_variables' + - '--args=--only=terraform_typed_variables' + - '--args=--only=terraform_module_pinned_source' + - '--args=--only=terraform_naming_convention' + - '--args=--only=terraform_required_version' + - '--args=--only=terraform_required_providers' + - '--args=--only=terraform_standard_module_structure' + - '--args=--only=terraform_workspace_remote' - repo: git://github.com/pre-commit/pre-commit-hooks - rev: v3.1.0 + rev: v3.3.0 hooks: - id: check-merge-conflict diff --git a/CHANGELOG.md b/CHANGELOG.md index cce3923f..17be52bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file. ## [Unreleased] - +- feat: Add support for "metadata_options" argument ([#191](https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/issues/191)) ## [v2.15.0] - 2020-06-10 diff --git a/README.md b/README.md index 729e6d01..a6e00b5f 100644 --- a/README.md +++ b/README.md @@ -94,13 +94,16 @@ data "aws_ami" "ubuntu-xenial" { ## Requirements -No requirements. +| Name | Version | +|------|---------| +| terraform | >= 0.12.6 | +| aws | >= 2.65 | ## Providers | Name | Version | |------|---------| -| aws | n/a | +| aws | >= 2.65 | ## Inputs @@ -121,6 +124,7 @@ No requirements. | ipv6\_address\_count | A number of IPv6 addresses to associate with the primary network interface. Amazon EC2 chooses the IPv6 addresses from the range of your subnet. | `number` | `null` | no | | ipv6\_addresses | Specify one or more IPv6 addresses from the range of the subnet to associate with the primary network interface | `list(string)` | `null` | no | | key\_name | The key name to use for the instance | `string` | `""` | no | +| metadata\_options | Customize the metadata options of the instance | `map(string)` | `{}` | no | | monitoring | If true, the launched EC2 instance will have detailed monitoring enabled | `bool` | `false` | no | | name | Name to be used on all resources as prefix | `string` | n/a | yes | | network\_interface | Customize network interfaces to be attached at instance boot time | `list(map(string))` | `[]` | no | @@ -153,6 +157,7 @@ No requirements. | instance\_state | List of instance states of instances | | ipv6\_addresses | List of assigned IPv6 addresses of instances | | key\_name | List of key names of instances | +| metadata\_options | List of metadata options of instances | | password\_data | List of Base-64 encoded encrypted password data for the instance | | placement\_group | List of placement groups of instances | | primary\_network\_interface\_id | List of IDs of the primary network interface of instances | diff --git a/examples/basic/README.md b/examples/basic/README.md index c1497e60..0be7834e 100644 --- a/examples/basic/README.md +++ b/examples/basic/README.md @@ -17,13 +17,16 @@ Note that this example may create resources which can cost money. Run `terraform ## Requirements -No requirements. +| Name | Version | +|------|---------| +| terraform | >= 0.12.6 | +| aws | >= 2.65 | ## Providers | Name | Version | |------|---------| -| aws | n/a | +| aws | >= 2.65 | ## Inputs @@ -40,6 +43,8 @@ No input. | ids\_t2 | List of IDs of t2-type instances | | instance\_id | EC2 instance ID | | instance\_public\_dns | Public DNS name assigned to the EC2 instance | +| metadata\_options | Metadata options for the instance | +| metadata\_options\_custom | Customized metadata options for the instance | | placement\_group | List of placement group | | public\_dns | List of public DNS names assigned to the instances | | root\_block\_device\_volume\_ids | List of volume IDs of root block devices of instances | diff --git a/examples/basic/main.tf b/examples/basic/main.tf index 2ae888d6..43d53620 100644 --- a/examples/basic/main.tf +++ b/examples/basic/main.tf @@ -83,7 +83,7 @@ module "ec2" { ami = data.aws_ami.amazon_linux.id instance_type = "c5.large" subnet_id = tolist(data.aws_subnet_ids.all.ids)[0] - // private_ips = ["172.31.32.5", "172.31.46.20"] + # private_ips = ["172.31.32.5", "172.31.46.20"] vpc_security_group_ids = [module.security_group.this_security_group_id] associate_public_ip_address = true placement_group = aws_placement_group.web.id @@ -123,7 +123,7 @@ module "ec2_with_t2_unlimited" { instance_type = "t2.micro" cpu_credits = "unlimited" subnet_id = tolist(data.aws_subnet_ids.all.ids)[0] - // private_ip = "172.31.32.10" + # private_ip = "172.31.32.10" vpc_security_group_ids = [module.security_group.this_security_group_id] associate_public_ip_address = true } @@ -142,6 +142,25 @@ module "ec2_with_t3_unlimited" { associate_public_ip_address = true } +module "ec2_with_metadata_options" { + source = "../../" + + instance_count = 1 + + name = "example-metadata_options" + ami = data.aws_ami.amazon_linux.id + instance_type = "t2.small" + subnet_id = tolist(data.aws_subnet_ids.all.ids)[0] + vpc_security_group_ids = [module.security_group.this_security_group_id] + associate_public_ip_address = true + + metadata_options = { + http_endpoint = "enabled" + http_tokens = "required" + http_put_response_hop_limit = 8 + } +} + module "ec2_with_network_interface" { source = "../../" diff --git a/examples/basic/outputs.tf b/examples/basic/outputs.tf index bb6055cc..9a7a52d6 100644 --- a/examples/basic/outputs.tf +++ b/examples/basic/outputs.tf @@ -63,3 +63,12 @@ output "credit_specification_t2_unlimited" { value = module.ec2_with_t2_unlimited.credit_specification } +output "metadata_options" { + description = "Metadata options for the instance" + value = module.ec2.metadata_options +} + +output "metadata_options_custom" { + description = "Customized metadata options for the instance" + value = module.ec2_with_metadata_options.metadata_options +} diff --git a/examples/basic/variables.tf b/examples/basic/variables.tf new file mode 100644 index 00000000..e69de29b diff --git a/examples/basic/versions.tf b/examples/basic/versions.tf new file mode 100644 index 00000000..069bfb89 --- /dev/null +++ b/examples/basic/versions.tf @@ -0,0 +1,7 @@ +terraform { + required_version = ">= 0.12.6" + + required_providers { + aws = ">= 2.65" + } +} diff --git a/examples/volume-attachment/README.md b/examples/volume-attachment/README.md index 550af75e..0f69cb24 100644 --- a/examples/volume-attachment/README.md +++ b/examples/volume-attachment/README.md @@ -21,19 +21,22 @@ Note that this example may create resources which can cost money. Run `terraform ## Requirements -No requirements. +| Name | Version | +|------|---------| +| terraform | >= 0.12.6 | +| aws | >= 2.65 | ## Providers | Name | Version | |------|---------| -| aws | n/a | +| aws | >= 2.65 | ## Inputs | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| instances\_number | n/a | `number` | `1` | no | +| instances\_number | NUmber of instances | `number` | `1` | no | ## Outputs diff --git a/examples/volume-attachment/main.tf b/examples/volume-attachment/main.tf index d5a51403..5a8f9b00 100644 --- a/examples/volume-attachment/main.tf +++ b/examples/volume-attachment/main.tf @@ -2,10 +2,6 @@ provider "aws" { region = "eu-west-1" } -variable "instances_number" { - default = 1 -} - ################################################################## # Data sources to get VPC, subnet, security group and AMI details ################################################################## diff --git a/examples/volume-attachment/variables.tf b/examples/volume-attachment/variables.tf new file mode 100644 index 00000000..5ef4cf0e --- /dev/null +++ b/examples/volume-attachment/variables.tf @@ -0,0 +1,5 @@ +variable "instances_number" { + description = "NUmber of instances" + type = number + default = 1 +} diff --git a/examples/volume-attachment/versions.tf b/examples/volume-attachment/versions.tf new file mode 100644 index 00000000..069bfb89 --- /dev/null +++ b/examples/volume-attachment/versions.tf @@ -0,0 +1,7 @@ +terraform { + required_version = ">= 0.12.6" + + required_providers { + aws = ">= 2.65" + } +} diff --git a/main.tf b/main.tf index cb8f5896..df33ec2e 100644 --- a/main.tf +++ b/main.tf @@ -61,6 +61,15 @@ resource "aws_instance" "this" { } } + dynamic "metadata_options" { + for_each = length(keys(var.metadata_options)) == 0 ? [] : [var.metadata_options] + content { + http_endpoint = lookup(metadata_options.value, "http_endpoint", "enabled") + http_tokens = lookup(metadata_options.value, "http_tokens", "optional") + http_put_response_hop_limit = lookup(metadata_options.value, "http_put_response_hop_limit", "1") + } + } + dynamic "network_interface" { for_each = var.network_interface content { diff --git a/outputs.tf b/outputs.tf index f13cd4c7..70bf0b71 100644 --- a/outputs.tf +++ b/outputs.tf @@ -78,6 +78,11 @@ output "credit_specification" { value = aws_instance.this.*.credit_specification } +output "metadata_options" { + description = "List of metadata options of instances" + value = aws_instance.this.*.metadata_options +} + output "instance_state" { description = "List of instance states of instances" value = aws_instance.this.*.instance_state diff --git a/variables.tf b/variables.tf index fe5b0d3a..3662f265 100644 --- a/variables.tf +++ b/variables.tf @@ -181,6 +181,12 @@ variable "cpu_credits" { default = "standard" } +variable "metadata_options" { + description = "Customize the metadata options of the instance" + type = map(string) + default = {} +} + variable "use_num_suffix" { description = "Always append numerical suffix to instance name, even if instance_count is 1" type = bool @@ -192,5 +198,3 @@ variable "num_suffix_format" { type = string default = "-%d" } - - diff --git a/versions.tf b/versions.tf new file mode 100644 index 00000000..069bfb89 --- /dev/null +++ b/versions.tf @@ -0,0 +1,7 @@ +terraform { + required_version = ">= 0.12.6" + + required_providers { + aws = ">= 2.65" + } +}