Skip to content

Latest commit

 

History

History
128 lines (83 loc) · 5.03 KB

File metadata and controls

128 lines (83 loc) · 5.03 KB

Lab: Terraform Import

Help for the VSCode editor.

  1. Navigate to indicated directory in Explorer pane

  2. Which of the below resources is not part of this configuration?

    We can attack this two ways

    1. Use terraform show to inspect the created resources. There is a lot of output!
    2. Inspect the resource.
      1. Note there is a for_each on the name variable, so one EC2 instance will be created for each name
      2. Inspect variables.tf to find the values for name. The answer not in this list is the correct one.
  3. 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.

  4. Is the key pair resource created by this terraform configuration?

    Inspect the resources in main.tf. Do you see a likely candidate for a key_pair resource?

  5. Information only.

  6. 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 is jade-mw and therefore the one we need.

    Scroll back up through the output to find the InstanceId which is near the beginning

  7. 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 new aws_instance resource

    Reveal
    resource "aws_instance" "jade-mw" {
    }
    
  8. 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 is i-39773b28caf25392f but it may be different for you!

    cd /root/terraform-projects/project-jade
    terraform import aws_instance.jade-mw i-39773b28caf25392f
  9. Great! We are nearly there. What would happen if we run terraform apply?

    Run terraform plan and inspect the errors

  10. 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 are Missing required arguments. We start here.

    1. Load terraform.tfstate into the editor and find the jade-mw resource.
    2. We are definitely going to need ami, key_name and instance_type so find these values in the state file and add the appropriate arguments to ourt empty resource block
    3. Run terraform plan again and note what happens. Are there errors? If not, is it going to replace the resource, or even modify it?
    4. You should now see that it will modify it, due to the Name tag going from jade-mw to empty. Add a tags block into the resource with the correct entry.
    5. 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 by terraform 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 {}
    }