Help for the VSCode editor.
-
Navigate to indicated directory in Explorer pane
-
Which of the below resources is not part of this configuration?
We can attack this two ways
- Use
terraform show
to inspect the created resources. There is a lot of output! - Inspect the resource.
- Note there is a
for_each
on thename
variable, so one EC2 instance will be created for each name - Inspect
variables.tf
to find the values forname
. The answer not in this list is the correct one.
- Note there is a
- Use
-
What is the name of the ssh key which is used by all of these instances?
Inspect
variables.tf
and note the default value for the key name. -
Is the key pair resource created by this terraform configuration?
Inspect the resources in
main.tf
. Do you see a likely candidate for akey_pair
resource? -
Information only.
-
We have another EC2 instance created called jade-mw using the the AWS CLI.
Using the AWS CLI inspect this EC2 instance and find the ID that is created by it.EC2 instances output a lot of data when described using the CLI. Therefore make use of the specifications given. The AMI id is the best to use here, as it is different from the one used in
variables.tf
to create the others, meaning we are more likely to get only the instance we want returned from the query.In filter syntax,
image-id
is the key name to search by AMI (documentation).aws ec2 describe-instances \ --endpoint http://aws:4566 \ --filters Name=image-id,Values=ami-082b3eca746b12a89
We get a big block of JSON, but if we go to the end of it, we can see in the
Tags
that it isjade-mw
and therefore the one we need.Scroll back up through the output to find the
InstanceId
which is near the beginning -
Let's manage this instance called jade-mw with Terraform! First, create an empty resource block for this instance in the main.tf file in the configuration directory /root/terraform-projects/project-jade
Edit the
main.tf
file to add the empty resource block for the newaws_instance
resourceReveal
resource "aws_instance" "jade-mw" { }
-
Now, import this instance into the terraform state.
Refer to the import documentation for aws_instance
You will need the
InstanceId
you discovered in Q6. For this example let's assume it isi-39773b28caf25392f
but it may be different for you!cd /root/terraform-projects/project-jade terraform import aws_instance.jade-mw i-39773b28caf25392f
-
Great! We are nearly there. What would happen if we run terraform apply?
Run
terraform plan
and inspect the errors -
Let us fix that now. Complete the resource block for jade-mw. Inspect the state to make sure all the arguments used to create this resource are defined in the resource block.
To solve this, we need to supply the minimum set or arguments that will satisfy the resource state, and we get the values by inspecting what has been imported into the state file. We also have a good idea of the minimum requirements by inspecting the
ruby
instance we already have in the configuration.From the output of
terraform plan
note there areMissing required arguments
. We start here.- Load
terraform.tfstate
into the editor and find thejade-mw
resource. - We are definitely going to need
ami
,key_name
andinstance_type
so find these values in the state file and add the appropriate arguments to ourt empty resource block - Run
terraform plan
again and note what happens. Are there errors? If not, is it going to replace the resource, or even modify it? - You should now see that it will modify it, due to the
Name
tag going fromjade-mw
to empty. Add atags
block into the resource with the correct entry. - Run an apply
Your final resource should look like this
Reveal
The AMI id generated by the question will be different each time.
Note that we must also supply an empty block for
timeouts
to prevent the removal as shown byterraform plan
if you do not.resource "aws_instance" "jade-mw" { ami = "ami-082b3eca746b12a89" instance_type = "t2.large" key_name = "jade" tags = { Name = "jade-mw" } timeouts {} }
- Load