### Terraform Module Usage (v5.x Example) Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/docs/UPGRADE-6.0.md Example HCL configuration demonstrating how to use the terraform-aws-ec2-instance module with version 5.8.0. It showcases configurations for root block device, EBS block device, and network interfaces, reflecting usage patterns prior to version 6.x. ```hcl module "ec2_upgrade" { source = "terraform-aws-modules/ec2-instance/aws" version = "5.8.0" # Truncated for brevity, only relevant module API changes are shown ... root_block_device = [ { encrypted = true volume_size = 50 volume_type = "gp3" throughput = 200 tags = { Name = "my-root-block" } }, ] ebs_block_device = [ { device_name = "/dev/sdf" encrypted = true volume_size = 5 volume_type = "gp3" throughput = 200 tags = { MountPoint = "/mnt/data" } } ] network_interface = [ { device_index = 0 network_interface_id = aws_network_interface.this.id delete_on_termination = false } ] tags = local.tags } ``` -------------------------------- ### HCL: Terraform EC2 Instance Module v3.x Example Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/docs/UPGRADE-3.0.md Demonstrates the updated configuration structure for the terraform-aws-ec2-instance module in version 3.x. This version utilizes `for_each` and singular inputs like `subnet_id` for managing multiple instances. ```hcl locals { num_suffix_format = "-%d" multiple_instances = { 0 = { num_suffix = 1 instance_type = "c5.large" subnet_id = element(module.vpc.private_subnets, 0) } 1 = { num_suffix = 2 instance_type = "c5.large" subnet_id = element(module.vpc.private_subnets, 1) } 2 = { num_suffix = 3 instance_type = "c5.large" subnet_id = element(module.vpc.private_subnets, 2) } } } module "ec2_upgrade" { source = "../../" for_each = local.multiple_instances name = format("%s${local.num_suffix_format}", local.name, each.value.num_suffix) ami = data.aws_ami.amazon_linux.id instance_type = each.value.instance_type subnet_id = each.value.subnet_id vpc_security_group_ids = [module.security_group.security_group_id] associate_public_ip_address = true tags = local.tags } ``` -------------------------------- ### HCL: Terraform EC2 Instance Module v2.x Example Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/docs/UPGRADE-3.0.md Illustrates the configuration structure for the terraform-aws-ec2-instance module in version 2.x. This example uses list-based inputs like `subnet_ids` for multiple instances. ```hcl module "ec2_upgrade" { source = "terraform-aws-modules/ec2-instance/aws" version = "2.21.0" instance_count = 3 name = local.name ami = data.aws_ami.amazon_linux.id instance_type = "c5.large" subnet_ids = module.vpc.private_subnets vpc_security_group_ids = [module.security_group.security_group_id] associate_public_ip_address = true tags = local.tags } ``` -------------------------------- ### Execute Terraform Commands for EC2 Instance Setup Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/examples/volume-attachment/README.md Provides the standard Terraform workflow commands to initialize, plan, and apply the EC2 instance and EBS volume configuration. Users should run `terraform destroy` when resources are no longer needed to avoid costs. ```bash $ terraform init $ terraform plan $ terraform apply ``` -------------------------------- ### Terraform Deployment Commands Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/examples/session-manager/README.md Standard commands to initialize, plan, and apply a Terraform configuration. Ensure you have Terraform installed and configured for AWS access. ```bash terraform init terraform plan terraform apply ``` -------------------------------- ### Encrypted AMI Creation Example Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/README.md Provides an example of how to create an encrypted AMI using `aws_ami_copy` resource, based on a source AMI, for use with EC2 instances. It includes data sources to find the latest Ubuntu AMI. ```hcl provider "aws" { region = "us-west-2" } data "aws_ami" "ubuntu" { most_recent = true owners = ["679593333241"] filter { name = "name" values = ["ubuntu-minimal/images/hvm-ssd/ubuntu-focal-20.04-*"] } filter { name = "virtualization-type" values = ["hvm"] } } resource "aws_ami_copy" "ubuntu_encrypted_ami" { name = "ubuntu-encrypted-ami" description = "An encrypted root ami based off ${data.aws_ami.ubuntu.id}" source_ami_id = data.aws_ami.ubuntu.id source_ami_region = "eu-west-2" encrypted = true tags = { Name = "ubuntu-encrypted-ami" } } data "aws_ami" "encrypted-ami" { most_recent = true filter { name = "name" values = [aws_ami_copy.ubuntu_encrypted_ami.id] } owners = ["self"] } ``` -------------------------------- ### Bash: Terraform State Migration Commands Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/docs/UPGRADE-3.0.md Commands to migrate Terraform state when upgrading the EC2 instance module from v2.x to v3.x. These commands ensure that existing resources are correctly re-associated with the new module structure. ```bash terraform state mv 'module.ec2_upgrade.aws_instance.this[0]' 'module.ec2_upgrade["0"].aws_instance.this[0]' terraform state mv 'module.ec2_upgrade.aws_instance.this[1]' 'module.ec2_upgrade["1"].aws_instance.this[0]' terraform state mv 'module.ec2_upgrade.aws_instance.this[2]' 'module.ec2_upgrade["2"].aws_instance.this[0]' ``` -------------------------------- ### Spot Valid From Configuration Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/README.md Specifies the start date and time for the Spot Instance request in UTC RFC3339 format (e.g., YYYY-MM-DDTHH:MM:SSZ). ```APIDOC spot_valid_from: description: The start date and time of the request, in UTC RFC3339 format(for example, YYYY-MM-DDTHH:MM:SSZ) type: string default: null ``` -------------------------------- ### Terraform Inputs Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/examples/session-manager/README.md Describes the input variables for the Terraform configuration. In this specific example, no custom inputs are defined, meaning default values or implicit configurations will be used. ```APIDOC Inputs: No inputs defined. ``` -------------------------------- ### Terraform HCL: Configure EC2 Instance Module v6.x Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/docs/UPGRADE-6.0.md This HCL code demonstrates the updated configuration structure for the Terraform AWS EC2 Instance module in version 6.x. It highlights changes in how root block devices, EBS volumes, and network interfaces are defined, moving from lists to maps for greater flexibility. ```hcl module "ec2_upgrade" { source = "terraform-aws-modules/ec2-instance/aws" version = "6.0.0" # Truncated for brevity, only relevant module API changes are shown ... # There can only be one root block device, so the wrapping list is removed root_block_device = { encrypted = true size = 50 # Was `volume_size` type = "gp3" # Was `volume_type` throughput = 200 tags = { Name = "my-root-block" } } # Now a map of EBS volumes is used instead of a list ebs_volumes = { # The device_name can be the key of the map, or set by `device_name` attribute "/dev/sdf" = { encrypted = true size = 5 # Was `volume_size` type = "gp3" # Was `volume_type`, `gp3` is now the default throughput = 200 tags = { MountPoint = "/mnt/data" } } } # Now a map of network interfaces is used instead of a list network_interface = { # The device_index can be the key of the map, or set by `device_index` attribute 0 = { network_interface_id = aws_network_interface.this.id delete_on_termination = false } } tags = local.tags } ``` -------------------------------- ### Terragrunt Usage for Multiple S3 Buckets Wrapper Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/wrappers/README.md An example of using the S3 bucket module wrapper with Terragrunt to manage multiple S3 buckets. It configures default settings like force_destroy and attaches various policies, then defines individual bucket configurations. ```hcl terraform { source = "tfr:///terraform-aws-modules/s3-bucket/aws//wrappers" # Alternative source: # source = "git::git@github.com:terraform-aws-modules/terraform-aws-s3-bucket.git//wrappers?ref=master" } inputs = { defaults = { force_destroy = true attach_elb_log_delivery_policy = true attach_lb_log_delivery_policy = true attach_deny_insecure_transport_policy = true attach_require_latest_tls_policy = true } items = { bucket1 = { bucket = "my-random-bucket-1" } bucket2 = { bucket = "my-random-bucket-2" tags = { Secure = "probably" } } } } ``` -------------------------------- ### Terraform Bash: Migrate EC2 Instance State Commands Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/docs/UPGRADE-6.0.md These bash commands are used to migrate existing Terraform state for EC2 instances when upgrading to version 6.x of the AWS EC2 Instance module. They involve removing the old state and importing the resources into the new module structure, particularly for instances with additional EBS volumes. ```bash terraform state rm 'module.ec2_complete.aws_instance.this[0]' terraform import 'module.ec2_complete.aws_instance.this[0]' # Do the following for each additional EBS volume attached to the instance terraform import 'module.ec2_complete.aws_ebs_volume.this["/dev/sdf"]' terraform import 'module.ec2_complete.aws_volume_attachment.this["/dev/sdf"]' :: ``` -------------------------------- ### Connect to EC2 Instance via Session Manager Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/examples/session-manager/README.md Command to establish a Session Manager connection to the deployed EC2 instance. Requires the AWS CLI and Session Manager plugin to be installed. ```bash aws ssm start-session --target --region ``` -------------------------------- ### Terraform EC2 Instance Usage Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/examples/complete/README.md Demonstrates the basic commands required to initialize, plan, apply, and destroy Terraform configurations for AWS EC2 instances. These commands are essential for managing infrastructure as code. ```bash terraform init terraform plan terraform apply terraform destroy ``` -------------------------------- ### Terraform AWS EC2 Instance Resources Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/examples/complete/README.md Lists the AWS resources managed by this Terraform module. It includes EC2 capacity reservations, KMS keys, network interfaces, placement groups, and data sources for AMIs and availability zones. ```APIDOC Terraform AWS EC2 Instance Module Resources: Resources: - aws_ec2_capacity_reservation.open (resource) - aws_ec2_capacity_reservation.targeted (resource) - aws_kms_key.this (resource) - aws_network_interface.this (resource) - aws_placement_group.web (resource) Data Sources: - aws_ami.amazon_linux (data source) - aws_availability_zones.available (data source) ``` -------------------------------- ### Terraform AWS EC2 Instance Module API Documentation Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/examples/volume-attachment/README.md Detailed documentation for the Terraform module, outlining its requirements, providers, dependent modules, managed resources, and exposed outputs for an EC2 instance with EBS volume attachment. ```APIDOC Requirements: terraform: >= 1.0 aws: >= 4.66 Providers: aws: >= 4.66 Modules: ec2: Source: ../../ Version: n/a security_group: Source: terraform-aws-modules/security-group/aws Version: ~> 4.0 vpc: Source: terraform-aws-modules/vpc/aws Version: ~> 4.0 Resources: aws_ebs_volume.this: resource aws_volume_attachment.this: resource aws_ami.amazon_linux: data source aws_availability_zones.available: data source Inputs: No inputs. Outputs: ec2_arn: The ARN of the instance ec2_availability_zone: The availability zone of the created spot instance ec2_capacity_reservation_specification: Capacity reservation specification of the instance ec2_id: The ID of the instance ec2_instance_state: The state of the instance. One of: `pending`, `running`, `shutting-down`, `terminated`, `stopping`, `stopped` ec2_primary_network_interface_id: The ID of the instance's primary network interface ec2_private_dns: The private DNS name assigned to the instance. Can only be used inside the Amazon EC2, and only available if you've enabled DNS hostnames for your VPC ec2_public_dns: The public DNS name assigned to the instance. For EC2-VPC, this is only available if you've enabled DNS hostnames for your VPC ec2_public_ip: The public IP address assigned to the instance, if applicable. NOTE: If you are using an aws_eip with your instance, you should refer to the EIP's address directly and not use `public_ip` as this field will change after the EIP is attached ec2_tags_all: A map of tags assigned to the resource, including those inherited from the provider default_tags configuration block ``` -------------------------------- ### Terraform AWS EC2 Instance Requirements Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/examples/complete/README.md Specifies the minimum required versions for Terraform and the AWS provider to successfully use this module. Ensuring these dependencies are met is crucial for compatibility and functionality. ```APIDOC Terraform AWS EC2 Instance Module Requirements: - Terraform: >= 1.5.7 - AWS Provider: >= 6.0 ``` -------------------------------- ### Terraform AWS EC2 Instance Providers Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/examples/complete/README.md Lists the providers utilized by the Terraform AWS EC2 instance module, primarily the AWS provider. This section details the version constraints for the providers. ```APIDOC Terraform AWS EC2 Instance Module Providers: - AWS Provider: >= 6.0 ``` -------------------------------- ### Multiple EC2 Instance Creation with for_each Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/README.md Shows how to create multiple EC2 instances dynamically using the `for_each` meta-argument, allowing for scalable deployment of identical instances. ```hcl module "ec2_instance" { source = "terraform-aws-modules/ec2-instance/aws" for_each = toset(["one", "two", "three"]) name = "instance-${each.key}" instance_type = "t2.micro" key_name = "user1" monitoring = true subnet_id = "subnet-eddcdzz4" tags = { Terraform = "true" Environment = "dev" } } ``` -------------------------------- ### Terraform AWS EC2 Instance Modules Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/examples/complete/README.md Details the sub-modules used within the Terraform AWS EC2 instance configuration. This includes various EC2-specific configurations and common infrastructure modules like VPC and Security Group. ```APIDOC Terraform AWS EC2 Instance Module Dependencies: - ec2_complete: ../.. - ec2_disabled: ../.. - ec2_ignore_ami_changes: ../.. - ec2_metadata_options: ../.. - ec2_multiple: ../.. - ec2_network_interface: ../.. - ec2_open_capacity_reservation: ../.. - ec2_spot_instance: ../.. - ec2_t2_unlimited: ../.. - ec2_t3_unlimited: ../.. - ec2_targeted_capacity_reservation: ../.. - security_group: terraform-aws-modules/security-group/aws, ~> 5.0 - vpc: terraform-aws-modules/vpc/aws, ~> 6.0 ``` -------------------------------- ### Spot EC2 Instance Configuration Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/README.md Configures the module to create a Spot EC2 instance, specifying parameters like `create_spot_instance`, `spot_price`, and `spot_type`. ```hcl module "ec2_instance" { source = "terraform-aws-modules/ec2-instance/aws" name = "spot-instance" create_spot_instance = true spot_price = "0.60" spot_type = "persistent" instance_type = "t2.micro" key_name = "user1" monitoring = true subnet_id = "subnet-eddcdzz4" tags = { Terraform = "true" Environment = "dev" } } ``` -------------------------------- ### Terraform EC2 Instance Module Outputs Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/examples/complete/README.md This section lists and describes the outputs generated by the terraform-aws-modules/ec2-instance Terraform module. These outputs provide key information about the created EC2 instance, such as its ARN, ID, state, network configuration, and associated IAM roles. ```APIDOC Terraform EC2 Instance Module Outputs: - ec2_complete_arn: The ARN of the instance. - ec2_complete_availability_zone: The availability zone of the created instance. - ec2_complete_capacity_reservation_specification: Capacity reservation specification of the instance. - ec2_complete_ebs_block_device: EBS block device information. - ec2_complete_ephemeral_block_device: Ephemeral block device information. - ec2_complete_iam_instance_profile_arn: ARN assigned by AWS to the instance profile. - ec2_complete_iam_instance_profile_id: Instance profile's ID. - ec2_complete_iam_instance_profile_unique: Stable and unique string identifying the IAM instance profile. - ec2_complete_iam_role_arn: The Amazon Resource Name (ARN) specifying the IAM role. - ec2_complete_iam_role_name: The name of the IAM role. - ec2_complete_iam_role_unique_id: Stable and unique string identifying the IAM role. - ec2_complete_id: The ID of the instance. - ec2_complete_instance_state: The state of the instance. One of: `pending`, `running`, `shutting-down`, `terminated`, `stopping`, `stopped`. - ec2_complete_primary_network_interface_id: The ID of the instance's primary network interface. - ec2_complete_private_dns: The private DNS name assigned to the instance. Can only be used inside the Amazon EC2, and only available if you've enabled DNS hostnames for your VPC. - ec2_complete_public_dns: The public DNS name assigned to the instance. For EC2-VPC, this is only available if you've enabled DNS hostnames for your VPC. - ec2_complete_public_ip: The public IP address assigned to the instance, if applicable. NOTE: If you are using an aws_eip with your instance, you should refer to the EIP's address directly and not use `public_ip` as this field will change after the EIP is attached. - ec2_complete_root_block_device: Root block device information. - ec2_complete_tags_all: A map of tags assigned to the resource, including those inherited from the provider default_tags configuration block. - ec2_ignore_ami_changes_ami: The AMI of the instance (ignore_ami_changes = true). - ec2_multiple: The full output of the `ec2_module` module. - ec2_spot_instance_arn: The ARN of the instance. - ec2_spot_instance_capacity_reservation_specification: Capacity reservation specification of the instance. - ec2_spot_instance_id: The ID of the instance. ``` -------------------------------- ### Terraform AWS EC2 Instance Module Inputs Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/README.md This section details the input variables for the terraform-aws-ec2-instance module. It covers parameters for AMI selection, networking, instance type, storage, security, and more, enabling flexible EC2 instance deployment. ```APIDOC terraform-aws-ec2-instance Module Inputs: ami: string - Description: ID of AMI to use for the instance - Default: null - Required: no ami_ssm_parameter: string - Description: SSM parameter name for the AMI ID. For Amazon Linux AMI SSM parameters see [reference](https://docs.aws.amazon.com/systems-manager/latest/userguide/parameter-store-public-parameters-ami.html) - Default: "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64" - Required: no associate_public_ip_address: bool - Description: Whether to associate a public IP address with an instance in a VPC - Default: null - Required: no availability_zone: string - Description: AZ to start the instance in - Default: null - Required: no capacity_reservation_specification: object({ capacity_reservation_preference = optional(string) capacity_reservation_target = optional(object({ capacity_reservation_id = optional(string) capacity_reservation_resource_group_arn = optional(string) })) }) - Description: Describes an instance's Capacity Reservation targeting option - Default: null - Required: no cpu_credits: string - Description: The credit option for CPU usage (unlimited or standard) - Default: null - Required: no cpu_options: object({ amd_sev_snp = optional(string) core_count = optional(number) threads_per_core = optional(number) }) - Description: Defines CPU options to apply to the instance at launch time. - Default: null - Required: no create: bool - Description: Whether to create an instance - Default: true - Required: no create_eip: bool - Description: Determines whether a public EIP will be created and associated with the instance. - Default: false - Required: no create_iam_instance_profile: bool - Description: Determines whether an IAM instance profile is created or to use an existing IAM instance profile - Default: false - Required: no create_security_group: bool - Description: Determines whether a security group will be created - Default: true - Required: no create_spot_instance: bool - Description: Depicts if the instance is a spot instance - Default: false - Required: no disable_api_stop: bool - Description: If true, enables EC2 Instance Stop Protection - Default: null - Required: no disable_api_termination: bool - Description: If true, enables EC2 Instance Termination Protection - Default: null - Required: no ebs_optimized: bool - Description: If true, the launched EC2 instance will be EBS-optimized - Default: null - Required: no ebs_volumes: map(object({ encrypted = optional(bool) final_snapshot = optional(bool) iops = optional(number) kms_key_id = optional(string) multi_attach_enabled = optional(bool) outpost_arn = optional(string) size = optional(number) snapshot_id = optional(string) tags = optional(map(string), {}) throughput = optional(number) type = optional(string, "gp3") # Attachment device_name = optional(string) # Will fall back to use map key as device name force_detach = optional(bool) skip_destroy = optional(bool) stop_instance_before_detaching = optional(bool) })) - Description: Additional EBS volumes to attach to the instance - Default: null - Required: no eip_domain: string - Description: Indicates if this EIP is for use in VPC - Default: "vpc" - Required: no eip_tags: map(string) - Description: A map of additional tags to add to the eip - Default: {} - Required: no enable_primary_ipv6: bool - Description: Whether to assign a primary IPv6 Global Unicast Address (GUA) to the instance when launched in a dual-stack or IPv6-only subnet - Default: null - Required: no ``` -------------------------------- ### Terraform AWS EC2 Instance Module Outputs Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/README.md Lists the available outputs from the terraform-aws-ec2-instance module, detailing the purpose of each output value. ```APIDOC Terraform AWS EC2 Instance Module Outputs: - ami: AMI ID that was used to create the instance - arn: The ARN of the instance - availability_zone: The availability zone of the created instance - capacity_reservation_specification: Capacity reservation specification of the instance - ebs_block_device: EBS block device information - ebs_volumes: Map of EBS volumes created and their attributes - ephemeral_block_device: Ephemeral block device information - iam_instance_profile_arn: ARN assigned by AWS to the instance profile - iam_instance_profile_id: Instance profile's ID - iam_instance_profile_unique: Stable and unique string identifying the IAM instance profile - iam_role_arn: The Amazon Resource Name (ARN) specifying the IAM role - iam_role_name: The name of the IAM role - iam_role_unique_id: Stable and unique string identifying the IAM role - id: The ID of the instance - instance_state: The state of the instance - ipv6_addresses: The IPv6 address assigned to the instance, if applicable - outpost_arn: The ARN of the Outpost the instance is assigned to - password_data: Base-64 encoded encrypted password data for the instance. Useful for getting the administrator password for instances running Microsoft Windows. This attribute is only exported if `get_password_data` is true - primary_network_interface_id: The ID of the instance's primary network interface - private_dns: The private DNS name assigned to the instance. Can only be used inside the Amazon EC2, and only available if you've enabled DNS hostnames for your VPC - private_ip: The private IP address assigned to the instance - public_dns: The public DNS name assigned to the instance. For EC2-VPC, this is only available if you've enabled DNS hostnames for your VPC - public_ip: The public IP address assigned to the instance, if applicable. - root_block_device: Root block device information - spot_bid_status: The current bid status of the Spot Instance Request - spot_instance_id: The Instance ID (if any) that is currently fulfilling the Spot Instance request - spot_request_state: The current request state of the Spot Instance Request - tags_all: A map of tags assigned to the resource, including those inherited from the provider default_tags configuration block ``` -------------------------------- ### Spot Launch Group Configuration Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/README.md Assigns the Spot Instance to a launch group, ensuring that instances within the group launch and terminate together. Leaving it empty means individual instance management. ```APIDOC spot_launch_group: description: A launch group is a group of spot instances that launch together and terminate together. If left empty instances are launched and terminated individually type: string default: null ``` -------------------------------- ### Terraform AWS EC2 Instance Input Variables Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/README.md Defines the input variables for configuring an AWS EC2 instance using Terraform. This includes network interface settings, instance metadata, storage options, and general instance parameters. Each variable specifies its type, default value, and a description of its purpose. ```APIDOC Terraform EC2 Instance Module Inputs: - ipv6_addresses: Specify one or more IPv6 addresses from the range of the subnet to associate with the primary network interface. - Type: `list(string)` - Default: `null` - key_name: Key name of the Key Pair to use for the instance; which can be managed using the `aws_key_pair` resource. - Type: `string` - Default: `null` - launch_template: Specifies a Launch Template to configure the instance. Parameters configured on this resource will override the corresponding parameters in the Launch Template. - Type: `object({ id = optional(string) name = optional(string) version = optional(string) })` - Default: `null` - maintenance_options: The maintenance options for the instance. - Type: `object({ auto_recovery = optional(string) })` - Default: `null` - metadata_options: Customize the metadata options of the instance. - Type: `object({ http_endpoint = optional(string, "enabled") http_protocol_ipv6 = optional(string) http_put_response_hop_limit = optional(number, 1) http_tokens = optional(string, "required") instance_metadata_tags = optional(string) })` - Default: `{ "http_endpoint": "enabled", "http_put_response_hop_limit": 1, "http_tokens": "required" }` - monitoring: If true, the launched EC2 instance will have detailed monitoring enabled. - Type: `bool` - Default: `null` - name: Name to be used on EC2 instance created. - Type: `string` - Default: `""` - network_interface: Customize network interfaces to be attached at instance boot time. - Type: `map(object({ delete_on_termination = optional(bool) device_index = optional(number) # Will fall back to use map key as device index network_card_index = optional(number) network_interface_id = string }))` - Default: `null` - placement_group: The Placement Group to start the instance in. - Type: `string` - Default: `null` - placement_partition_number: Number of the partition the instance is in. Valid only if the `aws_placement_group` resource's `strategy` argument is set to `partition`. - Type: `number` - Default: `null` - private_dns_name_options: Customize the private DNS name options of the instance. - Type: `object({ enable_resource_name_dns_a_record = optional(bool) enable_resource_name_dns_aaaa_record = optional(bool) hostname_type = optional(string) })` - Default: `null` - private_ip: Private IP address to associate with the instance in a VPC. - Type: `string` - Default: `null` - putin_khuylo: Do you agree that Putin doesn't respect Ukrainian sovereignty and territorial integrity? More info: https://en.wikipedia.org/wiki/Putin_khuylo! - Type: `bool` - Default: `true` - region: Region where the resource(s) will be managed. Defaults to the Region set in the provider configuration. - Type: `string` - Default: `null` - root_block_device: Customize details about the root block device of the instance. See Block Devices below for details. - Type: `object({ delete_on_termination = optional(bool) encrypted = optional(bool) iops = optional(number) kms_key_id = optional(string) tags = optional(map(string)) throughput = optional(number) size = optional(number) type = optional(string) })` - Default: `null` - secondary_private_ips: A list of secondary private IPv4 addresses to assign to the instance's primary network interface (eth0) in a VPC. Can only be assigned to the primary network interface (eth0) attached at instance creation, not a pre-existing network interface i.e. referenced in a `network_interface block`. - Type: `list(string)` - Default: `null` ``` -------------------------------- ### Single EC2 Instance Creation Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/README.md Demonstrates the basic usage of the module to create a single EC2 instance with essential parameters like name, instance type, key name, and subnet ID. ```hcl module "ec2_instance" { source = "terraform-aws-modules/ec2-instance/aws" name = "single-instance" instance_type = "t2.micro" key_name = "user1" monitoring = true subnet_id = "subnet-eddcdzz4" tags = { Terraform = "true" Environment = "dev" } } ``` -------------------------------- ### Terraform AWS EC2 Instance Module Inputs Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/README.md This entry documents the input variables for the terraform-aws-ec2-instance module. It covers parameters for instance tenancy, user data, volume tagging, security group association, and timeout configurations. ```APIDOC Terraform AWS EC2 Instance Module Inputs: - tenancy: The tenancy of the instance (if the instance is running in a VPC). Available values: default, dedicated, host - Type: `string` - Default: `null` - timeouts: Define maximum timeout for creating, updating, and deleting EC2 instance resources - Type: `map(string)` - Default: `{}` - user_data: The user data to provide when launching the instance. Do not pass gzip-compressed data via this argument; see user_data_base64 instead - Type: `string` - Default: `null` - user_data_base64: Can be used instead of user_data to pass base64-encoded binary data directly. Use this instead of user_data whenever the value is not a valid UTF-8 string. For example, gzip-encoded user data must be base64-encoded and passed via this argument to avoid corruption - Type: `string` - Default: `null` - user_data_replace_on_change: When used in combination with user_data or user_data_base64 will trigger a destroy and recreate when set to true. Defaults to false if not set - Type: `bool` - Default: `null` - volume_tags: A mapping of tags to assign to the devices created by the instance at launch time - Type: `map(string)` - Default: `{}` - vpc_security_group_ids: A list of security group IDs to associate with - Type: `list(string)` - Default: `[]` ``` -------------------------------- ### EC2 Instance Tags All Output Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/examples/complete/README.md Returns a map of all tags assigned to the EC2 resource, including those inherited from the provider's default_tags configuration block. This is useful for resource organization and management. ```APIDOC ec2_spot_instance_tags_all: description: A map of tags assigned to the resource, including those inherited from the provider default_tags configuration block ec2_t2_unlimited_tags_all: description: A map of tags assigned to the resource, including those inherited from the provider default_tags configuration block ec2_t3_unlimited_tags_all: description: A map of tags assigned to the resource, including those inherited from the provider default_tags configuration block ``` -------------------------------- ### Terraform Usage for EC2 Instance Wrapper Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/wrappers/README.md Shows how to integrate the EC2 instance module wrapper within a Terraform configuration. It outlines the module source and the structure for defining default and item-specific inputs for managing multiple EC2 instances. ```hcl module "wrapper" { source = "terraform-aws-modules/ec2-instance/aws//wrappers" defaults = { # Default values create = true tags = { Terraform = "true" Environment = "dev" } } items = { my-item = { # omitted... can be any argument supported by the module } my-second-item = { # omitted... can be any argument supported by the module } # omitted... } } ``` -------------------------------- ### Terraform EC2 Instance Configuration (v3.x) with for_each Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/UPGRADE-3.0.md This HCL code snippet illustrates the updated configuration for AWS EC2 instances using version 3.x of the `terraform-aws-modules/ec2-instance` module. It leverages `for_each` for multiple instances and demonstrates how to manage `subnet_id` and naming conventions after the removal of `instance_count` and `subnet_ids`. ```HCL locals { num_suffix_format = "-%d" multiple_instances = { 0 = { num_suffix = 1 instance_type = "c5.large" subnet_id = element(module.vpc.private_subnets, 0) } 1 = { num_suffix = 2 instance_type = "c5.large" subnet_id = element(module.vpc.private_subnets, 1) } 2 = { num_suffix = 3 instance_type = "c5.large" subnet_id = element(module.vpc.private_subnets, 2) } } } module "ec2_upgrade" { source = "../../" for_each = local.multiple_instances name = format("%s${local.num_suffix_format}", local.name, each.value.num_suffix) ami = data.aws_ami.amazon_linux.id instance_type = each.value.instance_type subnet_id = each.value.subnet_id vpc_security_group_ids = [module.security_group.security_group_id] associate_public_ip_address = true tags = local.tags } ``` -------------------------------- ### Terragrunt Usage for EC2 Instance Wrapper Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/wrappers/README.md Demonstrates how to configure the EC2 instance module wrapper using Terragrunt. It specifies the module source and defines default input values and specific items to manage multiple EC2 instances. ```hcl terraform { source = "tfr:///terraform-aws-modules/ec2-instance/aws//wrappers" # Alternative source: # source = "git::git@github.com:terraform-aws-modules/terraform-aws-ec2-instance.git//wrappers?ref=master" } inputs = { defaults = { # Default values create = true tags = { Terraform = "true" Environment = "dev" } } items = { my-item = { # omitted... can be any argument supported by the module } my-second-item = { # omitted... can be any argument supported by the module } # omitted... } } ``` -------------------------------- ### EC2 Instance Capacity Reservation Specification Output Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/examples/complete/README.md Details the capacity reservation specification for the EC2 instance. This indicates how the instance is associated with capacity reservations. ```APIDOC ec2_t2_unlimited_capacity_reservation_specification: description: Capacity reservation specification of the instance ec2_t3_unlimited_capacity_reservation_specification: description: Capacity reservation specification of the instance ``` -------------------------------- ### EC2 Instance State Output Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/examples/complete/README.md Provides the current state of the EC2 instance. This attribute is common across different instance types and reflects the operational status of the instance. ```APIDOC ec2_spot_instance_instance_state: description: The state of the instance. One of: `pending`, `running`, `shutting-down`, `terminated`, `stopping`, `stopped` ec2_t2_unlimited_instance_state: description: The state of the instance. One of: `pending`, `running`, `shutting-down`, `terminated`, `stopping`, `stopped` ec2_t3_unlimited_instance_state: description: The state of the instance. One of: `pending`, `running`, `shutting-down`, `terminated`, `stopping`, `stopped` ``` -------------------------------- ### Terraform State Migration Commands for EC2 Module Upgrade Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/UPGRADE-3.0.md These Bash commands are used to migrate the Terraform state when upgrading the `terraform-aws-modules/ec2-instance` module from v2.x to v3.x. They move existing `aws_instance` resources from the old list-based indexing to the new `for_each` map-based indexing, preventing resource recreation. ```bash terraform state mv 'module.ec2_upgrade.aws_instance.this[0]' 'module.ec2_upgrade["0"].aws_instance.this[0]' terraform state mv 'module.ec2_upgrade.aws_instance.this[1]' 'module.ec2_upgrade["1"].aws_instance.this[0]' terraform state mv 'module.ec2_upgrade.aws_instance.this[2]' 'module.ec2_upgrade["2"].aws_instance.this[0]' ``` -------------------------------- ### Terraform EC2 Instance Module Inputs Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/README.md This section details the configurable input variables for the Terraform AWS EC2 Instance module. Each variable includes its purpose, data type, default value, and any specific constraints or behaviors. ```APIDOC Terraform EC2 Instance Module Inputs: - enable_volume_tags: description: "Whether to enable volume tags (if enabled it conflicts with root_block_device tags)" type: "bool" default: "true" - enclave_options_enabled: description: "Whether Nitro Enclaves will be enabled on the instance. Defaults to `false`" type: "bool" default: "null" - ephemeral_block_device: description: "Customize Ephemeral (also known as Instance Store) volumes on the instance" type: "map(object({ device_name = string no_device = optional(bool) virtual_name = optional(string) }))" default: "null" - get_password_data: description: "If true, wait for password data to become available and retrieve it" type: "bool" default: "null" - hibernation: description: "If true, the launched EC2 instance will support hibernation" type: "bool" default: "null" - host_id: description: "ID of a dedicated host that the instance will be assigned to. Use when an instance is to be launched on a specific dedicated host" type: "string" default: "null" - host_resource_group_arn: description: "ARN of the host resource group in which to launch the instances. If you specify an ARN, omit the `tenancy` parameter or set it to `host`" type: "string" default: "null" - iam_instance_profile: description: "IAM Instance Profile to launch the instance with. Specified as the name of the Instance Profile" type: "string" default: "null" - iam_role_description: description: "Description of the role" type: "string" default: "null" - iam_role_name: description: "Name to use on IAM role created" type: "string" default: "null" - iam_role_path: description: "IAM role path" type: "string" default: "null" - iam_role_permissions_boundary: description: "ARN of the policy that is used to set the permissions boundary for the IAM role" type: "string" default: "null" - iam_role_policies: description: "Policies attached to the IAM role" type: "map(string)" default: "{}" - iam_role_tags: description: "A map of additional tags to add to the IAM role/profile created" type: "map(string)" default: "{}" - iam_role_use_name_prefix: description: "Determines whether the IAM role name (`iam_role_name` or `name`) is used as a prefix" type: "bool" default: "true" - ignore_ami_changes: description: "Whether changes to the AMI ID changes should be ignored by Terraform. Note - changing this value will result in the replacement of the instance" type: "bool" default: "false" - instance_initiated_shutdown_behavior: description: "Shutdown behavior for the instance. Amazon defaults this to stop for EBS-backed instances and terminate for instance-store instances. Cannot be set on instance-store instance" type: "string" default: "null" - instance_market_options: description: "The market (purchasing) option for the instance. If set, overrides the `create_spot_instance` variable" type: "object({ market_type = optional(string) spot_options = optional(object({ instance_interruption_behavior = optional(string) max_price = optional(string) spot_instance_type = optional(string) valid_until = optional(string) })) })" default: "null" - instance_tags: description: "Additional tags for the instance" type: "map(string)" default: "{}" - instance_type: description: "The type of instance to start" type: "string" default: "t3.micro" - 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" type: "number" default: "null" ``` -------------------------------- ### Spot Price Configuration Source: https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/master/README.md Sets the maximum price to bid for the Spot Instance. If not specified, it defaults to the on-demand price for the instance type. ```APIDOC spot_price: description: The maximum price to request on the spot market. Defaults to on-demand price type: string default: null ```