AWS CloudFormation Template Tutorial

Stephane Maarek
4 Jun 201915:07

Summary

TLDRThis tutorial demonstrates the process of using AWS CloudFormation to create, update, and delete cloud resources. It begins with the creation of an EC2 instance and an Elastic IP, followed by the addition of security groups. The script illustrates how CloudFormation templates are updated to reflect changes, and how the service manages resource lifecycle, including automatic cleanup. The video concludes with a demonstration of deleting a stack, showcasing CloudFormation's efficiency in resource management.

Takeaways

  • 🚀 The script provides a hands-on example of using AWS CloudFormation to create an EC2 instance, an Elastic IP, and two security groups.
  • 📚 The focus is on understanding the process of creating and updating CloudFormation templates rather than the syntax or content of the template itself.
  • 📍 It's important to work in the AWS North Virginia region (USEast1) to ensure consistency with the course setup.
  • 📂 The script demonstrates how to upload a CloudFormation template to Amazon S3 and create a stack from that template.
  • 🔖 Tags are used in CloudFormation to categorize and manage resources, with the example showing a 'course' tag with 'CloudFormation' as its value.
  • 🔄 CloudFormation templates can be updated by uploading a new template, which triggers changes to the stack, including resource creation and updates.
  • 🛠️ The script shows that adding certain resources to a template can result in the replacement of existing resources, such as when adding security groups to an EC2 instance.
  • 🔍 CloudFormation provides a preview of changes before updating a stack, allowing users to review what will be created, updated, or deleted.
  • 🗑️ Deleting a CloudFormation stack automatically cleans up all resources associated with that stack, simplifying the management process.
  • 🔑 The script highlights the importance of managing resources through CloudFormation for consistency, control, and ease of maintenance.
  • 🔍 The script emphasizes the visibility provided by CloudFormation's event logs, which track the creation, update, and deletion of resources.

Q & A

  • What is an EC2 instance in AWS?

    -An EC2 instance is a virtual server in the cloud that users can launch and manage in the Amazon Web Services (AWS) cloud. It is a fundamental part of the AWS infrastructure that allows users to run applications on virtual servers in the AWS cloud.

  • What is an Elastic IP and why is it used in AWS?

    -An Elastic IP is a static IP address designed for dynamic cloud computing, designed to be associated with an EC2 instance in a VPC. It is used to maintain a consistent IP address even when the underlying instance changes, which is useful for maintaining a persistent connection to an application.

  • What are security groups in AWS and what role do they play?

    -Security groups act as a virtual firewall for instances to control inbound and outbound traffic. They are used to allow or deny network access to instances based on IP addresses, protocols, and ports.

  • What is CloudFormation and how does it simplify infrastructure management?

    -AWS CloudFormation provides a common language for you to describe and provision all the infrastructure resources in your cloud environment. It simplifies the management of related resources as a stack, making it easier to manage and update the infrastructure as a whole.

  • Why is it important to work in the US East (N. Virginia) region for this course?

    -The instructor wants all participants to have the exact same setup as them to ensure consistency and avoid confusion. This standardization helps in following along with the course without encountering region-specific differences.

  • What is the purpose of tags in CloudFormation?

    -Tags in CloudFormation are used to organize and categorize resources within a stack. They can be used to filter resources and provide additional metadata about the resources that are created.

  • How does CloudFormation handle updates to a stack?

    -CloudFormation updates a stack by deploying changes to the resources defined in the new template. It can create new resources, update existing ones, and even replace resources if necessary, while ensuring that the overall stack remains functional.

  • What happens when a CloudFormation stack is updated with a new template?

    -When a stack is updated, CloudFormation compares the new template with the current state of the stack and determines the changes needed. It then applies these changes, which may include creating new resources, modifying existing ones, or replacing them entirely.

  • How does CloudFormation manage resource dependencies during updates?

    -CloudFormation understands the dependencies between resources and updates them in the correct order. For example, it may create security groups before an EC2 instance to ensure that the instance can be properly configured with the groups.

  • What is the process for deleting resources created by CloudFormation?

    -To delete resources created by CloudFormation, you can use the 'Delete Stack' option in the CloudFormation console. This will initiate the deletion of all resources associated with the stack in the correct order, ensuring a clean removal of the infrastructure.

Outlines

00:00

🚀 Introduction to AWS CloudFormation

The video script begins with an introduction to AWS CloudFormation, focusing on creating a simple EC2 instance, which is a fundamental server offering in AWS. The presenter guides the audience to set aside concerns about code syntax and instead concentrate on the process of creating a CloudFormation template. The script emphasizes the importance of having the same setup as the presenter, particularly the use of the North Virginia (US East) region in AWS. The audience is instructed to navigate to the CloudFormation service and create a new stack using an uploaded template, which in this case, is a basic EC2 instance template. The presenter also explains the process of uploading the template to Amazon S3, validating it, and initiating the creation of the stack.

05:02

🛠️ Exploring CloudFormation Stack Resources and Tags

In this section, the script delves into the resources created by the CloudFormation stack, specifically highlighting the creation of an EC2 instance with tags that include a course identifier and CloudFormation-specific tags such as the logical ID, stack ID, and stack name. The presenter demonstrates how to access the EC2 management console to view the instance and its tags, emphasizing the automatic application of tags specified in the CloudFormation template. The script then discusses the need to update the stack to include additional resources, such as security groups and an elastic IP, which requires uploading a new CloudFormation template and reviewing the changes before updating the stack.

10:03

🔄 Updating and Managing CloudFormation Stacks

The script continues with a detailed explanation of how to update an existing CloudFormation stack by uploading a new template that includes modifications such as additional security groups and an elastic IP. The presenter clarifies that CloudFormation templates cannot be edited directly but must be replaced with updated versions. The audience is shown how to review the changes that will occur, including the addition of resources and the replacement of the EC2 instance due to the security group update. The script also covers the automatic cleanup performed by CloudFormation, such as terminating the old EC2 instance once the new one is up and running. Finally, the presenter discusses the process of deleting a CloudFormation stack, which cleans up all associated resources in the correct order, demonstrating the ease and efficiency of managing cloud resources through CloudFormation.

Mindmap

Keywords

💡CloudFormation

CloudFormation is a service offered by Amazon Web Services (AWS) that allows users to create and manage a collection of related AWS resources using a template file. In the video, it is the primary tool used to demonstrate the creation, update, and deletion of AWS resources such as EC2 instances, security groups, and Elastic IPs. The script mentions CloudFormation as the central theme, showing how it can be used to automate the provisioning of AWS resources.

💡EC2 Instance

An EC2 (Elastic Compute Cloud) Instance is a virtual server in the AWS cloud. It is a fundamental concept in the video, where the script describes the process of creating an EC2 instance using CloudFormation. The EC2 instance is part of the AWS free tier, and it is used to demonstrate the automation capabilities of CloudFormation.

💡Elastic IP

An Elastic IP is a static IP address designed to be attached to an AWS EC2 instance to maintain a consistent IP for the instance, even if it is stopped and started again. In the video, the script explains how to add an Elastic IP to an EC2 instance using CloudFormation, enhancing the instance's accessibility and reliability.

💡Security Groups

Security groups in AWS are a virtual firewall that controls inbound and outbound traffic to EC2 instances. The script discusses the creation of two security groups and their association with an EC2 instance. This demonstrates the ability of CloudFormation to handle complex configurations that ensure the security and accessibility of AWS resources.

💡Template

A template in the context of CloudFormation is a JSON or YAML file that describes the AWS resources and their configuration. The script refers to templates as the blueprints for creating stacks in CloudFormation, which are collections of AWS resources that are created, updated, or deleted together.

💡Stack

A stack in CloudFormation is a collection of AWS resources that are created and managed together. The video script uses the term 'stack' to describe the process of creating, updating, and deleting a group of AWS resources as a single unit, which is a core concept in managing infrastructure as code.

💡S3

S3 stands for Simple Storage Service, which is an object storage service offered by AWS. In the script, S3 is mentioned as a place to upload the CloudFormation template file, demonstrating how resources can be stored and referenced in AWS for use in automation tasks.

💡Update Stack

Updating a stack in CloudFormation involves making changes to the stack's resources or template. The script explains the process of updating a stack by uploading a new template that includes additional resources like security groups and an Elastic IP, showing how CloudFormation can adapt to changing requirements.

💡Tagging

Tagging is the process of adding metadata to AWS resources to help organize and manage them. The video script mentions the use of tags in CloudFormation to label resources with specific information like 'course' and 'CloudFormation', which aids in resource identification and management.

💡Logical ID

A logical ID is a unique identifier used within a CloudFormation template to refer to resources. The script refers to the logical ID when discussing how resources are identified and managed within a CloudFormation stack, which is crucial for tracking and referencing specific resources.

💡Delete Stack

Deleting a stack in CloudFormation is the process of removing all the resources associated with that stack. The script concludes with the demonstration of deleting a stack, which results in the cleanup of all resources that were created through CloudFormation, emphasizing the automation of resource management.

Highlights

Introduction to creating a simple EC2 instance using AWS CloudFormation.

Explanation of EC2 instances as servers in AWS available on-demand.

Guidance on adding an Elastic IP to an EC2 instance for static IP addressing.

Demonstration of assigning two security groups to an EC2 instance for enhanced security.

Emphasis on ignoring code syntax for now to focus on CloudFormation template creation and updates.

Instructions to use the North Virginia US East region for consistency during the course.

Step-by-step guide on how to upload a CloudFormation template from Amazon S3.

Tutorial on specifying a stack name and adding resource tags in CloudFormation.

Overview of the CloudFormation stack creation process and its status updates.

Description of the event log in CloudFormation for tracking the creation and update of resources.

Explanation of how CloudFormation automatically applies tags to resources based on the stack.

Illustration of managing and customizing AWS resources through CloudFormation tags.

Process of updating a CloudFormation stack with new resources like security groups and an Elastic IP.

Clarification that CloudFormation templates cannot be edited directly but require uploading a new template.

Demonstration of previewing changes before updating a CloudFormation stack.

Observation of CloudFormation's intelligent handling of resource creation and deletion order.

Highlight of CloudFormation's self-cleaning feature after updates by terminating old instances.

Tutorial on deleting a CloudFormation stack to clean up all associated resources.

Conclusion emphasizing the power and efficiency of CloudFormation for managing AWS resources.

Transcripts

play00:00

okay so let's get started with some

play00:01

hands-on example around cloud formation

play00:04

in this example when you create a simple

play00:07

ec2 instance and ec2 instances pretty

play00:10

much everyone knows about it it's the

play00:12

server's in AWS that you can get on

play00:14

demand we're also going to create and

play00:17

add an elastic IP to that instance and

play00:19

we're also going to add two security

play00:21

groups to it for now I want you to

play00:24

forget about the code syntax I want you

play00:26

to forget about what is the content of

play00:29

the template just for now we'll do a

play00:31

much bigger deeper dive later on in this

play00:33

course what I'm going to focus on is how

play00:36

it creates my alias CloudFormation

play00:38

template how it's being updated what

play00:41

happens in my alias account and so on

play00:43

okay so we'll see how in no time you are

play00:47

able to get started with cloud formation

play00:48

so hopefully in the previous lecture you

play00:51

did download the code it was attached in

play00:54

the resources so you should have the

play00:55

code by now so here is your areas

play00:58

console and I just created my accounts

play01:01

if you don't have one should create one

play01:03

obviously you do is tap it with cloud

play01:05

signup and you have it when you're

play01:08

logged in you see this screen the first

play01:10

thing I want to make sure is that for

play01:12

the rest of this class just to make sure

play01:14

we're on the same page I want you to

play01:16

work in a North Virginia US East region

play01:20

and that's called USGS one and the only

play01:22

reason is I want you to have the exact

play01:24

same setup as me

play01:25

it doesn't matter when you get to work

play01:27

into your own region you can change the

play01:29

region later on but for this course

play01:31

please bear with me and please use the

play01:33

US East North Virginia region okay so if

play01:37

you find CloudFormation you click on

play01:38

services and then you just type in cloud

play01:42

formation and this is the first link we

play01:46

are being greeted by the screen which

play01:49

prompts us to create stack stack set

play01:52

designing a template or from educating

play01:54

resources for now we're just going to

play01:57

create a new stack when we get into that

play02:00

screen it's ask us for a template we can

play02:03

either design a template and we'll go

play02:05

over that's in there for next lectures

play02:06

or we can choose a template we can

play02:09

select from some templates that they

play02:10

already have for us we can upload a file

play02:13

to Amazonas

play02:13

three or specify an Amazon s3 template

play02:16

URL so what we notice is already that

play02:19

either we do something in the cloud or

play02:22

directly we have to select something

play02:25

that lives already in Amazon s3 so we'll

play02:29

choose upload a template Amazon s3 now

play02:32

if we look at the templates

play02:33

we'll have to we have just easy to and

play02:35

easy to is security group II IP which is

play02:38

the more complex one so we'll just start

play02:40

with just ec2 and you can have a look

play02:43

already but what this will do is that it

play02:45

will create one ec2 instance in the US

play02:47

east one a availability zone and there

play02:50

will be a teacher micro which is part of

play02:52

the free tier so what I'll do is I'll

play02:55

just upload this file for this there

play02:57

easy

play02:57

I choose a file and I click on 0 just

play03:01

ec2 alright so far so good then we click

play03:05

Next and the template is being uploaded

play03:07

to Amazon s3

play03:09

and it's being validated now we have to

play03:12

specify it's stacked a stack name for

play03:15

this I will call it introduction and you

play03:17

can choose whatever you want but I like

play03:19

to call things but what they are this is

play03:21

an introduction so we'll call this

play03:23

introduction

play03:24

next you are able to specify some tags

play03:27

in your resources and tags we'll just

play03:30

add one the name of the tag will be

play03:33

course and the value will be

play03:35

CloudFormation just to show you what

play03:38

this does when we do create these

play03:40

resources we have a bunch of Advanced

play03:42

Options we'll go over there later on

play03:44

next here is our template URL it has

play03:49

been uploaded through Amazon s3 there's

play03:52

no description here we can estimate the

play03:54

class booking on a cost the stock name

play03:56

is introduction we have a tag name

play03:58

confirmation and that's about it and we

play04:01

click on creates so here we go we see

play04:04

now the screen is being populated with

play04:06

the stock name introduction and has been

play04:09

created at this time and the status is

play04:11

created in progress

play04:12

so while we wait basically what this

play04:14

will do is that it will go ahead and

play04:16

create whatever was in a CloudFormation

play04:20

document in which case what we had was

play04:23

just an ec2 instance

play04:27

so this CloudFormation template will not

play04:28

go green up until that ec2 instance has

play04:32

been created and is working ok so that's

play04:35

why right now you see create in progress

play04:37

the really cool thing though is that you

play04:39

can refresh obviously and you see that

play04:43

there's an events tab right here which

play04:45

tells you exactly with the timestamp how

play04:48

the events are going for example I

play04:50

started at 22 29 32 and 26 seconds later

play04:55

the create was completes ok so you can

play04:59

see all the events into one very nice

play05:01

timeline now if you go to overview

play05:04

there's nothing new I put nothing but if

play05:06

we go to resources now we see that there

play05:09

is an ec2 instance that has been create

play05:12

complete and the logical idea is my

play05:14

instance so if you click on it and open

play05:18

it in a new tab we get redirected to the

play05:21

ec2 management console and we see right

play05:24

here that we have our instance I opposed

play05:26

to just teach you micro in the

play05:28

availability zone that we wanted u.s. 1a

play05:30

and it's been created so pretty amazing

play05:32

right we just dumped a file into cloud

play05:36

formation around the cloud formation and

play05:38

all of a sudden we end up with an issue

play05:40

2 instance what's even better is that if

play05:43

you go to tags we can see that this

play05:45

instance has been tagged with a lot of

play05:47

things already first of all there is

play05:49

this course CloudFormation tag that I

play05:52

specified during the lunch so what that

play05:54

means that for your cloud formations any

play05:56

tags that you specify in the cloud

play05:59

formation prompts will be applied to any

play06:01

resource in the stack which is amazing

play06:04

but also you inherit three areas

play06:08

specific tags based on cloud formation

play06:10

the number one is the logical idea of

play06:13

the thing which is my instance the

play06:15

second one is the stack ID

play06:17

it belongs to my big AR n stack and

play06:20

finally this tech name which was cool

play06:23

introduction so that's really cool

play06:25

because from a management and from a

play06:27

custom perspective you have lots and

play06:29

lots of control over which instance is

play06:32

tagged with what what it belongs to and

play06:34

so on so it's pretty amazing

play06:36

so to summarize I've created a cloud

play06:38

formation template

play06:40

and has just an easy-to instance now say

play06:43

you go you go you go and guess what

play06:45

things change your boss asked for two

play06:48

security groups and an elastic IP as we

play06:52

can see right now there are no security

play06:54

groups there's just a default one okay

play06:57

and there is no elastic IP just a public

play07:00

IP but it was random so let's go ahead

play07:02

and edit this cloud formation templates

play07:05

well it turns out you can't edit

play07:07

CloudFormation template instead what you

play07:11

have to do is to provide a new cloud

play07:13

formation templates so let's do this

play07:15

we're going to click on update stack and

play07:17

we get right back to that first screen

play07:20

of selected templates again we have to

play07:23

upload a new file okay we can't edit

play07:25

what already existed we have to plot a

play07:27

new file and now we have one easy to

play07:30

with s G and E IP if you don't look at

play07:33

the code again we'll go an emerge deeper

play07:35

dive later on in this course but now we

play07:38

have our instance and it has two

play07:40

security groups which are defined right

play07:43

here but also it has an elastic IP and

play07:47

that elastic IP belongs to my instance

play07:51

so let's to take on again it believe me

play07:54

that basically creates the resources and

play07:56

links them together ok so I would put

play08:01

the file I click on next and I'm going

play08:05

to be asked to review the cloud

play08:06

formation name and as you can see this

play08:09

is grayed out we can't change the name

play08:11

after it's been created ok so if you do

play08:14

specify a stack name you can't change it

play08:16

later you have to delete your task stack

play08:18

and recreate it to change the name press

play08:21

next and as you can see we can add more

play08:25

tags we can delete the previous ones

play08:26

it's pretty cool ok anyway what we'll do

play08:30

is go on next and what we see is that we

play08:35

can review what's happening there's a

play08:37

new template URL the stack names the

play08:39

same the tags are the same but oh here

play08:42

we go

play08:43

something happens cloud formations tells

play08:47

us there's going to be changes and of

play08:49

course they're gonna be changes because

play08:50

we did upload a new templates but what's

play08:53

really

play08:54

is that we can preview what the changes

play08:57

are going to be and that's really nice

play08:59

because if you were to change something

play09:01

you really want to make sure that you're

play09:03

not you know messing things around so

play09:06

here we go we're going to add an elastic

play09:09

IP and it says add we're going to modify

play09:12

our ec2 instance okay and because we

play09:16

added security groups to it and that

play09:18

change actually will trigger a

play09:20

replacement of that instance so very

play09:23

good to know that previous one will be

play09:24

replaced entirely finally we'll have two

play09:29

security groups that will be created all

play09:32

right so yeah I'm pretty happy with this

play09:35

changes I'll click on updates and here

play09:39

we go so now if we look at the events

play09:41

log again the previous events are kept

play09:45

but now we are into an update in

play09:47

progress events so as I as a refresh we

play09:52

can see that the event is getting more

play09:54

and more populated for example here it

play09:57

created my two security groups and now

play10:00

it's going to create my ec2 instance so

play10:03

it's going to create a whole new one and

play10:04

stuff so as I let it happen basically

play10:08

Arius CloudFormation figures out the

play10:11

order in which things need to happen it

play10:14

understood that first you needed to

play10:16

create the security groups before you

play10:19

could create the ec2 instance and that's

play10:21

why the security rules were created

play10:22

first and then my ec2 instance was being

play10:25

created so if you're crippling freshing

play10:28

for example once the ec2 instance has

play10:30

been created then we can go ahead and

play10:33

have our elastic IP being created and

play10:36

finally once the elastic IP is created

play10:39

which might take a while it weighs we'll

play10:41

go and remove the old ec2 instance so

play10:45

one more refresh

play10:50

anyway in the meantime what we can do is

play10:53

go to resources and as you can see now

play10:56

in the resources we don't see one item

play10:58

though and now we see four items we see

play11:01

that we have two security groups my

play11:04

instance and an elastic IP that has this

play11:08

IP and that's really cool because

play11:09

everything was provisioned from within a

play11:12

template okay so as we've seen here

play11:14

every resources has been created

play11:17

successfully and some were created

play11:19

somewhere updated and then finally in

play11:22

the event log is going to delete the old

play11:25

database instance that we didn't use

play11:27

okay so let's go ahead and review all

play11:31

these resources if we go to the

play11:34

management console we see that the new

play11:36

instance is running but now it has two

play11:39

security groups attached to them so

play11:40

let's just go and go to the security

play11:43

groups and if you check the security

play11:45

groups we can see that the group name is

play11:47

introduction which is the name of my

play11:49

stack and then the idea of my group and

play11:52

some random number the tags are what we

play11:55

expect them to be here is the course

play11:57

CloudFormation tag and it has the right

play12:01

rules we assign them to this is my

play12:03

second group right here and again the

play12:05

tags are correct

play12:06

the group name is different obviously

play12:08

and so on finally if we check something

play12:13

the elastic IP parent has been defined

play12:15

and we can open it and again obviously

play12:19

you know what if you guess now the

play12:21

elastic IP will be exactly what we want

play12:23

it to be okay anyone have the right

play12:25

association so it's all very very very

play12:29

very nice but finally let's look at what

play12:32

happened the previous instance got

play12:34

terminated so on top of like creating a

play12:38

new instance areas cloud formation was

play12:41

smart enough to say wait wait I'm going

play12:44

to terminate the old instance because my

play12:47

user doesn't want it anymore and it went

play12:50

ahead and terminated it so that's

play12:53

awesome

play12:53

because it just cleaned up after itself

play12:55

and that's really cool that's something

play12:58

we can see in the events log and for

play13:00

example here

play13:01

the cleanup was happening and it was

play13:03

delete

play13:04

the old instance and completing the

play13:06

deletes so so far we've seen a create an

play13:11

updates and to finish this little part

play13:16

of the tutorial say we want to delete

play13:18

say we want to clean up what we did

play13:19

because we created one is you to

play13:21

instance we have created two security

play13:24

groups and we have created elastic IPS

play13:27

so how do we go about and cleaning

play13:29

everything do I go here and and

play13:31

terminate and everything or or is there

play13:33

a better way or in fact there's a better

play13:36

way if you go to the cloud formation

play13:39

right-click and say delete stack it will

play13:42

go ahead and delete all the stack

play13:44

resources for you which is amazing

play13:46

because everything you've created

play13:48

through cloud formation will be deleted

play13:50

with your cloud formation so I'll go

play13:52

ahead and say yes deletes and the delete

play13:56

is in progress and as you can guess it

play13:58

will start deleting things in order as

play14:00

well so you don't have to figure out

play14:02

what to delete first and what not to

play14:04

delete first it with cloud formation

play14:07

will figure this out for you so as you

play14:09

go ahead first will be deleted the

play14:12

elastic IP then the instance and then

play14:15

finally the security groups when all of

play14:18

this is over

play14:20

basically the cloud formation will

play14:22

disappear and you can change the filter

play14:24

to go and delete it so I'll just pause

play14:26

when this is done so after using the

play14:28

filter and everything disappears you can

play14:30

see that you can just use the filters

play14:32

get back to your CloudFormation template

play14:34

and then you see that the event log

play14:36

everything was being deleted and the

play14:38

delete is complete so now if I go to my

play14:40

management console and just refresh all

play14:44

my instances have been terminated if I

play14:46

go to my security groups my two new

play14:49

secrets are gone and if I go to my

play14:51

elastic IPS I would see the elastic IP

play14:54

will be gone so it's really cool because

play14:56

CloudFormation just cleaned up

play14:57

everything in one click so here we just

play15:00

witness the power of cloud formation for

play15:02

creating updating and deleting errors

play15:05

resources in no time

Rate This

5.0 / 5 (0 votes)

Étiquettes Connexes
AWSCloudFormationEC2Elastic IPSecurity GroupsResource ManagementAutomationCloud ComputingInfrastructure as CodeDevOps
Besoin d'un résumé en anglais ?