119 lines
5.0 KiB
Markdown
119 lines
5.0 KiB
Markdown
# Assignment
|
|
|
|
During the migration process, the Nautilus DevOps team created several EC2 instances in different regions. They are currently in the process of identifying the correct resources and utilization and are making continuous changes to ensure optimal resource utilization. Recently, they discovered that one of the EC2 instances was underutilized, prompting them to decide to change the instance type. Please make sure the Status check is completed (if it's still in Initializing state) before making any changes to the instance.
|
|
|
|
Change the instance type from t2.micro to t2.nano for nautilus-ec2 instance using terraform.
|
|
|
|
Make sure the EC2 instance nautilus-ec2 is in running state after the change.
|
|
|
|
The Terraform working directory is /home/bob/terraform. Update the main.tf file (do not create a separate .tf file) to change the instance type.
|
|
|
|
Note: Right-click under the EXPLORER section in VS Code and select Open in Integrated Terminal to launch the terminal.
|
|
|
|
# Solution
|
|
|
|
# Change EC2 Instance Type — `nautilus-ec2` (t2.micro → t2.nano)
|
|
|
|
This is an **update** to an already Terraform-managed instance, not a new resource.
|
|
The instance definition already exists in `main.tf` from when it was created; the task
|
|
is to change its `instance_type` and re-apply.
|
|
|
|
## The change
|
|
|
|
Find the `aws_instance` block for `nautilus-ec2` in the existing `main.tf` and change
|
|
the single `instance_type` line:
|
|
|
|
```diff
|
|
resource "aws_instance" "nautilus_ec2" {
|
|
ami = "ami-xxxxxxxxxxxxxxxxx"
|
|
- instance_type = "t2.micro"
|
|
+ instance_type = "t2.nano"
|
|
# ... key_name, vpc_security_group_ids, tags, etc. unchanged ...
|
|
|
|
tags = {
|
|
Name = "nautilus-ec2"
|
|
}
|
|
}
|
|
```
|
|
|
|
For reference, the full block looks like this after the edit (leave every other
|
|
argument exactly as it already is — only `instance_type` changes):
|
|
|
|
```hcl
|
|
resource "aws_instance" "nautilus_ec2" {
|
|
ami = "ami-xxxxxxxxxxxxxxxxx" # keep the existing value
|
|
instance_type = "t2.nano" # was t2.micro
|
|
|
|
tags = {
|
|
Name = "nautilus-ec2"
|
|
}
|
|
}
|
|
```
|
|
|
|
> Do **not** rewrite the block from scratch or change the resource's Terraform name,
|
|
> AMI, key, or security groups. Altering those could force a **replacement** (destroy +
|
|
> recreate) instead of an in-place modify. Only the one line changes.
|
|
|
|
## How to run
|
|
|
|
```bash
|
|
cd /home/bob/terraform
|
|
|
|
# 1) Make sure status checks are done (not "Initializing") BEFORE changing anything.
|
|
aws ec2 describe-instance-status \
|
|
--filters Name=tag:Name,Values=nautilus-ec2 \
|
|
--query 'InstanceStatuses[0].{Instance:InstanceStatus.Status,System:SystemStatus.Status}'
|
|
# Wait until both report "ok" (2/2 checks passed). If it shows "initializing", wait and re-run.
|
|
|
|
# 2) Apply the instance-type change.
|
|
terraform plan # should show ~ instance_type "t2.micro" -> "t2.nano" (update in place)
|
|
terraform apply -auto-approve
|
|
```
|
|
|
|
## How it works
|
|
|
|
### Why it's an in-place update, not a replacement
|
|
|
|
`instance_type` is a mutable attribute in EC2. Terraform's AWS provider changes it
|
|
**without destroying the instance**: it stops the instance, issues
|
|
`ModifyInstanceAttribute` to set the new type, then starts it again. You'll see this
|
|
in the plan as `~ update in-place`, not `-/+ destroy and recreate`. The instance keeps
|
|
its ID, EBS volumes, private IP, and (if it has one) EIP association.
|
|
|
|
`t2.micro` and `t2.nano` are in the same family and share the same virtualization and
|
|
architecture, so the modify is fully compatible — no AMI or platform mismatch.
|
|
|
|
### Why wait for the status check first
|
|
|
|
The task calls this out specifically. Changing instance type requires the instance to
|
|
be **stopped** momentarily. If the instance is still `Initializing` (status checks
|
|
haven't reached 2/2), issuing a stop/modify mid-initialization can fail or leave the
|
|
instance in an inconsistent state. Waiting until both the system and instance status
|
|
checks report `ok` guarantees a clean stop → modify → start cycle.
|
|
|
|
### Why it ends up `running` (requirement #2)
|
|
|
|
After a type change, the AWS provider automatically starts the instance back up and
|
|
waits for it to reach the `running` state before `apply` returns. No extra
|
|
configuration is needed — an `aws_instance` resource's desired state is running, so
|
|
Terraform restores that after the modify. When `apply` completes, the instance is
|
|
running as `t2.nano`.
|
|
|
|
### If the instance isn't in Terraform state
|
|
|
|
The task says *update* `main.tf`, which means the instance is already managed here. If
|
|
for some reason `terraform plan` reports it wants to **create** `nautilus-ec2` (i.e.
|
|
it's not in state), stop — that means the instance was made outside Terraform and you'd
|
|
need to `terraform import aws_instance.nautilus_ec2 <instance-id>` first, then apply the
|
|
type change. In the normal lab flow this isn't necessary.
|
|
|
|
## Verify
|
|
|
|
```bash
|
|
aws ec2 describe-instances \
|
|
--filters Name=tag:Name,Values=nautilus-ec2 \
|
|
--query 'Reservations[0].Instances[0].{Id:InstanceId,Type:InstanceType,State:State.Name}'
|
|
```
|
|
|
|
Expected — the same instance ID as before, `InstanceType: t2.nano`, and
|
|
`State: running`. |