Getting Started With AWS Cloudformation
Published: 2023-11-28 21:29:02

## Introduction
The term "cloudformation" is frequently used in discussions on infrastructure as code (IAC), but what exactly is it? Let's dive right in:

CloudFormation is a service provided by Amazon Web Services (AWS) that allows you to define and provision AWS infrastructure as code. With CloudFormation, you can use a template to describe the resources and properties needed for your applications, and then deploy those resources in a predictable and repeatable way.

## Why is it necessary
Here are some key reasons why AWS cloudformation is necessary
1) **Infrastructure as Code (IaC)**: CloudFormation allows you to define and manage your infrastructure using code. This approach, known as Infrastructure as Code (IaC), brings several benefits, including version control, code reuse, and the ability to treat infrastructure changes like software changes.

2) **Automation**: CloudFormation enables automation of the infrastructure provisioning process. Instead of manually creating and configuring resources, you can use templates to define your infrastructure requirements, allowing for consistent and repeatable deployments. 

3) **Consistency and Standardization**: With CloudFormation, you can define a standard set of resources, configurations, and dependencies for your applications. This ensures that your infrastructure is consistent across different environments (development, testing, production) and over time.

4) **Scalability**: As your application grows, manually managing infrastructure becomes challenging. CloudFormation allows you to scale your infrastructure seamlessly. You can easily replicate templates to provision additional resources, whether it's adding more servers, databases, or other components. 

5) **Versioning and Rollback**: CloudFormation templates can be version-controlled using standard version control systems like Git. This enables you to track changes, roll back to previous versions, and maintain a history of your infrastructure configurations.

## Basic Syntax
AWS Cloudformation is defined either in JSON or YAML format. The choice between JSON and YAML is largely a matter of personal preference, as both formats are supported.
Templates consist of sections like Resources, Parameters, Outputs, etc.
Let's break each one down:

**Stack**: is a set of AWS resources created and managed together.
**Resource**: is an AWS component like EC2 instance, S3 bucket, etc.
**Parameters**: are input values for your template.
**Output**: Values you want to view after the stack is created.

## Creating a Cloudformation Template
We will be working on a YAML template that creates an S3 bucket:
### Resource Definition:
We will start by declaring the resource name and the resource type:
```yaml
Resources:
  MyS3Bucket:
    Type: AWS::S3::Bucket
```

### Parameters:
Parameters allow you to input custom values when you create or update a stack. They provide a way to make your CloudFormation templates more flexible and reusable.
Example parameter in the template:

```yaml
Parameters:
  BucketName:
    Type: String
```

You can reference parameters within your template using the `!Ref` function:
```yaml
Resources:
  MyS3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref BucketName
```
### Outputs:

Outputs help you to get useful information after stack creation.
An example output would look like this:

```yaml
Outputs:
  BucketArn:
    Value: !GetAtt MyS3Bucket.Arn
```
### Connecting the dots:
The completed cloudformation file would look like this:
```yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: "A simple CloudFormation template to create an S3 bucket."

Parameters:
  BucketName:
    Type: String
    Description: "Name for the S3 bucket."

Resources:
  MyS3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref BucketName

Outputs:
  BucketArn:
    Value: !GetAtt MyS3Bucket.Arn
    Description: "The Amazon Resource Name (ARN) of the created S3 bucket."
```

### Deploying a Stack:
Now that our cloudformation template file has been created, we will now consider how to deploy the template. You can use the AWS Management Console, AWS CLI, or SDKs to deploy your CloudFormation stack.
You can run this command to deploy the stack using the CLI:
```bash
aws cloudformation create-stack --stack-name MyStack --template-body file://template.yml
```

### Update and Delete:
If any changes have been made to your cloudformation template, you can update your stack with changes to the template using this command.

```bash
aws cloudformation update-stack --stack-name MyStack --template-body file://template.yml
```

Delete the stack when you're done:

```bash
aws cloudformation delete-stack --stack-name MyStack
```
Congratulations!!! You have successfully created and deleted a cloudformation stack.

## Next Steps
Learning never ends. After successfully deploying your cloudformation stack, consider the following suggestions to improve and build on your knowledge:

1. **Documentation**: The importance of documentation cannot be overemphasized. Detail the purpose of each resource and any configuration specifics in your template file. Future you will thank you.
2. **Automation and CI/CD Integration**: Integrate CloudFormation into your continuous integration/continuous deployment (CI/CD) pipeline so you don't have to manage anything manually. Tools like AWS CodePipeline and AWS CodeBuild can be used for this purpose to easily detect and update your cloudformation stack based on changes to the template.

Till next time, keep learning, keep growing👋