Link Search Menu Expand Document

AMIs

Lesson 5

An Amazon Machine Image (AMI for short) is the foundation for your EC2 instance. You can think of an AMI as the blueprint for you EC2 instance - it tells the EC2 instance what to look like when it’s created.

Amazon has a host of different AMIs, from Linux to Microsoft to Wordpress. When you choose an AMI type, HAVE MATT FILL OUT THIS PORTION.

AMI ids are region-specific, meaning the id of any one AMI will change from region to region. Some AMIs are only available in a single region, or are available in all but one. If you ever get an error about your AMI not being found, the first thing to check is that your AMI id is correct for the region you are launching your instance into.

Table of contents

  1. AMIs
  2. Searching for AMIs in AWS
  3. EC2 Wordpress site with terraform
    1. Edit your terraform project files
    2. Deploy your Wordpress site
  4. Next Steps
    1. Redo the lesson (optional)
    2. Move On

Searching for AMIs in AWS

Sometimes clients will come to you with their own private AMI or want you to create an AMI for them. Other times, though, they just need an AMI AWS already provides. In the following example, our client needs a Wordpress AMI as the base image for their EC2 instance. Follow along to learn how to search for AMIs in AWS.

Sometimes AWS AMIs need to be subscribed to before you can start using them. In these cases, before using terraform, you’ll need to manually launch an AMI in the AWS Console (or AWS CLI). You can then delete the instance, and every time from then on you use that AMI in terraform you’ll be good to go. You are going to subscribe to a Wordpress AMI by launching an instance in the AWS Console.

  1. Log in to the AWS Console.
  2. Navigate to the EC2 Console (search for EC2 and find the dashboard) and click on Instances.
  3. Click Launch instances in the upper right-hand corner.
  4. You will be taken to a page that says, Choose an Amazon Machine Image (AMI).
  5. In the search box, type in wordpress.
  6. Click on AWS Marketplace in the left-hand navigation pane.
  7. Choose the first AMI listed, WordPress Certified by Bitnami and Automattic by Bitnami by clicking Select.
  8. Click Continue on the pop-up.
  9. Choose t2.micro as the instance type, then click the blue, Review and launch button.
  10. Click Launch, choose your key-pair from the dropdown (the one you’ve been using this whole time), check the acknowledgement box, and click Launch instances.
  11. On the next page, click the instances link to navigate to your newly created instance, then click the link again on the next page.
  12. Under the Details tab, you should see the AMI ID listed. Copy it to your clipboard (or a notepad on your computer). You’ll need it in the next section.

wordpress ami

That’s it! In this example, we searched for a Wordpress AMI, but you can search for any AMI you or a client might need in the future. The most common AMI types are the ones we have been using up until this point: AWS Amazon Linux 2, AWS Amazon Ubuntu, and a few more. (We’ve been using AWS Amazon Linux 2).

You can now delete the EC2 instance by clicking on the Instance state dropdown in the upper-right. Choose Terminate instance from the drop-down.

EC2 Wordpress site with terraform

Now that you have subscribed to a Wordpress AMI and you have the ami id for it, let’s launch an EC2 instance using terraform with the new AMI.

Edit your terraform project files

  1. Open up your code editor to the ec2-terraform project.
  2. For this project, all we need to change is the ami id. Because we are using variables, this is really simple to do! Open up the terraform.tfvars file (where we give variables value) and for ami, replace the current value with the value you copied from your Wordpress AMI above.

    Note: If you are using us-east-1 like we’ve suggested, this AMI id is: "ami-0c00935023a833df1". If you are deploying resources to a different region, you will need to search for the ami id using the steps above in the region you are deploying to.

  3. Now that we have an official Wordpress AMI, we no longer need the user data we input to turn our basic image into a web server.
    • In your ec2.tf file, you will need to comment out your user_data parameter.
    • Do NOT delete this parameter! You will need it again later.
    • Save your file.

Note: To comment out a block of code, select the code you want (as if you were going to copy it) then press Ctrl + / (Windows/Linux) or Cmd + / (MacOS). This will put double backslashes (//) in front of those lines of code. Terraform will ignore all code with these slashes and know not to run it.

wordpress ami 1

Deploy your Wordpress site

Run terraform apply in your terminal (make sure you’re in the correct directory by running the command pwd first, then change into the ec2-terraform directory if needed) and wait for terraform to deploy your instance!

terraform apply wordpress

Once terraform is finished, go find your instance in the AWS Console and check that the ami id matches what you put. Then, copy the Public IP address and paste it into a browser tab. You should now see a basic Wordpress site instead of our previous Hello from Learn the Cloud! web server from before.

terraform apply wordpress

When you are finished marvelling at your genius, go ahead and run terraform destroy in the terminal to delete your instance.

Next Steps

You’ve now searched for an AMI in the AWS Marketplace, subscribed to a Wordpress image, and launched an instance of a Wordpress website. How you feel about the process determines what you should do next.

Redo the lesson (optional)

If you don’t feel as comfortable as you’d like with the above lesson, you can repeat it by:

  1. Searching AWS Marketplace for a different AMI, then following the steps above as you did before OR
  2. You can use this ami-id: ami-0947d2ba12ee1ff75 and uncomment (run the same command you used to comment it out) the user_data parameter.

Changing #2 will get you back to where you were before you started this lesson, so you can repeat it from the beginning.

Move On

Now that you feel comfortable finding AWS AMIs and launching different instances via terraform, you can move on to the next lesson, Instance Types.