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
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.
- Log in to the AWS Console.
- Navigate to the EC2 Console (search for EC2 and find the dashboard) and click on
Instances
. - Click
Launch instances
in the upper right-hand corner. - You will be taken to a page that says,
Choose an Amazon Machine Image (AMI)
. - In the search box, type in
wordpress
. - Click on
AWS Marketplace
in the left-hand navigation pane. - Choose the first
AMI
listed,WordPress Certified by Bitnami and Automattic
byBitnami
by clickingSelect
. - Click
Continue
on the pop-up. - Choose
t2.micro
as the instance type, then click the blue,Review and launch
button. - Click
Launch
, choose your key-pair from the dropdown (the one you’ve been using this whole time), check the acknowledgement box, and clickLaunch instances
. - On the next page, click the instances link to navigate to your newly created instance, then click the link again on the next page.
- Under the
Details
tab, you should see theAMI ID
listed. Copy it to your clipboard (or a notepad on your computer). You’ll need it in the next section.
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
- Open up your code editor to the
ec2-terraform
project. - 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 theterraform.tfvars
file (where we give variables value) and forami
, replace the current value with the value you copied from yourWordpress 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. - 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 tocomment out
youruser_data
parameter. - Do NOT delete this parameter! You will need it again later.
- Save your file.
- In your
Note: To
comment out
a block of code, select the code you want (as if you were going to copy it) then pressCtrl + /
(Windows/Linux) orCmd + /
(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.
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!
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.
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:
- Searching AWS Marketplace for a different
AMI
, then following the steps above as you did before OR - You can use this
ami-id: ami-0947d2ba12ee1ff75
anduncomment
(run the same command you used to comment it out) theuser_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.