Automation with Hashicorp Packer #5: Building Your First AMI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so now that we've got packer installed i'm going to create a directory to store all of our source code and don't worry guys i'll make sure to upload this to a github repository i'm going to open this folder up within vs code that's going to be my text editor of choice and the last thing i'm going to do is um you know we're going to run through a couple of example scenarios and i want to make sure that you guys have access to the code for each one of these scenarios so i'm going to break it out into different subfolders and each scenario will have its own folder so i'll create the first one i'm just going to call this project one even though it's not even a project we're going to be doing a simple scenario a simple example scenario but you know just know that everything's going to be updated chronologically so you know the first example that i give is going to be project one the second one is going to be in project two now packer um for its configuration files it uses a json format which i'm sure you guys are already pretty familiar with so i'm going to create a new file and i'm just going to call this example.json so this is going to contain all the configurations that packer needs now what i want you guys to do is open up the packer documentation this is always going to be your main reference point they do a pretty good job i think hashicorp does a great job overall across all of their different apps when it comes to documenting them and how to use them and so in the documentation i want to kind of quickly go over the different components that make up a packer config and there's really three building blocks to a packer config and those are going to be builders provisioners and post processors and what we're going to do is we're going to start off by taking a look at builders uh and so we'll create an image just using builders and then we'll and then in the next project we'll add in provisioners just so that you guys can see what each one of these do on a step-by-step basis and then finally we'll have a final project where we can add in the post processor as well and so hopefully at that point you guys can really understand what each one of these does and figure out how you want to set up your configs when you're creating an image for yourself so let's take a look at builders real quick and if we go over the overview it's going to ex it's going to explain what builders are and so this is kind of the main component but builders are ultimately responsible for uh creating a machine and generating an image from that machine across various platforms and if you take a look at the different platforms that packer supports uh you know they support a ton of platforms um all of the major cloud providers so amazon azure i'm sure we've got google cloud somewhere in here wherever that is google cloud but you can see they support a ton of other platforms so digitalocean they support docker hyper-v so it's not just cloud providers you can do local hyper-v uh vmware virtualbox right so they they have a ton of support um but we're going to be focusing mainly on aws and a little bit of azure um but you'll see that all of the principles that i uh go over in this video they're going to apply to across all of the different providers there'll just be a couple of different config options that you may need to pass in to kick things off we're going to create our very own aws ami and since we're working with amazon select the amazon tab right here and you'll see that there's a couple of different builders for amazon so we've got one just for plain old ec2 instances we've got one for ebs backed instances so we're going to select this one this is going to allow us to create an ami for an ebs backed ec2 instance all right so this is going to contain all of the documentation for how to work with this um but for now i'm just going to kind of walk you through the different configs that you need so this is once again a json file so let's get our curly braces and the first thing we have to do is we have to pass in the builder that we're going to use and packer actually allows us to pass in multiple builders so that's why this option or this config option is called builders and we actually have to pass an array and so once again right we can actually use multiple builders so if we wanted to create an identical image across aws azure and gcp uh we could actually configure three different builders in this config so that we could have you know a ubuntu image with certain packages installed that's identical across the three different cloud providers uh and so we're only going to be using one so let's just specify the amazon ebs builder so what we have to do is type and so this is going to be the name of the builder and it's going to be called amazon ebs all right and since uh packer needs to be able to connect to our aws account to actually create an ec2 instance so that it can then create an image from that ec2 instance it obviously needs access to our aws environment so we're going to have to pass in some credentials and if we take a look at the documentation under there should be an authentication section access configuration there's three different options that we have to pass in there's going to be access key region key and secret key and so if you don't know how to get this data i'll show you guys how to do that so go to your aws management console and select your account name and select on security credentials and under here select access keys and so i've got a couple of access keys you may not have that if you haven't set this up but what i can do is i'll just walk you through how to do that so i'm going to delete my current access keys and i'm going to create a new access key and when you do that you want to click on this button right here to see your current access keys because uh once you see them uh once you close out this window you won't be able to see them again so i recommend you actually download this file so that you have it and then just store it someplace safe and so we've got our access key and we've got our secret access key so what we can do is i can just copy that and we're going to pass in the axis key property and we need to pass in our secret key as well and then finally we're going to have to give our region and so i'm going to use the u.s east one that's my default region if you guys want to use a different region pick whichever one's closest to you or if you want to make sure that everything kind of works as you follow along then just go ahead and use us east one okay so this is all we need from a authentication perspective when it comes to logging into our aws account and i'm sure you guys are probably thinking the same thing right this um you know it's probably against best practices to just write your you know access keys or write into your configs right so if you ever want to uh save your configs in a github repository well now all of your passwords and credentials are stored in there as well and that's not quite secure and there's actually a couple of different ways of making this a little bit more secure so that we don't just hard code our credentials into our configs i'm going to hold off on talking about those methods for now i want to just start off with the basics so we're going to hard code them in for now but i promise you guys we'll have a dedicated section to um setting up a more secure way of authenticating with aws there's a couple of different options right we can always use environment variables and things like that so you know we'll we'll cover this all in the future but i just want to keep things simple i don't want to over complicate things at first all right so now that we've got our authentication set up let's take a look at all the other configs that are necessary for this specific builder and i'm going to scroll up to the top and under the ami configuration it says that there's one property that's absolutely required and that's ami underscore name so um what this property is used for is when we create a new image a new ami we need to give it a name so that you know we know which one we just created so let's pass that in and i'm just going to call this you know my first ami and you know there's obviously other properties we can pass in like a description and things like that but you know let's keep things simple uh let's just make sure we cover all of the required options so we've got access finished and then we want to go down to run configuration there's two different properties so there's source ami and instance type so the source ami is well we have to give packer a ami that you know we kind of want to modify right so we have to give it access to a mod to an ami that we want to tweak so that we can kind of use it as a base template for our new ami and so we can use any existing ami it can be a private ami that you've created through packer it can be one of the default amis that's within the aws uh ami marketplace uh and so let's grab one real quick and so we can go under services and ec2 if you select running instances and let's just launch a new instance and so this is going to show us all of the different amis i'm just going to grab the plain old amazon linux 2 ami very simple ami and so we can just copy this and i recommend that you guys don't copy what you see on my screen instead i want you to go to this page and copy whatever value it shows because yours may be different depending on what your region is and i think sometimes the ami values can also change so don't just copy what you see on my screen so we'll copy that let's go back to our configs and let's give it the source underscore ami and the next thing that we have to do is we have to specify an instance type and you know this kind of confused me at first because i didn't understand why we need to give it an instance type we're not trying to create an ec2 instance we want to create an ami and well the way that we create an ami or the way that packer creates an ami is it actually deploys an ec2 instance it then configures that ec2 instances with all the custom configs that you want and then it creates an ami from that ec2 instance so it does technically have to deploy an ec2 instance to be able to create an ami but packer will automatically destroy that ec2 instance once that ami is configured so it's just a temporary instance and so we're going to use the smallest cheapest instance type which is going to be the t2 micro here we can do instance underscore type and then pass in t2.micro and as i stated before and as i just stated right so packer is going to have to create an ec2 instance to log in configure it to then create an ami but we also have to tell packer what are the credentials to log in to that ec2 instance so here i'm going to pass in a ssh username and this is going to be the default username that aws uses for this specific ami which is always ec2 user however i know that under certain amis the default username is ubuntu if you're using like an ubuntu ami that's going to be the default user so in this case if you're following along with me go ahead and just type in easy to user but if you're using ubuntu vm then you want to set the user uh to be ubuntu and this is just to make sure that it knows what to log in as because it needs to be able to connect into it to make those necessary config changes okay so this is all the configs that we need uh for our builder there's obviously plenty of other extra options we can pass in depending on what we're looking for um but this is the base configuration that we need so right now we can create a custom ami from this source ami and let's actually go ahead and do that so we're going to create our first ami and let's do a i'm going to open up my integrated terminal if you're not using vs code or you don't have an integrated terminal that's okay just to open up the command line for whatever operating system you're using i'm going to navigate to that project one folder and here to actually run this configure create our ami we do packer build and then example example.json right so whatever your file name is and i want you to notice what's happening it prints out some detailed output so the main thing i want to focus on is that look at this it's launching an ec2 instance for us so if we go to our aws console and select ec2 and go into running instances you can see that we have an ec2 instance called packer builder so this is the one that packer creates for us it's that t2 micro so that's the option that we provided and so this is the ec2 instance that packer creates so that it can then generate an ami image from so it's going to create this once it loads up it's going to ssh into it it's going to pass in whatever configs we specified which at the moment we didn't actually give it any configs so we're kind of making almost a pointless ami because all we're doing is we're taking an ami deploying it as an ec2 instance and then creating a brand new ami so this ami that we're about to create is identical to that default ami that we're using so you know the reason i'm doing that is because i want to keep it as simple as possible in the next section we'll obviously figure out how to add custom configs deploy with extra packages and things like that but you can see it looks like it's already stopping it so if you take a look at the output you can see it's waiting for an ssh session to be available it'll connect and at that point it'll pass any specific configs and then once that's done it's going to stop the instance and then it's going to create an ami from that stopped instance and then once that's done it's going to then delete the ec2 instance so right now it's still creating that ami and so i'm going to pause this video for now and i'll touch base with you guys once that's complete all right so we can see that the ami was completed and so now packer is trying to delete uh the ec2 instance so it's terminating the ec2 instance it's cleaning up any extra volumes and just making sure it deletes all of the unnecessary stuff that it temporarily created now if we go back so we can see this is in a well actually let's refresh it so it's now in a terminated state so eventually aws will clean this out but if we go to amis we can see our very first ami right and we gave it a name of my first ami and so this is the ami you've created your first custom ami and if you go to instances we can actually create a new ec2 instance using that ami so if we go under my amis we can see this uh ami but once again guys keep in mind that you know we didn't actually do anything special or custom we didn't tweak the ami um i just wanted to show you guys how to create an ami um but in the next section we're going to start incorporating provisioner so provisioners is what ultimately allows us to modify and make changes to these images so that we have custom images that will already have all of the necessary packages and configs that we need uh and guys keep and guys keep in mind if you have any custom images on aws it will incur a recurring cost i'm not sure what the exact cost is it's very little but if you're trying to keep things you know as close to free as possible if you're following along just make sure to delete these amis as you go along and uh if you aren't aware on how to actually delete an ami it's pretty easy let's just go back i want to select amis select this ami and then we can just right click or we can go under actions and select deregister so that's the equivalent of deleting all right and so there we go we've cleaned out our ami and if we go back to instances let's just double check to see yep it's already in a terminated state so at this point you should not be accruing any extra costs
Info
Channel: Sanjeev Thiyagarajan
Views: 16,262
Rating: undefined out of 5
Keywords: pakcer, hashicorp, cloud, aws, azure, automation, image, build, ami, programming, linux, server, infrastructure
Id: vOV74gevFgs
Channel Id: undefined
Length: 16min 16sec (976 seconds)
Published: Mon Aug 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.