AWS CloudFormation by Example: An Introduction | Robert Chen | Skillshare

AWS CloudFormation by Example: An Introduction

Robert Chen

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
10 Lessons (20m)
    • 1. Introduction

      0:33
    • 2. Create a CloudFormation Stack

      2:03
    • 3. Delete a CloudFormation Stack

      1:03
    • 4. Update a CloudFormation Stack

      2:38
    • 5. YAML Syntax

      3:50
    • 6. Template syntax so far

      1:32
    • 7. Using Parameters

      2:10
    • 8. Parameter Validation

      3:52
    • 9. Troubleshooting Stack Deletion

      2:10
    • 10. Closing

      0:34

About This Class

This class covers the basics of AWS CloudFormation. 

CloudFormation is an important skill for anyone working with AWS. Whether you're working with S3, EC2, or RedShift, you want to script your deployments eventually. 

The CloudFormation by Example series uses a "CloudFormation first" approach. Rather than clicking around the GUI to provision EC2 instances, we'll skip all that and start with CloudFormation. This is what I do at work, and this is how I'll teach you.

People sometimes ask why CloudFormation is so hard, even though it's just YAML. The reason is that you need to understand how AWS resources work, but at one layer deeper than what you see in the GUI. So knowing CloudFormation is really about having both breadth and depth of knowledge about AWS. 

In the CloudFormation by Example series, we'll create common AWS architectures: Serverless, WordPress, and RedShift -- but build them from the ground up. Before we can do this though, we first need to get the intro stuff out of the way (which is this class).   

Transcripts

1. Introduction: welcome to injured a confirmation. My name is Robert Chen, and I've been working in I t. For over a decade with a recent focus on AWS. The purpose of this course is to give you a quick primer on AWS confirmation. So we'll be covering common tasks like creating and deleting stacks, working with perimeters and doing a little basic troubleshooting. I've kept the lessons pretty short, but after this course, you should be able to spend up confirmation stacks and maybe even handle a little trouble shooting, even if you've never used cloud permission before, so let's get started. 2. Create a CloudFormation Stack: in this video, we're going to learn the basics of deploying a cloud formacion template through the AWS console. So here I have a pretty basic animal file. It just has a single resource, which isn't s three bucket. This is probably the simplest confirmation template you could have. And we're going to deploy this through the AWS console using the cloud formation service here. I'm gonna search for cloud formation, and it will take me to the confirmation service up here. And when I click on create Stack and for a specified template, I'm going to upload a template file, and then I'm going to uploaded, uh, using the finder. Here, have my history bucket dot GMO file, click open. And next on this page, I'm gonna specify the stack name. I'm gonna call it rob test. That's three stock and click next on this page. I'm gonna skip everything and click next also on this page, scroll down and click. Create stack. The confirmation stack is creating right now. I can click refresh here, click on the events, Todd, and monitor the progress. Also, there's another refresh button here to see if this status has changed as well. This will take a few moments. Okay. It looks like the stack is done creating. I'm going to switch over to s three. It created a as three bucket for me called Rob Tests as three stack, which is what I called the cloud formation template. Get appended the cloud formation resource name and also a random string at the end. But it's basically an empty as three. Book it and this was all created using confirmation. 3. Delete a CloudFormation Stack: we're going to use the AWS console to delete our confirmation stack. Here we have our confirmation stack. And in it, if you go to the resource is tab, we have a single resource, which is industry bucket. I'm gonna click on this link to open the SUV bucket in different tab. So back to the confirmation page, I'm going to delete the stack by selecting it and clicking Delete. It's gonna ask me if I'm sure I want to do lead it. And when I click delete Stack. Right now, the delete is in progress. It should take a few moments, but if you reverse the page, the stack should no longer be there to double check. Going to click on this s three tab. Probably need to back out to the list of buckets. This right here is my s three bucket dep alone's to that confirmation stack. I'm going to click, refresh and issue disappear. The bucket is actually gone. And that's how you delete a cloud formation stack using the AWS console 4. Update a CloudFormation Stack: in this video, we're going to update our confirmation stack. In fact, we have a single resource which is in the street Buckets. We're going to update this bucket name, which looks like it's generated. We're going to update it to a name that we specify explicitly going back to our M O file in here, we're going to set some sort of property that will specify the bucket name. The syntax to do that is in their documentation, what I usually do. I copied the type, which is eight of us three bucket, and I just google it Google. This is usually the first thing that pops up in the documentation. There's usually a Yamil section that will show this syntax of all the properties that are supported by this resource at the top, we have the type which we partly have here we have the type and beneath that we have a properties key and the value is going to be an object here, which is this key value pair of bucket name. So we have to invent this under properties and that this inter properties and then the string which I'm going to call in true to plan information and save this and then to update the confirmation stack, I'm just going to upload this modified template on top of what we already have. So under stacks, I'm going to select the stack click update. I'm going to replace the current template with anyone and then but upload this updated template, Click next. Just gonna accept all the defaults. Next, go to the bottom on the last page, right before I click Update Stack, There's something called a change set preview. It'll tell me how it's going to change the bucket name. Since you can't actually change an estimate Bucket name. What it's gonna do is replace it entirely. It's going to delete the existing book it and create a new one with the name that we specify, which is fine. I'm gonna click. Update stuck. Our update is complete to check our work. I'm going to go to s three. I'm gonna back out to the service page and click. Refresh. This auto generated bucket name should disappear and it should be replaced by whatever we specified in the tablet. And here it is. Intruder confirmation rob test. And that's how you update a confirmation stack 5. YAML Syntax: I'm going to take a step back and go over gamble syntax. So I'm going to cover the basics of how Yamma works. So Yamil is very similar to Jason. It's a markup language You might be used to, Jason. Pretty common. And so in Jason, you have an object denoted by this curly bracket. And then you have a key child, objects, keys and values. You could also have an array that contains multiple items separated by commas. Gammell is the same thing. You have your first child object. You do know child objects by inventing. So this right here is a child object of this key right here. And you could also nest objects like we're doing right here. Um oh. Also supports a race in tax. This right here is an array. The array is denoted by this hyphen right here. The contents here. It represents a single object. This right here represents another single object. Gammell is very similar to Jason, but it's a lot cleaner, syntax wise. But you don't have all of this syntax noise. You don't need to have these curly braces or commas. Everything is done through inventing and hyphens like us. you also do not need double quotes. And, um Oh, so notice here. You don't really see any double coats. You could add them if you want, but just to keep things clean, I usually remove all the double quotes. Now, if I have a special character like an asterisk, I will usually surround this string with single quotes. Sometimes my I. D e prefers four syntax highlighting purposes that I surround Asterix with single quotes. One benefit that animal has over Jason is that you could add a comment. So I could at a comment here with Jason, I can't really do that. Um, if I wanted to add a comments and javascript, it would be this. But this isn't supported in Jason, but what you would have to do instead, you would actually have to create another property, my comment like that. And now your comment is actually part of your data, which I guess it's a work around something that you can't do in. Jason is comment out a block of code. So if I wanted to comment this out, I could not really do that here. So see if I wanted to do a multi line comments. This is not allowed. Gamble does look cleaner and easier to use than Jason. It takes a little bit of getting used to. When your confirmation temple it becomes very complicated. It's important you're using Gamel when you're writing syntax for your PC. Two instance. Easy to supports a user data property, and here you can inject bash code to write on. Start up just with two lines of bash. You have maybe 10 lines of syntax noise, which can become very unwieldy if you have any type of complexity in here. All of this is basically the equivalent of this. So in this course, we're going to be sticking with Gamel not only because it's simpler, but it's what you should ideally be using when you're reading riel confirmation templates. 6. Template syntax so far: So far we've been deploying this one clever mission template. And right now I'm going to take a few moments to explain what each of these lines mean. Starting from the top. Here we have the AWS template format version, which is always gonna be this hard coded date. You don't really need to worry about this. Just remember, you need to paste it into each one of your confirmation templates. This is a top level item. Another top level item is the resource is key. Under resource is this is where you provisions each one of your cloud formacion resource is that you define So in this case, I'm calling my resource my s three bucket. You can call it whatever you want and the value of this resource will have the following items. The first required item is a type. Confirmation needs to know what type of resource this is. The type is a string, and it's always going to be in the format of aws double colon, the service double Colin and then something else. So in this case, it's aws s three bucket at the same level as type. There's also properties and under properties, you have the various properties that are supported by the S three bucket resource type. So in this example, we have the bucket name sent to injured a confirmation rob test. So far, we have to top level items. This required template version. The resource is the resource is key. There are other top level keys that confirmation supports. For example, there's parameters, wrappings and the data conditions and outputs some of these air used more commonly than others. But I'll be going over these enough coming lectures, and that's how this confirmation template works over. 7. Using Parameters: So far, we have a confirmation template with a single as three bucket right here. But the problem is, the bucket name is hard coded. This is really inflexible. And what this template needs is the ability to dynamically set the bucket name. To do this, we use confirmation parameters. Here. I have the AWS documentation open for confirmation parameters. And this is what this Intacs looks like. I'm just gonna copy and paste it into my template. So I'm going to modify each one of these to suit our use case for the perimeter name. Let's call it the story. Okay? Name the type. We could keep a string. We don't need a default or these a lot values. And for the description, let's call it enter. And three bucket. Right now, we have a single perimeter called a three bucket name, and the user is going to input a string. But so far, the perimeters not actually doing anything, So we need to wire it up to Rs three bucket. How we do that is we use something called an intrinsic function. I'm just going to type exclamation rough. And the research name will cover intrinsic functions in more duct later on. But just know that whatever the user inputs into the three bucket name will show up as a string here. I'm gonna hit, save as usual, I'm gonna click, create stack. I upload my template. Click next, enter a stack name. So this is new. This is the perimeter that we set before with the perimeter name and the perimeter description. Here, we could set the bucket name. I'm gonna call it Intro to cloud Formation. Rock tests to and click Next on the configure Stock Options page will just accept the defaults as usual, click next and on the review page, Scroll down to the bottom and click. Create stack. Give it a few moments to provision and click Refresh to monitor the progress. So it looks like our confirmation stock is done provisioning. Let's go to s three and check it. So here in history, we have enough three bucket called intro to confirmation rob custom, which is what we specified. And that's how you set a perimeter in confirmation 8. Parameter Validation: in this lesson, I'm gonna cover confirmation parameter validation. So here we have the confirmation template that we've been working with so far. We have a single resource, which is in history Bucket. And we have a single parameter that defines the S three bucket name. I'm going to go ahead and deploy this to confirmation. As usual. I'm gonna create the stack and upload my confirmation GMO file. No, I'm gonna call the stack Rob test as three stack as before, but this time for the S three bucket name. I'm going to do something a little bit different this time. I'm adding some upper case letters. Uppercase letters are not allowed for us three bucket names. So let's see what happens. I'm going to keep clicking next and accept all the defaults. Well, so far, so good. My confirmation stock is at these creating. But as soon as I hit refresh, I have a status of rollback complete. So if you ever run into a status of rollback complete or roll back in progress, the first thing you want to do is click on the events tab in the scroll all the way to the bottom from the bottom. You want to work your way up until you reach the oldest error. In this case, it says, create failed get name should not contain uppercase characters. Sometimes this status doesn't make sense. So in those cases you could at least refer to the logical I D and figure out which resource is causing the problem, since the stack failed to deploy gonna go ahead and delete it. So back to our, um oh file. Let's add some syntax to prevent this from happening again. Here I'm adding some form validation rules to are a three bucket name parameter. And if the string that the user enters doesn't match these requirements and it will receive an error, which we define here, it's a real quick. Let's take a look at the three bucket naming requirements. The bucket name needs to be between three and 36 characters. It can have lower case letters, but not upper case, and you could have numbers, periods and dashes, but no underscores. There are also some more obscure rules, but I'm not gonna worry about that for now. Back in her confirmation template, we added some rules to make sure that he s three bucket name is between three and 36 characters. We also have a rejects pattern where you start with a single character that is either a letter or a number, followed by a bunch of characters that are either letters numbers that having character or a period. And when you add these together, you end up with three and 63 characters. If you include this one. Finally, we have some help for text that shows up whenever you're s three bucket name doesn't comply with these rules. So let's go ahead and deploy this to confirmation and see if we could trigger a form validation error here. I'm still using capital letters, and as soon as I click create Stack, I get this Red Banner that includes the constraint description that I defined earlier. Now let's go back and set the S three bucket name to lower Case. This time, when I create the stack, it should deploy without any issues, and there we have it. Now our status is green. So to recap, I made a common mistake of including uppercase letters in my history bucket name. Instead of letting users run into this era runtime here We had some form validation to catch this error immediately, and that's how you add form validation to your confirmation parameters. 9. Troubleshooting Stack Deletion: in this video, I'm going to cover a cloud formacion error that I run into all the time. Here we have the cloud formation stack that we've been using to about the course. I'm going to try to delete the stack, but it's actually going to fail. So here we have a status of delete failed. Let's check out the events and see what happened. So here on the events have I'm going to scroll to the bottom and then work my way up until I see the first error. And here it says, the bucket you tried to delete. It's not empty. So what's happening here is AWS is trying to prevent a data loss situation by preventing confirmation from deleting in S three bucket if there's anything in it. And so you're going to run into this pretty much any time you have a Confirmation Templar that contains in a storybook it. So in order to delete the confirmation stack, we have to first empty that's three bucket during stuff. Select the bucket, click empty and then type in the name of the bucket to confirm. And just to be sure, I'm going to check out the bucket contents. It looks like the book. It is empty. So now we can retry deleting the stack. So I'm going to see the screen because I feel to delete the stack last time if I wanted to . I can actually retain the S three bucket and delete the rest of the confirmation stack, but I don't need it, so I'm gonna delete the whole thing. This time, the deletion was successful. So to recap, if you ever run into an error deleting a confirmation stack first check to make sure that you're as three buckets are empty. And that's how you troubleshoot a confirmation stack deletion. 10. Closing: This is a pretty good stopping point for the course. So far, you've learned how to create, delete and update stacks. You learned the basics of familes syntax. You've worked with the basic confirmation template, and you made some changes to it by adding a perimeter and validating that perimeter. And finally we did some basic troubleshooting around a couple issues that you're likely to see in the wild. This course is pretty short and covers the bare minimum, but stay tuned for future courses on deploying Common resource is with Cloud Formacion such Azaz three E. C to conserve. Ellis. Thanks for watching.