DEV Community

Cover image for AWS Deployment with CloudFormation: A Beginner's Guide
Indika_Wimalasuriya
Indika_Wimalasuriya

Posted on

AWS Deployment with CloudFormation: A Beginner's Guide

CloudFormation is a service provided by Amazon Web Services (AWS) that enables the creation and management of AWS resources using a declarative template. It allows you to define your infrastructure as code, specifying the desired state of your resources and their relationships. CloudFormation templates are written in YAML or JSON and can describe a wide range of resources, including EC2 instances, databases, load balancers, and more. By using CloudFormation, you can automate the provisioning and configuration of your infrastructure, ensuring consistency, scalability, and efficiency. It simplifies the process of deploying and managing complex environments, making it an essential tool for infrastructure as code and DevOps practices.

Here are the key components of a CloudFormation template:

  • AWSTemplateFormatVersion: Specifies the CloudFormation template version.
  • Parameters: Defines input parameters that can be customized when creating or updating the stack.
  • Resources: Specifies the AWS resources to be created or modified, such as EC2 instances, S3 buckets, or IAM roles.
  • Outputs: Defines the values that can be retrieved after the stack creation or update, such as instance IP addresses or resource identifiers.
  • Metadata: Optional section for adding arbitrary metadata to the template.
  • Conditions: Allows conditional creation or modification of resources based on the parameter values or other conditions.
  • Transform: Enables the use of AWS CloudFormation macros or other transformations on the template.
  • Mappings: Defines a set of named values that can be referenced in the template.
  • Functions: Includes intrinsic functions like Fn::Sub, Fn::Ref, Fn::ImportValue, and more, to perform transformations and retrieve values.
  • DependsOn: Specifies dependencies between resources to control the order of their creation or modification.

These components work together to define the desired state of your AWS infrastructure and automate the provisioning and management of resources.

Let's create a sample CloudFormation template that provisions an EC2 instance, installs the Apache HTTP server, and renames the default homepage content.

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MyInstance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-080785a633a551d87 # Replace with your desired AMI ID
      InstanceType: t2.micro
      KeyName: YourKeyPairName # Replace with your key pair name
      SecurityGroups:
        - Ref: MySecurityGroup
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash
          yum update -y
          yum install -y httpd
          service httpd start
          chkconfig httpd on
          echo "<h1>Hello CloudFormation</h1>" > /var/www/html/index.html
  MySecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow SSH and HTTP traffic
      SecurityGroupIngress:
        - CidrIp: 0.0.0.0/0
          IpProtocol: tcp
          FromPort: 22
          ToPort: 22
        - CidrIp: 0.0.0.0/0
          IpProtocol: tcp
          FromPort: 80
          ToPort: 80
        - CidrIp: 0.0.0.0/0
          IpProtocol: tcp
          FromPort: 443
          ToPort: 443
Outputs:
  PublicIP:
    Value: !GetAtt MyInstance.PublicIp

Enter fullscreen mode Exit fullscreen mode

Let's go through the CloudFormation code in detail. We will analyze and understand each component of the code to gain a comprehensive understanding of its functionality.

Field Description Example Value
AWSTemplateFormatVersion The version of AWS CloudFormation template format '2010-09-09'
Resources Defines the AWS resources to be created MyInstance and MySecurityGroup
MyInstance Defines an EC2 instance resource Type: AWS::EC2::Instance
ImageId The ID of the Amazon Machine Image (AMI) ami-080785a633a551d87 (replace with desired AMI)
InstanceType The EC2 instance type t2.micro
KeyName The name of the key pair for SSH access YourKeyPairName (replace with your key pair name)
SecurityGroups References the security group for the instance Ref: MySecurityGroup
UserData Custom script to run on instance launch Shell script to update packages and start HTTP server
MySecurityGroup Defines a security group resource Type: AWS::EC2::SecurityGroup
GroupDescription Description of the security group Allow SSH and HTTP traffic
SecurityGroupIngress Inbound rules for the security group Rules to allow SSH (port 22), HTTP (port 80), and HTTPS (port 443) traffic
Outputs Defines the output values of the stack PublicIP: The public IP address of the EC2 instance

Here are the steps to configure and run a CloudFormation template in AWS:

  1. - Prepare your CloudFormation Template: Create or obtain a CloudFormation template written in YAML or JSON format. The template should define the desired AWS resources and their configurations.
  2. - Sign in to the AWS Management Console: Access the AWS Management Console using your AWS account credentials.
  3. - Navigate to CloudFormation: Open the AWS Management Console and navigate to the CloudFormation service.
  4. - Create a new stack: Click on the "Create Stack" button to start the process of creating a new stack.
  5. - Specify the template file: Choose the option to upload a template file and provide the path to the CloudFormation template file you prepared in Step 1.
  6. - Configure stack options: Enter a unique stack name and provide any necessary input parameters as defined in the CloudFormation template. Set other optional parameters as required.
  7. - Review and create the stack: Review the stack details, including the resources to be created, and click on the "Create Stack" button to initiate the stack creation process.
  8. - Monitor stack creation: The CloudFormation service will create the stack and provision the specified resources. You can monitor the progress of stack creation in the AWS Management Console.
  9. - Access stack outputs: Once the stack creation is complete, you can access any specified stack outputs such as instance IP addresses or resource identifiers.

That's it! You have successfully configured and run a CloudFormation template in AWS. The template will automatically create and configure the specified AWS resources based on your defined infrastructure-as-code.

Top comments (0)