AWS CloudFormation is a powerful tool provided by Amazon Web Services that allows you to use a simple text file to model and provision all the resources needed for your applications across all regions and accounts. This file is the template, and the format can be either JSON or YAML. Here’s how to set up basic configurations using CloudFormation, along with specific examples of templates.

Setting Up AWS CloudFormation

Prerequisites

  1. AWS Account: You need an active AWS account.
  2. AWS CLI: Optionally, you might want to install the AWS CLI for deploying and managing your stacks via the command line.

Step 1: Create a CloudFormation Template

CloudFormation templates are structured data files that define the resources you need to deploy for your application. Here’s a simple example that creates an Amazon EC2 instance.

Example: EC2 Instance

YAML Format

AWSTemplateFormatVersion: '2010-09-09'
Description: Simple EC2 Instance Example
Resources:
  MyEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0c55b159cbfafe1f0
      InstanceType: t2.micro
      SecurityGroups:
        - !Ref MySecurityGroup
      KeyName: my-key-pair

  MySecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow SSH access from anywhere
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0

JSON Format

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Simple EC2 Instance Example",
  "Resources": {
    "MyEC2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "ImageId": "ami-0c55b159cbfafe1f0",
        "InstanceType": "t2.micro",
        "SecurityGroups": [
          { "Ref": "MySecurityGroup" }
        ],
        "KeyName": "my-key-pair"
      }
    },
    "MySecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Allow SSH access from anywhere",
        "SecurityGroupIngress": [
          {
            "IpProtocol": "tcp",
            "FromPort": 22,
            "ToPort": 22,
            "CidrIp": "0.0.0.0/0"
          }
        ]
      }
    }
  }
}

Step 2: Deploy the Template

You can deploy this template directly from the AWS Management Console, or use the AWS CLI.

Using the AWS CLI:

aws cloudformation create-stack --stack-name MyEC2Stack --template-body file://mytemplate.yaml

Replace mytemplate.yaml with the path to your template file.

Step 3: Monitor and Manage the Stack