Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izmilia-prastika/a7717eebbab67d8f8bfb869bd8ff63a3 to your computer and use it in GitHub Desktop.
Save izmilia-prastika/a7717eebbab67d8f8bfb869bd8ff63a3 to your computer and use it in GitHub Desktop.
contoh main.tf AWS
# Provider yang digunakan adalah AWS
# Versi dikunci pada 3.22.x
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.22.0"
}
}
}
provider "aws" {
region = "ap-southeast-1"
profile = "teknocerdas-infra" # Ganti dengan profile AWS CLI profile anda
}
# Ambil data VPC yang memiliki attribute default = true
data "aws_vpc" "demo" {
default = true
}
# Ambil data AMI untuk Ubuntu Server 20.04 LTS
data "aws_ami" "demo_ami" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
# Buat sebuah SSH Key pair baru yang akan digunakan untuk SSH login
resource "aws_key_pair" "demo-ssh" {
key_name = "my-demo-key"
public_key = chomp(file(var.ssh_key_file))
}
# Security group. Hanya buka port 22 SSH.
resource "aws_security_group" "demo" {
name = "my-demo-firewall"
name_prefix = null
description = "Firewall for Demo"
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
# Allowed IPs ambil dari variabel yang sudah dibuat sebelumnya.
cidr_blocks = var.ssh_allowed_ips
}
egress {
description = "Allow ouput for all"
from_port = 0
to_port = 0
protocol = "-1" # All
cidr_blocks = ["0.0.0.0/0"]
}
vpc_id = data.aws_vpc.demo.id
tags = var.default_tags
}
resource "aws_instance" "demo" {
ami = data.aws_ami.demo_ami.id
instance_type = var.ec2.type
key_name = "my-demo-key"
availability_zone = var.ec2.az
tags = var.default_tags
volume_tags = var.default_tags
vpc_security_group_ids = [aws_security_group.demo.id]
# Ukuran dari storage
root_block_device {
volume_type = "gp3"
volume_size = 32
delete_on_termination = true
encrypted = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment