# AWS CloudFormation

AWS CloudFormation is a service provided by Amazon Web Services (AWS) that enables users to model and manage infrastructure resources in an automated and secure manner. Using CloudFormation, developers can define and provision AWS infrastructure resources using a JSON- or YAML-formatted infrastructure as code template. The service was released on February 25, 2011.

## Overview

AWS CloudFormation provides a way for users to model an entire AWS infrastructure in a text file, allowing for the infrastructure to be version-controlled, shared, and reused. By using templates, users can create, update, and delete a collection of resources together as a single unit, known as a stack.

## Template anatomy

A CloudFormation template consists of several sections, including Resources, Parameters, Mappings, Conditions, Outputs, and Metadata. The most important section is the Resources section, which defines the AWS resources to be created or modified.

### Resources

Resources are the AWS components that are created, updated, or deleted when the CloudFormation stack is created, updated, or deleted. Examples of resources include Amazon EC2 instances, Amazon S3 buckets, and AWS Lambda functions.

### Parameters

Parameters enable users to input custom values to a CloudFormation template, allowing for customization without modifying the template itself. This makes templates more reusable and flexible to accommodate different environments and use cases.

### Mappings

Mappings define a set of key-value pairs that can be used to map input values to corresponding output values, making it possible to conditionally define properties and values based on the input parameters. For example, mappings can be used to define different instance types and Amazon Machine Images (AMIs) for different environments.

### Conditions

Conditions enable users to define conditional statements within a CloudFormation template, allowing for the creation or modification of resources based on specific criteria. This can be useful in cases where certain resources should only be created or modified under specific circumstances.

### Outputs

Outputs provide a way to export information about the created resources, making it possible to share this information between stacks or with external systems. Outputs can be used to easily reference resources created by a stack in another stack, facilitating the integration of different AWS services.

### Metadata

Metadata is used to provide additional information about the template or resources within the template. This can include documentation, licensing information, or other descriptive data.

## Example CloudFormation template

The following example demonstrates a simple AWS CloudFormation template in YAML format:
This template has the following sections:
AWSTemplateFormatVersion: Specifies the CloudFormation template version.
Description: Provides a brief description of the template.
Parameters: Defines input parameters for the template. In this example, the parameter is the Amazon S3 bucket's name.
Resources: Lists the AWS resources to be created. Here, it specifies the creation of an Amazon S3 bucket.
Outputs: Defines the values to be returned after the stack is created. In this case, it returns the Amazon Resource Name (ARN) of the created S3 bucket.
By using this template in AWS CloudFormation, a user can create an Amazon S3 bucket with a specified name, and the ARN of the created bucket will be returned as an output value.

## AWS CloudFormation Drift Detection

AWS CloudFormation Drift Detection is a feature that allows users to detect whether their stack resources have drifted from their expected template configurations. Drift detection helps maintain the consistency of infrastructure resources and aids in troubleshooting when unexpected changes occur.

## Key milestones

2011: AWS CloudFormation was officially launched on February 25, 2011, providing users with a way to define and manage AWS infrastructure resources using a single text file.
2016: Support for YAML formatted templates was introduced, in addition to the original JSON format, offering users more options when creating templates.
2018: AWS CloudFormation Drift Detection was launched, allowing users to detect when their stack resources have drifted from their expected configurations. This feature helps users identify and address configuration discrepancies.
2019: AWS CloudFormation Registry and CLI were introduced, enabling users to extend the service with third-party and custom resource types. This enhancement allows for more flexibility in managing resources beyond the native AWS offerings.