|
| 1 | +# Upgrade from v5.x to v6.x |
| 2 | + |
| 3 | +If you have any questions regarding this upgrade process, please consult the `examples` directory: |
| 4 | + |
| 5 | +- [Complete](https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/tree/master/examples/complete) |
| 6 | + |
| 7 | +If you find a bug, please open an issue with supporting configuration to reproduce. |
| 8 | + |
| 9 | +## List of backwards incompatible changes |
| 10 | + |
| 11 | +- Terraform v1.10.0 is now minimum supported version |
| 12 | +- AWS provider v6.0.0 is now minimum supported version |
| 13 | +- The default value for `ami_ssm_parameter` was changed from `"/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"` to `"/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64"`. AL2 is approaching end of life. |
| 14 | + |
| 15 | +## Additional changes |
| 16 | + |
| 17 | +### Added |
| 18 | + |
| 19 | +- Support for creating a security group within the module; this is now the default behavior and can be disabled by setting `create_security_group = false`. |
| 20 | +- Support for `region` parameter to specify the AWS region for the resources created if different from the provider region. |
| 21 | +- Support for tagging spot instances |
| 22 | + |
| 23 | +### Modified |
| 24 | + |
| 25 | +- Variable definitions now contain detailed `object` types in place of the previously used `any` type. |
| 26 | +- Inline `ebs_block_device` argument has been removed in favor of `ebs_volumes` which is a map of EBS volumes created through `aws_ebs_volume` and `aws_ebs_volume_attachment` resources. This provides the same API as before, but allows for more flexibility without generating diffs when adding or removing EBS volumes as well as unintended changes to the volumes. |
| 27 | +- Correct tag precedence ordering (least specific to most specific) |
| 28 | + |
| 29 | +### Removed |
| 30 | + |
| 31 | +- The `volume-attachment` example has been removed since the module has been updated to use the corrected form of EBS volume creation and attachment (tl;dr - example is no longer useful). |
| 32 | + |
| 33 | +### Variable and output changes |
| 34 | + |
| 35 | +1. Removed variables: |
| 36 | + |
| 37 | + - `cpu_core_count` - removed from provider `v6.x` |
| 38 | + - `cpu_threads_per_core` - removed from provider `v6.x` |
| 39 | + |
| 40 | +2. Renamed variables: |
| 41 | + |
| 42 | + - `ebs_block_device` -> `ebs_volumes` |
| 43 | + |
| 44 | +3. Added variables: |
| 45 | + |
| 46 | + - `region` |
| 47 | + - `enable_primary_ipv6` |
| 48 | + - `host_resource_group_arn` |
| 49 | + - `instance_market_options` |
| 50 | + - `placement_partition_number` |
| 51 | + - `create_security_group` |
| 52 | + - `security_group_name` |
| 53 | + - `security_group_use_name_prefix` |
| 54 | + - `security_group_description` |
| 55 | + - `security_group_vpc_id` |
| 56 | + - `security_group_tags` |
| 57 | + - `security_group_egress_rules` |
| 58 | + - `security_group_ingress_rules` |
| 59 | + |
| 60 | +4. Removed outputs: |
| 61 | + |
| 62 | + - None |
| 63 | + |
| 64 | +5. Renamed outputs: |
| 65 | + |
| 66 | + - None |
| 67 | + |
| 68 | +6. Added outputs: |
| 69 | + |
| 70 | + - `ebs_volumes` |
| 71 | + |
| 72 | +## Upgrade State Migrations |
| 73 | + |
| 74 | +### Before 5.x Example |
| 75 | + |
| 76 | +```hcl |
| 77 | +module "ec2_upgrade" { |
| 78 | + source = "terraform-aws-modules/ec2-instance/aws" |
| 79 | + version = "5.8.0" |
| 80 | +
|
| 81 | + # Truncated for brevity, only relevant module API changes are shown ... |
| 82 | +
|
| 83 | + root_block_device = [ |
| 84 | + { |
| 85 | + encrypted = true |
| 86 | + volume_size = 50 |
| 87 | + volume_type = "gp3" |
| 88 | + throughput = 200 |
| 89 | + tags = { |
| 90 | + Name = "my-root-block" |
| 91 | + } |
| 92 | + }, |
| 93 | + ] |
| 94 | +
|
| 95 | + ebs_block_device = [ |
| 96 | + { |
| 97 | + device_name = "/dev/sdf" |
| 98 | + encrypted = true |
| 99 | + volume_size = 5 |
| 100 | + volume_type = "gp3" |
| 101 | + throughput = 200 |
| 102 | + tags = { |
| 103 | + MountPoint = "/mnt/data" |
| 104 | + } |
| 105 | + } |
| 106 | + ] |
| 107 | +
|
| 108 | + network_interface = [ |
| 109 | + { |
| 110 | + device_index = 0 |
| 111 | + network_interface_id = aws_network_interface.this.id |
| 112 | + delete_on_termination = false |
| 113 | + } |
| 114 | + ] |
| 115 | +
|
| 116 | + tags = local.tags |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +### After 6.x Example |
| 121 | + |
| 122 | +```hcl |
| 123 | +module "ec2_upgrade" { |
| 124 | + source = "terraform-aws-modules/ec2-instance/aws" |
| 125 | + version = "6.0.0" |
| 126 | +
|
| 127 | + # Truncated for brevity, only relevant module API changes are shown ... |
| 128 | +
|
| 129 | + # There can only be one root block device, so the wrapping list is removed |
| 130 | + root_block_device = { |
| 131 | + encrypted = true |
| 132 | + size = 50 # Was `volume_size` |
| 133 | + type = "gp3" # Was `volume_type` |
| 134 | + throughput = 200 |
| 135 | + tags = { |
| 136 | + Name = "my-root-block" |
| 137 | + } |
| 138 | + } |
| 139 | +
|
| 140 | + # Now a map of EBS volumes is used instead of a list |
| 141 | + ebs_volumes = { |
| 142 | + # The device_name can be the key of the map, or set by `device_name` attribute |
| 143 | + "/dev/sdf" = { |
| 144 | + encrypted = true |
| 145 | + size = 5 # Was `volume_size` |
| 146 | + type = "gp3" # Was `volume_type`, `gp3` is now the default |
| 147 | + throughput = 200 |
| 148 | + tags = { |
| 149 | + MountPoint = "/mnt/data" |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +
|
| 154 | + # Now a map of network interfaces is used instead of a list |
| 155 | + network_interface = { |
| 156 | + # The device_index can be the key of the map, or set by `device_index` attribute |
| 157 | + 0 = { |
| 158 | + network_interface_id = aws_network_interface.this.id |
| 159 | + delete_on_termination = false |
| 160 | + } |
| 161 | + } |
| 162 | +
|
| 163 | + tags = local.tags |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +To migrate from the `v5.x` version to `v6.x` version example shown above, the following state move commands can be performed to maintain the current resources without modification: |
| 168 | + |
| 169 | +> [!NOTE] |
| 170 | +> State move commands should only be required on instances that have additional EBS volumes attached to them. |
| 171 | +
|
| 172 | +```bash |
| 173 | +terraform state rm 'module.ec2_complete.aws_instance.this[0]' |
| 174 | +terraform import 'module.ec2_complete.aws_instance.this[0]' <INSTANCE_ID> |
| 175 | + |
| 176 | +# Do the following for each additional EBS volume attached to the instance |
| 177 | +terraform import 'module.ec2_complete.aws_ebs_volume.this["/dev/sdf"]' <VOLUME_ID> |
| 178 | +terraform import 'module.ec2_complete.aws_volume_attachment.this["/dev/sdf"]' <DEVICE_NAME>:<VOLUME_ID>:<INSTANCE_ID> |
| 179 | +``` |
| 180 | + |
| 181 | +> [!TIP] |
| 182 | +> If you encounter a situation where Terraform wants to recreate the instance due to user data changes, you can set the `user_data_replace_on_change` variable to `false` to prevent this behavior. |
| 183 | +> This is related to https://github.com/hashicorp/terraform-provider-aws/issues/5011 |
0 commit comments