askvity

How do I create AWS CloudFormation?

Published in CloudFormation Deployment 3 mins read

To create an AWS CloudFormation stack, follow these steps:

  1. Create or Choose a Template: Start by creating a new CloudFormation template, or use an existing one. Templates are written in either JSON or YAML format. This template defines the AWS resources you want to create and configure.

    • You can find example templates on the AWS website or create your own.
    • Templates specify the resources (e.g., EC2 instances, S3 buckets, databases) and their properties.
  2. Store the Template: Save your template locally or in an Amazon S3 bucket. S3 is a scalable storage service from AWS, allowing you to store and retrieve your template easily.

  3. Create a Stack: Use the AWS CloudFormation service to build a "stack" based on your template. The CloudFormation service reads your template and begins provisioning the resources as you've defined them.

    • Go to the AWS Management Console and navigate to the CloudFormation service.
    • Choose "Create Stack."
    • Upload your template or provide the S3 URL where it's stored.
    • Provide any necessary parameters that the template requires.
    • CloudFormation will then construct and configure the stack resources specified in your template.
  4. CloudFormation Constructs Resources: AWS CloudFormation automatically constructs and configures the stack resources defined in your template (Reference: 10-Sept-2024). CloudFormation handles the provisioning in the correct order, managing dependencies between resources.

Example Scenario: Creating an EC2 Instance

Suppose you want to create a simple EC2 instance using CloudFormation.

  1. Template (example in YAML):
AWSTemplateFormatVersion: "2010-09-09"
Description: "A simple EC2 instance"

Resources:
  MyEC2Instance:
    Type: "AWS::EC2::Instance"
    Properties:
      ImageId: "ami-0c55b73473a34e5e2"  # Replace with a valid AMI ID
      InstanceType: "t2.micro"
  1. Save the Template: Save the YAML file (e.g., ec2_instance.yaml) locally or upload it to an S3 bucket.

  2. Create Stack:

    • In the AWS Management Console, navigate to CloudFormation.
    • Click "Create Stack" -> "With new resources (standard)."
    • Upload your ec2_instance.yaml file.
    • Click "Next," provide a stack name (e.g., "MyEC2Stack"), and click "Next" again.
    • Review the settings, then click "Create Stack."
  3. Monitor Stack Creation: CloudFormation will begin creating the EC2 instance. You can monitor the progress in the CloudFormation console. Once the stack status changes to CREATE_COMPLETE, your EC2 instance will be running.

Related Articles