Welcome to another episode of 'Back to Basics'.
I'm Aaron Lima.
And today we're going to discuss some recommended ways
to configure EC2 Instances after deployment.
In order to be useful, a screwdriver needs a screw.
The same can be said of an EC2 Instance.
It needs an application to be installed, to be truly useful.
Maybe you need to install a COTS-based application,
which is a consumer off the shelf application to your EC2 Instance,
or you need to install and configure software such as NGINX or IIS.
Now you can do all these task manually.
But in the cloud, you want to get away from manual tasks
as much as possible and have a fully automated deployment.
How do you perform a fully automated deployment in AWS?
While by using tools such as AWS CloudFormation,
you can not only ensure repeatable deployments,
but you can also have a way to document and track changes over time.
While CloudFormation excels at deploying AWS resources
and can perform configuration management,
it is recommended to use a tool purpose built for the job,
which is AWS Systems Manager.
And in this episode, we're going to discuss how to use AWS CloudFormation
to deploy an EC2 Instance,
and then use AWS Systems Manager to perform configuration management on that instance.
We'll also walk through some patterns of using auto-scaling groups
and AWS Systems Manager together
to perform configuration management.
Let's go ahead and dive in.
To define infrastructure with AWS CloudFormation,
you create a template, either written in JSON or YAML.
You could also use the AWS Cloud Development Kit,
which will generate a template for you.
This allows you to use an imperative programming language,
you are comfortable with like TypeScript, Python, Go, and others.
However, when the template is submitted to the CloudFormation Service,
it creates a stack.
A stack is a unit of related AWS resources.
If that stack includes an EC2 Instance,
you most likely need to install software or configure settings.
If this is the case within the stack, you can also create an AWS Systems Manager
State Manager Association.
When you create a State Manager Association,
you select a document, SSM has many documents that can apply
things such as Ansible playbooks,
or run PowerShell scripts or bash scripts.
Once an EC2 Instance with an SSM agent boots up
and checks in with the SSM service,
it first asks if there are any State Manager Associations to apply.
This makes for a good mechanism to perform bootstrap actions.
Anything you might perform with EC2 user data
you can use SSM for, provided the instance includes the SSM agent.
When creating a State Manager Association,
you can set it to have CloudFormation to wait
until the configuration completes successfully.
In doing so, when the stack is created successfully,
you'll know that your instances have been configured as well.
Now, a benefit of this pattern is being able to push logs centrally
for troubleshooting purposes.
The SSM agent can handle pushing the standard output
of our config task to an Amazon S3 bucket.
So if your configuration fails,
you don't need to SSH or RDP to the instances to collect logs
or add additional lines of code to push logs somewhere.
The agent will handle that for you.
Now, just a word on your scripts.
To take advantage of State Manager, you should have item potent scripts,
meaning that they can be re-run and check configuration
and only act when configuration has drifted.
This is where config tools like PowerShell DCE, Ansible,
Chef, or Puppet would come.
And SSM has documents that support all of these tools.
Now our first pattern we talk through using SSM to configure an EC2 Instance,
deploy by CloudFormation.
But let's say our stack creates an auto-scaling group.
Well, state manager allows us to target instances specifically
or by tags or resource groups.
Remember that when the SSM agent registers itself to the service,
it asks if there are any State Manager Associations to apply.
So if we create our State Manager Association to target based on a tag,
we can also configure an EC2 Instance that is deployed
in an auto-scaling group,
and it will be configured as it boots up.
Traffic will be sent to the EC2 Instance
once it reports as healthy to the auto-scaling group.
In this episode, we explored how we can configure EC2 Instances
deployed by AWS CloudFormation
or auto-scaling groups with State Manager Association.
We also highlighted the importance of using Idempotent scripts and tools and methods
when using this pattern.
This pattern can be used when installing web servers or consumer off the shelf applications.
Please check out the links below for more details.
See you next time.