Examples of Highlight HCL
43 | 44 |Highlight HashiCorp configuration language (HCL) with highlight.js.
45 | 46 |Terraform
47 | 48 |# Basic
49 | 50 |
51 | terraform {
52 | required_providers {
53 | aws = {
54 | source = "hashicorp/aws"
55 | version = "~> 4.16"
56 | }
57 | }
58 |
59 | required_version = ">= 1.2.0"
60 | }
61 |
62 | provider "aws" {
63 | region = "us-west-2"
64 | }
65 |
66 | resource "aws_instance" "app_server" {
67 | ami = "ami-830c94e3"
68 | instance_type = "t2.micro"
69 |
70 | tags = {
71 | Name = "${var.instance_name} - PoC"
72 | }
73 | }
74 |
75 |
76 | # Input Variables
77 | 78 |
79 | variable "image_id" {
80 | type = string
81 | }
82 |
83 | variable "availability_zone_names" {
84 | type = list(string)
85 | default = ["us-west-1a"]
86 | }
87 |
88 | variable "docker_ports" {
89 | type = list(object({
90 | internal = number
91 | external = number
92 | protocol = string
93 | }))
94 |
95 | default = [
96 | {
97 | internal = 8300
98 | external = 8300
99 | protocol = "tcp"
100 | }
101 | ]
102 | }
103 |
104 |
105 | # Output Values
106 | 107 |
108 | output "instance_ip_addr" {
109 | value = aws_instance.server["host_1"].private_ip
110 | }
111 |
112 | output "instance_ip_addr" {
113 | value = aws_instance.server[0].private_ip
114 | }
115 |
116 |
117 | # Local Values
118 | 119 |
120 | locals {
121 | # Ids for multiple sets of EC2 instances, merged together
122 | instance_ids = concat(aws_instance.blue.*.id, aws_instance.green.*.id)
123 | }
124 |
125 | locals {
126 | # Common tags to be assigned to all resources
127 | common_tags = {
128 | Service = local.service_name
129 | Owner = local.owner
130 | }
131 | }
132 |
133 |
134 | # For Loop
135 | 136 |
137 | variable "users" {
138 | type = map(object({
139 | is_admin = bool
140 | }))
141 | }
142 |
143 | locals {
144 | admin_users = {
145 | for name, user in var.users : name => user
146 | if user.is_admin
147 | }
148 | regular_users = {
149 | for name, user in var.users : name => user
150 | if !user.is_admin
151 | }
152 | }
153 |
154 |
155 | # Module
156 | 157 |
158 | module "servers" {
159 | source = "./app-cluster"
160 |
161 | servers = 5
162 | }
163 |
164 |
165 | # Meta-Arguments
166 | 167 |
168 | resource "aws_instance" "example" {
169 | ami = "ami-a1b2c3d4"
170 | instance_type = "t2.micro"
171 |
172 | iam_instance_profile = aws_iam_instance_profile.example
173 |
174 | # The depends_on Meta-Argument
175 | depends_on = [
176 | aws_iam_role_policy.example,
177 | aws_iam_role_policy.example_two
178 | ]
179 | }
180 |
181 | resource "azurerm_resource_group" "rg" {
182 | # The for_each Meta-Argument
183 | for_each = tomap({
184 | a_group = "eastus"
185 | another_group = "westus2"
186 | })
187 |
188 | name = each.key
189 | location = each.value
190 | }
191 |
192 |
193 | # Heredocs
194 | 195 |
196 | block {
197 | value = <<EOT
198 | hello
199 | world
200 | EOT
201 | }
202 |
203 | block {
204 | value = <<-EOT
205 | hello
206 | world
207 | EOT
208 | }
209 |
210 |
211 | Packer
212 | 213 |# Basic
214 | 215 |
216 | packer {
217 | required_plugins {
218 | amazon = {
219 | version = ">= 1.2.8"
220 | source = "github.com/hashicorp/amazon"
221 | }
222 | }
223 | }
224 |
225 | source "amazon-ebs" "ubuntu" {
226 | ami_name = "learn-packer-linux-aws"
227 | instance_type = "t2.micro"
228 | region = "us-west-2"
229 | source_ami_filter {
230 | filters = {
231 | name = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*"
232 | root-device-type = "ebs"
233 | virtualization-type = "hvm"
234 | }
235 | most_recent = true
236 | owners = ["099720109477"]
237 | }
238 | ssh_username = "ubuntu"
239 | }
240 |
241 | build {
242 | name = "learn-packer"
243 | sources = [
244 | "source.amazon-ebs.ubuntu"
245 | ]
246 | }
247 |
248 |