600===Dev AWS/Cloud Formation

Cloud Formation Introduced

블로글러 2024. 5. 28. 12:36

AWS CloudFormation is a service that helps you model and set up your Amazon Web Services (AWS) resources so that you can spend less time managing those resources and more time focusing on your applications.


The Big Picture

Imagine you're building a city. Instead of manually placing each building, road, and utility one by one, you create a master plan, a blueprint that defines every detail of your city. When you hand this blueprint to a construction team, they build the entire city for you based on your specifications. AWS CloudFormation acts like that master plan for your cloud infrastructure. It allows you to define your infrastructure as code (IaC), automating the creation and management of AWS resources in a consistent and repeatable way.

Core Concepts

  1. Templates: These are JSON or YAML files that describe the AWS resources you want to create and configure. Think of it as the detailed blueprint for your cloud infrastructure.
  2. Stacks: These are collections of AWS resources that you create and manage as a single unit. When you deploy a template, it creates a stack.
  3. Change Sets: These are previews of the changes that will be made to your stack. It allows you to review potential changes before they are applied, helping to avoid unexpected issues.
  4. Resources: These are the AWS components such as EC2 instances, S3 buckets, and IAM roles that you define in your templates and manage within your stacks.

Detailed Walkthrough

  1. Templates:

    • A CloudFormation template defines your infrastructure and its configuration. It specifies resources, their properties, dependencies, and how they interact.
    • Example structure of a YAML template:
      AWSTemplateFormatVersion: '2010-09-09'
      Description: A simple EC2 instance
      Resources:
        MyEC2Instance:
          Type: 'AWS::EC2::Instance'
          Properties:
            InstanceType: t2.micro
            ImageId: ami-0ff8a91507f77f867
  2. Stacks:

    • Once you have your template, you use it to create a stack. The stack manages the lifecycle of all resources defined in the template.
    • Creating a stack can be done through the AWS Management Console, AWS CLI, or AWS SDKs.
    • Example AWS CLI command to create a stack:
      aws cloudformation create-stack --stack-name MyStack --template-body file://template.yaml
  3. Change Sets:

    • Before updating a stack, you can create a change set to see how your changes will impact the existing resources.
    • Example AWS CLI command to create a change set:
      aws cloudformation create-change-set --stack-name MyStack --template-body file://updated-template.yaml --change-set-name MyChangeSet
  4. Resources:

    • Each resource in a template is defined with a type and properties specific to that type.
    • Example of defining an S3 bucket in a template:
      Resources:
        MyS3Bucket:
          Type: 'AWS::S3::Bucket'
          Properties:
            BucketName: my-unique-bucket-name

Understanding Through an Example

Let's say you want to deploy a web application. You need an EC2 instance for your web server, an RDS instance for your database, and an S3 bucket for storing static assets.

  1. Define the template:

    AWSTemplateFormatVersion: '2010-09-09'
    Description: Web application infrastructure
    Resources:
      WebServer:
        Type: 'AWS::EC2::Instance'
        Properties:
          InstanceType: t2.micro
          ImageId: ami-0ff8a91507f77f867
      Database:
        Type: 'AWS::RDS::DBInstance'
        Properties:
          DBInstanceClass: db.t2.micro
          Engine: mysql
          MasterUsername: admin
          MasterUserPassword: password
      StaticAssets:
        Type: 'AWS::S3::Bucket'
        Properties:
          BucketName: my-app-static-assets
  2. Create a stack using the template:

    aws cloudformation create-stack --stack-name WebAppStack --template-body file://webapp-template.yaml
  3. Review and apply changes with change sets:

    aws cloudformation create-change-set --stack-name WebAppStack --template-body file://updated-webapp-template.yaml --change-set-name UpdateChangeSet
    aws cloudformation execute-change-set --change-set-name UpdateChangeSet --stack-name WebAppStack

Conclusion and Summary

AWS CloudFormation simplifies the process of setting up and managing your AWS resources by allowing you to define your infrastructure as code. By using templates, you can create, update, and delete a collection of resources as a single unit (stack), ensuring consistency and efficiency. Change sets provide a safety net by allowing you to preview changes before applying them.

Test Your Understanding

  1. What is the primary purpose of AWS CloudFormation?
  2. Describe what a CloudFormation template is and give an example of what it might include.
  3. How does a change set help prevent issues during updates?

Reference

For more detailed information, you can refer to the AWS CloudFormation User Guide.

728x90