Pengenalan AWS CloudFormation
Apa Itu CloudFormation?
AWS CloudFormation adalah layanan dari Amazon Web Services yang memungkinkan kamu membuat dan mengelola infrastruktur AWS menggunakan kode. Kamu cukup tulis template berisi deskripsi infrastruktur yang diinginkan, lalu CloudFormation yang akan membuatnya secara otomatis.
CloudFormation termasuk dalam kategori Infrastructure as Code (IaC) — sama seperti Terraform, bedanya CloudFormation khusus untuk AWS saja.
Analoginya: CloudFormation itu seperti cetak biru bangunan. Arsitek (kamu) menggambar cetak birunya, lalu kontraktor (CloudFormation) yang membangunnya sesuai gambar tersebut.
Konsep Dasar CloudFormation
Template
Template adalah file konfigurasi yang berisi deskripsi infrastruktur yang ingin dibuat. Ditulis dalam format YAML atau JSON. Di sinilah kamu mendefinisikan semua resource yang dibutuhkan.
yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: "Template sederhana membuat EC2"
Resources:
ServerSaya:
Type: AWS::EC2::Instance
Properties:
InstanceType: t3.micro
ImageId: ami-0df7a207adb9748c7
Tags:
- Key: Name
Value: server-lksStack
Stack adalah kumpulan resource AWS yang dibuat dari satu template. Ketika kamu menjalankan template, CloudFormation membuat sebuah stack yang berisi semua resource di dalamnya — server, jaringan, database, semuanya dalam satu kesatuan.
Jika stack dihapus, semua resource di dalamnya ikut terhapus secara otomatis.
Stack Set
Jika ingin mendeploy template yang sama ke banyak region atau banyak akun AWS sekaligus, gunakan Stack Set.
Struktur Template CloudFormation
Sebuah template CloudFormation terdiri dari beberapa bagian:
yaml
AWSTemplateFormatVersion: "2010-09-09" # versi format (selalu ini)
Description: "Deskripsi template kamu"
Parameters: # input dari pengguna saat deploy
NamaInstance:
Type: String
Default: "server-lks"
Resources: # WAJIB — resource yang akan dibuat
NamaResource:
Type: AWS::Layanan::TipeResource
Properties:
# konfigurasi resource
Outputs: # nilai yang ingin ditampilkan setelah selesai
IPServer:
Value: !GetAtt NamaResource.PublicIpBagian yang wajib ada hanya Resources. Bagian lainnya opsional.
Contoh Nyata: Membuat VPC + Server
yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: "VPC dan EC2 untuk LKS"
Resources:
# 1. Buat VPC
VPCku:
Type: AWS::EC2::VPC
Properties:
CidrBlock: "10.0.0.0/16"
Tags:
- Key: Name
Value: lks-vpc
# 2. Buat Subnet
SubnetPublik:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPCku
CidrBlock: "10.0.1.0/24"
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: lks-subnet
# 3. Buat Internet Gateway
IGW:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: lks-igw
# 4. Hubungkan IGW ke VPC
AttachIGW:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref VPCku
InternetGatewayId: !Ref IGW
# 5. Buat Server
ServerWeb:
Type: AWS::EC2::Instance
Properties:
InstanceType: t3.micro
ImageId: ami-0df7a207adb9748c7
SubnetId: !Ref SubnetPublik
Tags:
- Key: Name
Value: lks-web-server
Outputs:
IPServer:
Description: "IP publik server"
Value: !GetAtt ServerWeb.PublicIpFungsi Bawaan CloudFormation yang Sering Dipakai
| Fungsi | Kegunaan | Contoh |
|---|---|---|
!Ref | Merujuk nilai resource atau parameter lain | !Ref NamaVPC |
!GetAtt | Mengambil atribut spesifik dari resource | !GetAtt Server.PublicIp |
!Sub | Substitusi string dengan nilai variabel | !Sub "nama-${AWS::Region}" |
!Join | Menggabungkan beberapa string | !Join ["-", [lks, server]] |
!Select | Memilih item dari list | !Select [0, !GetAZs ""] |
Cara Deploy Template
Lewat AWS Console:
- Buka layanan CloudFormation di AWS Console
- Klik Create Stack
- Upload file template YAML/JSON
- Isi parameter jika ada
- Klik Create — tunggu sampai status menjadi
CREATE_COMPLETE
Lewat AWS CLI:
bash
# Deploy template
aws cloudformation create-stack \
--stack-name lks-stack \
--template-body file://template.yaml
# Cek status stack
aws cloudformation describe-stacks \
--stack-name lks-stack
# Hapus stack (beserta semua resource-nya)
aws cloudformation delete-stack \
--stack-name lks-stackCloudFormation vs Terraform
| CloudFormation | Terraform | |
|---|---|---|
| Provider | AWS saja | AWS, GCP, Azure, dll |
| Format | YAML / JSON | HCL |
| State management | Dikelola AWS otomatis | Perlu dikelola sendiri |
| Gratis | Ya, tidak ada biaya tambahan | Ya (open source) |
| Ekosistem | Khusus AWS | Multi-cloud |
Keduanya sama-sama IaC. Pilih CloudFormation jika infrastrukturmu 100% di AWS. Pilih Terraform jika butuh multi-cloud atau lebih fleksibel.
Kesimpulan
CloudFormation adalah cara AWS mengajak penggunanya untuk berhenti konfigurasi manual dan mulai mengelola infrastruktur lewat kode. Dengan satu file template, kamu bisa membuat puluhan resource sekaligus, menghapusnya sekaligus, dan mengulangnya kapan saja dengan hasil yang selalu konsisten.
:
Kirim Komentar: