Skip to main content

M04.04 - Infrastructure and Automation

Tại thời điểm này, bạn đã có hiểu biết vững chắc về basics của AWS infrastructure. Bạn biết sự khác biệt giữa Regions, Availability Zones, và edge locations. Bạn cũng nhận ra benefits của việc deploying resources đến multiple Regions, Availability Zones, hoặc cả hai, và bạn có thể bắt đầu đưa ra một số quyết định về Region hoặc Regions mà business của bạn sẽ chọn. Trong bài học này, bạn sẽ xem xét các methods để automating các loại deployments này. Infrastructure as code (IaC) tools như CloudFormation có thể giúp bạn scale resources của mình từ một location đến location khác với minimal effort.

Mục tiêu học tập

Sau khi hoàn thành bài học này, bạn sẽ có thể:

  • ✅ Mô tả key features và benefits của CloudFormation
  • ✅ Xác định use cases cho programmatic access, AWS Management Console, và infrastructure as code
  • ✅ Hiểu về Infrastructure as Code (IaC) và cách nó hoạt động
  • ✅ So sánh các phương pháp tương tác với AWS resources

Vấn đề với Manual Provisioning

Bây giờ, bạn biết rằng để manage AWS resources, bạn phải interact với AWS APIs. Bạn đã học cách làm điều này bằng cách sử dụng Management Console, CLI, và SDKs. Nhưng điều gì xảy ra khi bạn cần create và manage multiple resources, có thể across multiple AWS Regions hoặc multiple accounts, và bạn muốn đảm bảo mọi thứ consistent và repeatable?

Ví dụ thực tế: Giả sử bạn có resources trong Region A, và bạn muốn launch chúng trong Region B cho high availability. Bạn có thể set everything up manually bằng cách clicking through console hoặc running commands, remembering tất cả configurations của bạn khi bạn đi, nhưng điều đó:

  • Slow - Chậm
  • Error-prone - Dễ mắc lỗi
  • 🔄 Hard to reproduce - Khó tái tạo

Hoặc bạn có thể sử dụng automation và đây là nơi concept của infrastructure as code, hoặc IaC, xuất hiện.

Infrastructure as Code (IaC)

Khái niệm

Infrastructure as Code (IaC) là practice của việc managing và provisioning infrastructure thông qua machine-readable definition files, thay vì physical hardware configuration hoặc interactive configuration tools.

Với IaC, bạn có thể:

  • 📝 Define infrastructure trong một file - Giống như một blueprint cho AWS architecture của bạn
  • 🔧 Sử dụng tools hoặc services để automatically build và configure resources của bạn dựa trên blueprint specifications
  • 🔄 Deploy cùng một setup nhiều lần mà không có variation
  • 📊 Track changes đến infrastructure của bạn hiệu quả hơn bằng cách sử dụng source control

Lợi ích của IaC

  • Consistency - Nhất quán trong deployments
  • Repeatability - Có thể tái tạo
  • Version Control - Quản lý phiên bản
  • Automation - Tự động hóa
  • Reduced Errors - Giảm lỗi
  • Faster Deployments - Triển khai nhanh hơn

AWS CloudFormation

AWS CloudFormation là một IaC service mà bạn có thể sử dụng để define một wide variety của AWS resources theo cách declarative bằng cách tạo text-based documents được gọi là CloudFormation templates.

AWS CloudFormation - Infrastructure as Code

Cách CloudFormation hoạt động

Bạn có thể define resources bạn muốn build mà không cần specify details của exactly how to build it. CloudFormation parses template và sau đó provisions tất cả resources bạn đã define, calling needed AWS APIs trong background để make it all happen.

Quy trình:

  1. 📝 Create template - Tạo CloudFormation template mô tả infrastructure
  2. 🚀 Deploy template - Deploy template đến AWS
  3. ⚙️ CloudFormation provisions - CloudFormation tự động provision và configure resources
  4. Resources ready - Resources sẵn sàng sử dụng

Key Features của CloudFormation

Theo AWS CloudFormation documentation:

1. Scale your infrastructure

Scale infrastructure worldwide và manage resources across all AWS accounts và regions through a single operation.

  • 🌍 Global deployment - Triển khai toàn cầu
  • 📊 Multi-account management - Quản lý nhiều tài khoản
  • 🔄 Single operation - Một thao tác duy nhất

2. Extend and manage your infrastructure

Extend và manage infrastructure để include cloud resources published trong AWS CloudFormation Registry, developer community, và library của bạn.

  • 📚 CloudFormation Registry - Registry của AWS và community
  • 🔧 Extensible - Có thể mở rộng
  • 📦 Reusable templates - Templates có thể tái sử dụng

3. Automate resource management

Automate resource management across organization của bạn với AWS service integrations offering turnkey application distribution và governance controls.

  • 🤖 Automation - Tự động hóa
  • 🏢 Organization-wide - Toàn tổ chức
  • 🎯 Governance controls - Kiểm soát quản trị

Benefits của CloudFormation

Khi bạn deploy cùng một template trong multiple accounts hoặc multiple Regions, identical environments được tạo across chúng.

  • Less room for human error - Ít chỗ cho lỗi con người, vì nó là một totally automated process
  • Time saving - Tiết kiệm thời gian
  • 🔄 Consistency - Nhất quán
  • 📊 Version control - Quản lý phiên bản
  • 🛡️ Resilience - Khả năng phục hồi

Ví dụ: Thay vì manually setting up resources trong Region B để match Region A, bạn có thể create một CloudFormation template định nghĩa everything infrastructure của bạn cần. Với một single command, AWS provisions những resources đó exactly as defined.

Và, hey, look at that, bạn đã saved time, reduced margin for error, và made architecture của bạn more resilient. Nice work! 🎉

Interacting với AWS Resources

Như bạn đã học, có một số ways bạn có thể operate trong AWS Cloud. Để interact với AWS resources, bạn phải invoke AWS APIs. Để interact với các APIs này, bạn có thể sử dụng AWS SDKs, AWS Command Line Interface (AWS CLI), AWS Management Console, hoặc IaC tools như CloudFormation.

Three Ways to Interact with AWS Resources

Để review functions và use cases cho việc sử dụng các approaches này, hãy chọn từng numbered marker:

1. Programmatic Access (Truy cập có lập trình)

Programmatic access bao gồm các options như AWS CLI và AWS SDKs. Các options này best suited cho developers và những người familiar với coding languages.

AWS CLI (Command Line Interface)

Với AWS CLI, bạn manage multiple AWS services directly từ command line. Bạn có thể automate tasks through scripts.

Use cases cho AWS CLI:

  • 🤖 Automate routine tasks - Tự động hóa các tác vụ thường xuyên
  • 📝 Scripting - Viết scripts để automate workflows
  • 🔄 Example: Bạn có thể write một script để provide routine backups cho một service như Amazon Elastic Block Store (Amazon EBS)

Đặc điểm:

  • 💻 Command-line based - Dựa trên dòng lệnh
  • 🔧 Scriptable - Có thể viết script
  • Fast - Nhanh
  • 🎯 Precise - Chính xác

AWS SDKs (Software Development Kits)

AWS SDKs có thể help integrate AWS services vào applications của bạn bằng cách providing APIs cho various programming languages. AWS provides documentation và sample code để help bạn get started với việc sử dụng SDKs.

Use cases cho SDKs:

  • 🔌 Invoke APIs cho một phần của application process - Gọi APIs cho một phần của quy trình ứng dụng
  • 💾 Example: Bạn có thể sử dụng một SDK để store user data trong một AWS storage service như Amazon Simple Storage Service (Amazon S3)

Đặc điểm:

  • 💻 Language-specific - Cụ thể theo ngôn ngữ
  • 🔧 Integration - Tích hợp vào applications
  • 📚 Well-documented - Được tài liệu hóa tốt
  • 🎯 Application-focused - Tập trung vào ứng dụng

2. AWS Management Console

AWS Management Console là một web interface mà bạn sử dụng để manage AWS services, offering quick access đến services, search functionality, và simplified workflows. Console là một great option cho những người mới với cloud hoặc users với minimal hoặc no development experience.

Use cases cho Console:

  • 💰 Billing và cost optimization dashboards và visualizations
  • 📊 Services focused on graphical representations, như Amazon QuickSight và Amazon Neptune
  • 🎓 Learning AWS services - Học các dịch vụ AWS
  • 🧪 Testing và experimentation - Kiểm thử và thử nghiệm
  • 👁️ Visual management - Quản lý trực quan

Đặc điểm:

  • 🌐 Web-based - Dựa trên web
  • 👁️ Visual - Trực quan
  • 🎓 User-friendly - Thân thiện với người dùng
  • 📊 Dashboards - Bảng điều khiển

3. Infrastructure as Code (IaC)

Với IaC tools như CloudFormation, bạn có thể automate resource management across organization của bạn với AWS service integrations offering efficient và repeatable resource creation và management.

Use cases cho CloudFormation:

  • 🔄 Managing infrastructure với DevOps như continuous integration và delivery (CI/CD) pipelines
  • 📈 Scaling resources như Amazon EC2 instances đến multi-Region applications trong một consistent, repeatable way
  • 🏢 Multi-environment deployments - Triển khai nhiều môi trường
  • 🔄 Disaster recovery - Phục hồi thảm họa
  • 📊 Infrastructure versioning - Quản lý phiên bản hạ tầng

Đặc điểm:

  • 📝 Template-based - Dựa trên template
  • 🤖 Automated - Tự động hóa
  • 🔄 Repeatable - Có thể tái tạo
  • 📊 Version controlled - Quản lý phiên bản
  • 🏢 Organization-wide - Toàn tổ chức

So sánh các phương pháp

Đặc điểmProgrammatic AccessAWS Management ConsoleInfrastructure as Code
Target UsersDevelopersBeginners, Non-developersDevOps, Engineers
ComplexityMedium to HighLowMedium to High
AutomationScript-basedManualTemplate-based
RepeatabilityHigh (với scripts)LowVery High
SpeedFastMediumVery Fast (sau khi setup)
Error-proneLow (với scripts)HighVery Low
Best forAutomation, ScriptingLearning, One-off tasksProduction, Multi-environment

CloudFormation Use Cases

1. Manage infrastructure với DevOps

Automate, test, và deploy infrastructure templates với continuous integration và delivery (CI/CD) automations.

  • 🔄 CI/CD integration - Tích hợp CI/CD
  • 🧪 Testing - Kiểm thử
  • 🚀 Automated deployment - Triển khai tự động

2. Scale production stacks

Run anything từ một single Amazon EC2 instance đến một complex multi-region application.

  • 📈 Single to complex - Từ đơn giản đến phức tạp
  • 🌍 Multi-region - Đa khu vực
  • 🏢 Production-ready - Sẵn sàng cho sản xuất

3. Share best practices

Define một Amazon Virtual Private Cloud (VPC) subnet hoặc provisioning services như AWS OpsWorks hoặc Amazon Elastic Container Service (ECS) với ease.

  • 📚 Best practices - Thực hành tốt nhất
  • 🔄 Reusable templates - Templates có thể tái sử dụng
  • 🎯 Standardization - Chuẩn hóa

Test Your Skills

Một rapidly growing tech startup company đang lên kế hoạch launch một new web application sẽ require một complex infrastructure setup, bao gồm multiple Amazon EC2 instances, Elastic Load Balancing, và Auto Scaling groups. Application phải được deployed consistently across different environments.

Câu hỏi:

Would AWS CloudFormation be a good solution for managing the company's infrastructure?

AWS CloudFormation có phải là một giải pháp tốt để quản lý infrastructure của công ty không?

📋 Xem các lựa chọn
  • CloudFormation would not be useful in this scenario because it only supports simple infrastructure setups.
    • CloudFormation sẽ không hữu ích trong scenario này vì nó chỉ hỗ trợ các thiết lập infrastructure đơn giản.
  • CloudFormation would be ideal because it supports infrastructure as code (IaC), enabling consistent, repeatable deployments across different environments.
    • CloudFormation sẽ lý tưởng vì nó hỗ trợ infrastructure as code (IaC), cho phép triển khai nhất quán, có thể tái tạo trên các môi trường khác nhau.
  • CloudFormation should be used only for setting up static websites, not for complex applications.
    • CloudFormation chỉ nên được sử dụng để thiết lập các trang web tĩnh, không phải cho các ứng dụng phức tạp.
  • CloudFormation is too complicated and would slow down the deployment process.
    • CloudFormation quá phức tạp và sẽ làm chậm quá trình triển khai.
✅ Đáp án đúng

Đáp án đúng:

  • CloudFormation would be ideal because it supports infrastructure as code (IaC), enabling consistent, repeatable deployments across different environments.

Giải thích:

CloudFormation highly capable của việc managing complex infrastructure setups, không chỉ simple ones. Nó supports một wide range của AWS services và có thể được sử dụng để define intricate dependencies và configurations.

Trong scenario này:

  • 🏗️ Complex infrastructure - Multiple EC2 instances, ELB, Auto Scaling groups
  • 🔄 Consistent deployments - Deploy consistently across different environments
  • 📊 Repeatable - Cần có thể tái tạo

CloudFormation là ideal solution vì:

  • Infrastructure as Code - Define infrastructure trong templates
  • Consistency - Đảm bảo consistency across environments
  • Repeatability - Có thể deploy cùng một setup nhiều lần
  • Complex setups - Hỗ trợ complex infrastructure setups
  • Multi-service - Hỗ trợ multiple AWS services cùng lúc

Các lựa chọn không đúng:

  • "Only supports simple infrastructure": CloudFormation hỗ trợ cả simple và complex infrastructure
  • "Only for static websites": CloudFormation có thể được sử dụng cho bất kỳ loại application nào
  • "Too complicated and slow": CloudFormation thực sự làm cho deployments nhanh hơn và ít lỗi hơn sau khi setup ban đầu

Tóm tắt

Trong bài học này, bạn đã học về:

  • Vấn đề với Manual Provisioning: Slow, error-prone, và hard to reproduce
  • Infrastructure as Code (IaC): Practice của việc managing infrastructure thông qua definition files
  • AWS CloudFormation: IaC service để define và provision AWS resources
  • Key Features của CloudFormation:
    • Scale infrastructure worldwide
    • Extend và manage infrastructure
    • Automate resource management
  • Three Ways to Interact với AWS:
    1. Programmatic Access (CLI, SDKs) - Cho developers
    2. AWS Management Console - Cho beginners
    3. Infrastructure as Code (CloudFormation) - Cho DevOps
  • Use Cases: Khi nào sử dụng mỗi phương pháp
  • Benefits: Consistency, repeatability, automation, reduced errors

Next Steps

Trong bài học tiếp theo, bạn sẽ:

  • Hoàn thành Module 4 Assessment
  • Review lại tất cả concepts đã học trong Module 4
  • Chuẩn bị cho Module Summary

Hãy tiếp tục với Module 4 Assessment để kiểm tra kiến thức của bạn! 📝