├── .gitignore ├── LICENSE ├── README.md ├── aws ├── ec2-ebs │ ├── instance.tf │ ├── keypairs.tf │ ├── provider.tf │ ├── securitygroup.tf │ ├── variables.tf │ ├── versions.tf │ └── vpc.tf ├── ec2 │ ├── instance.tf │ ├── keypairs.tf │ ├── provider.tf │ ├── securitygroup.tf │ ├── variables.tf │ ├── versions.tf │ └── vpc.tf ├── ecr │ ├── ecr.tf │ ├── output.tf │ ├── provider.tf │ ├── vars.tf │ └── versions.tf └── simple-vpc │ ├── private-vpc.tf │ ├── provider.tf │ ├── public-vpc.tf │ ├── variables.tf │ ├── versions.tf │ └── vpc-main.tf ├── datasources ├── provider.tf ├── securitygroup.tf ├── variables.tf └── versions.tf ├── modules ├── environments │ ├── dev │ │ └── main.tf │ └── prod │ │ └── main.tf ├── main.tf ├── provider.tf ├── simple_modules │ ├── elasticache-cluster.tf │ ├── variables.tf │ └── versions.tf └── variables.tf ├── packer ├── README.md ├── packer-example.json ├── provider.tf ├── variables.tf └── versions.tf ├── simple ├── terraform.tfvars └── variable.tf └── spinning ├── instance.tf ├── provider.tf └── variable.tf /.gitignore: -------------------------------------------------------------------------------- 1 | # varirables files 2 | **/terraform.tfvars 3 | !/simple/terraform.tfvars 4 | **/*.ppk 5 | **/*.pub 6 | **/*-public 7 | 8 | # Local .terraform directories 9 | **/.terraform/* 10 | 11 | # .tfstate files 12 | *.tfstate 13 | *.tfstate.* 14 | 15 | # Crash log files 16 | crash.log 17 | 18 | # Ignore any .tfvars files that are generated automatically for each Terraform run. Most 19 | # .tfvars files are managed as part of configuration and so should be included in 20 | # version control. 21 | # 22 | # example.tfvars 23 | 24 | # Ignore override files as they are usually used to override resources locally and so 25 | # are not checked in 26 | override.tf 27 | override.tf.json 28 | *_override.tf 29 | *_override.tf.json 30 | 31 | # Include override files you do wish to add to version control using negated pattern 32 | # 33 | # !example_override.tf 34 | 35 | # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan 36 | # example: *tfplan* 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Awesome Terraform 2 | A summarize of terraform usage and example 3 | 4 | ## Advantage of Terraform 5 | - Infrastruture as Code 6 | - Automation of your Infrastructure 7 | - Infrastructure auditable 8 | - Keep infrastructure in certain state 9 | 10 | ## Prerequisite 11 | - [Terraform with brew](https://brewinstall.org/install-terraform-on-mac-with-brew/) 12 | 13 | ## Terraform Command 14 | 15 | start with *terraform* 16 | 17 | - init 18 | - every time before do anythings 19 | - plan 20 | - check infrasturcture that is going to provision 21 | - apply 22 | - apply infrastructure to provider 23 | - console 24 | 25 | ### Note 26 | - Terraform v0.12 now allows you to use expressions directly when defining most attributes. No need to use interpolation any more 27 | 28 | ``` 29 | # Old 0.11 example 30 | tags = "${merge(map("Name", "example"), var.common_tags)}" 31 | 32 | # Updated 0.12 example 33 | tags = merge({ Name = "example" }, var.common_tags) 34 | ``` 35 | 36 | - To run an example of Terraform please provide *Terraform.tfvars* that contains AWS credentail on those folder 37 | 38 | ## Terraform Syntax 39 | 40 | Terraform will interpret with *tf* extension 41 | 42 | 3 main things are 43 | - Type 44 | - Blocks 45 | - Arguments 46 | 47 | ## Terraform Objects 48 | ### Resources 49 | 50 | > Resources are the most important element in the Terraform language 51 | 52 | Each resource is associated with a single resource type, which determines the kind of infrastructure object 53 | 54 | ``` 55 | resource "type" "name" { 56 | arguments = value 57 | } 58 | 59 | e.g. resource type aws_instance 60 | 61 | resource "aws_instance" "web" { 62 | ami = "ami-a1b2c3d4" 63 | instance_type = "t2.micro" 64 | } 65 | 66 | ``` 67 | 68 | Each resource type in turn belongs to a [provider](https://www.terraform.io/docs/providers/index.html) 69 | 70 | ``` 71 | e.g. aws 72 | 73 | provider "aws" { 74 | 75 | } 76 | ``` 77 | 78 | #### Meta-Arguments 79 | 80 | Meta-arguments, which can be used with any resource type to change the behavior of resources 81 | 82 | - [depends_on](https://www.terraform.io/docs/configuration/resources.html#depends_on-explicit-resource-dependencies) 83 | - for specifying hidden dependencies 84 | - [count](https://www.terraform.io/docs/configuration/resources.html#count-multiple-resource-instances-by-count) 85 | - for creating multiple resource instances according to a count 86 | - [for_each](https://www.terraform.io/docs/configuration/resources.html#for_each-multiple-resource-instances-defined-by-a-map-or-set-of-strings) 87 | - to create multiple instances according to a map, or set of strings 88 | - [provider](https://www.terraform.io/docs/configuration/resources.html#provider-selecting-a-non-default-provider-configuration) 89 | - for selecting a non-default provider configuration 90 | - [lifecycle](https://www.terraform.io/docs/configuration/resources.html#lifecycle-lifecycle-customizations) 91 | - for lifecycle customizations 92 | - [provisioner and connection](https://www.terraform.io/docs/configuration/resources.html#provisioner-and-connection-resource-provisioners) 93 | - for taking extra actions after resource creation 94 | 95 | ### Variable 96 | 97 | - Input Variable 98 | 99 | > Input variables serve as parameters for a Terraform module 100 | 101 | - Variable Type 102 | - string 103 | - bool 104 | - number 105 | - list 106 | - set 107 | - map 108 | - object 109 | - tuple 110 | 111 | ``` 112 | variable "image_id" { 113 | type = string 114 | } 115 | 116 | variable "availability_zone_names" { 117 | type = list(string) 118 | default = ["us-west-1a"] 119 | } 120 | 121 | usage by var. 122 | 123 | resource "aws_instance" "example" { 124 | instance_type = "t2.micro" 125 | ami = var.image_id 126 | } 127 | ``` 128 | 129 | - Variable Definitions Files 130 | 131 | > To set lots of variables, it is more convenient to specify their values in a variable definitions file (either .tfvars or .tfvars.json) 132 | 133 | ``` 134 | .tfvars 135 | 136 | image_id = "ami-abc123" 137 | availability_zone_names = [ 138 | "us-east-1a", 139 | "us-west-1c", 140 | ] 141 | 142 | .tfvars.json 143 | 144 | { 145 | "image_id": "ami-abc123", 146 | "availability_zone_names": ["us-west-1a", "us-west-1c"] 147 | } 148 | 149 | ``` 150 | 151 | - Output Variable 152 | 153 | > Output values are like the return values of a Terraform module 154 | 155 | ``` 156 | output "instance_ip_addr" { 157 | value = aws_instance.server.private_ip 158 | } 159 | ``` 160 | 161 | *Outputs are only rendered when Terraform applies your plan. Running terraform plan will not render outputs* 162 | 163 | ### Datasources 164 | 165 | > allow data to be fetched or computed for use elsewhere in Terraform configuration 166 | 167 | ``` 168 | # Find the latest available AMI that is tagged with Component = web 169 | data "aws_ami" "web" { 170 | filter { 171 | name = "state" 172 | values = ["available"] 173 | } 174 | 175 | filter { 176 | name = "tag:Component" 177 | values = ["web"] 178 | } 179 | 180 | most_recent = true 181 | } 182 | 183 | resource "aws_instance" "web" { 184 | ami = data.aws_ami.web.id 185 | instance_type = "t1.micro" 186 | } 187 | ``` 188 | 189 | Each [provider](https://www.terraform.io/docs/configuration/providers.html) may offer data sources alongside its set of [resource types](https://www.terraform.io/docs/configuration/resources.html#resource-types-and-arguments) 190 | 191 | ### Provisioners 192 | 193 | > Terraform includes the concept of provisioners as a measure of pragmatism, knowing that there will always be certain behaviors that can't be directly represented in Terraform's declarative model 194 | 195 | #### local-exec Provisioner 196 | 197 | invokes a local executable after a resource is created 198 | 199 | ``` 200 | resource "aws_instance" "web" { 201 | provisioner "local-exec" { 202 | command = "echo ${aws_instance.web.private_ip} >> private_ips.txt" 203 | } 204 | } 205 | ``` 206 | 207 | ### Template Provider 208 | 209 | > The template provider exposes data sources to use templates to generate strings for other Terraform resources or outputs 210 | 211 | ``` 212 | # Template for initial configuration bash script 213 | data "template_file" "init" { 214 | template = "${file("init.tpl")}" 215 | vars = { 216 | consul_address = "${aws_instance.consul.private_ip}" 217 | } 218 | } 219 | 220 | # Create a web server 221 | resource "aws_instance" "web" { 222 | # ... 223 | 224 | user_data = "${data.template_file.init.rendered}" 225 | } 226 | ``` 227 | 228 | ## State 229 | 230 | > Terraform keeps thre remote state of the infrastructure 231 | 232 | - Terraform stores state locally in a file named *terraform.tfstate* 233 | - Backup the previous state in *terraform.tfstate.backup* 234 | - When you execute *terraform apply*, a new *terraform.tfstate* and backup is written 235 | - If thre remote state changes and you hit terraform apply again, terraform will make changes to meet the correct remote state again 236 | 237 | ### Remote state 238 | 239 | > The terraform state can be saved remote, using *backend* functionality in terraform 240 | 241 | Example Backend 242 | - S3 243 | - Consul 244 | - terraform enterprise 245 | 246 | ### Backends Benefit 247 | 248 | - Working in a team 249 | - Backends can store their state remotely and protect that state with locks to prevent corruption 250 | - Keeping sensitive information off disk 251 | - The state ever is persisted not in memory 252 | - The default is a local backend (the local file) 253 | - Remote operations 254 | - For larger infrastructures or certain changes, terraform apply can take a long, long time. Some backends support remote operations which enable the operation to execute remotely 255 | 256 | ### Backend Configuration 257 | 258 | configured directly in Terraform files in the terraform section 259 | 260 | ``` 261 | # example configuring the "consul" backend 262 | 263 | terraform { 264 | backend "consul" { 265 | address = "demo.consul.io" 266 | scheme = "https" 267 | path = "example_app/terraform_state" 268 | } 269 | } 270 | ``` 271 | 272 | before use must always *terraform init* configured backend 273 | 274 | ## Control Statement 275 | - [if-else](https://www.terraform.io/docs/configuration-0-11/interpolation.html#conditionals) 276 | 277 | ``` 278 | # condition ? true_val : false_val 279 | 280 | resource "aws_instance" "web" { 281 | subnet = "${var.env == "production" ? var.prod_subnet : var.dev_subnet}" 282 | } 283 | 284 | ``` 285 | 286 | - [loop](https://www.terraform.io/docs/configuration/expressions.html#for-expressions) 287 | - for 288 | - creates a complex type value by transforming another complex type value 289 | 290 | ``` 291 | # Produce tuple 292 | [for s in var.list : upper(s)] 293 | 294 | # Produce object 295 | {for s in var.list : s => upper(s)} 296 | 297 | Diff is "[" or "{" 298 | 299 | # optional if clause to filter elements 300 | [for s in var.list : upper(s) if s != ""] 301 | 302 | # add ... symbol for object 303 | {for s in var.list : substr(s, 0, 1) => s... if s != ""} 304 | ``` 305 | 306 | - dynamic block 307 | - supported inside resource, data, provider, and provisioner blocks 308 | - acts much like a for expression 309 | - The for_each argument provides the complex value to iterate over 310 | - Best Practices for dynamic Blocks 311 | - Overuse of dynamic blocks can make configuration hard to read and maintain 312 | 313 | ``` 314 | resource "aws_security_group" "example" { 315 | name = "example" # can use expressions here 316 | 317 | dynamic "ingress" { 318 | for_each = var.service_ports 319 | content { 320 | from_port = ingress.value 321 | to_port = ingress.value 322 | protocol = "tcp" 323 | } 324 | } 325 | } 326 | ``` 327 | 328 | ## Modules 329 | 330 | > is a container for multiple resources that are used together 331 | 332 | To define a module, create a new directory for it and place one or more .tf files inside just as you would do for a root module. Modules can also call other modules using a *module* block (but recommend keeping flat) 333 | 334 | ### Module structure 335 | 336 | Most commonly, modules use 337 | 338 | - [Input Variables](https://www.terraform.io/docs/configuration/variables.html) 339 | - [Ouput Values](https://www.terraform.io/docs/configuration/outputs.html) 340 | - [Resources](https://www.terraform.io/docs/configuration/resources.html) 341 | 342 | ### When to write a module 343 | 344 | Any combination of resources and other constructs can be factored out into a module, but over-using modules can make your overall Terraform configuration harder to understand and maintain, so we recommend moderation. 345 | 346 | ``` 347 | For example, aws_instance and aws_elb are both resource types belonging to the AWS provider. 348 | You might use a module to represent the higher-level concept "HashiCorp Consul cluster running in AWS" 349 | which happens to be constructed from these and other AWS provider resources. 350 | ``` 351 | 352 | ### Standard Module Structure 353 | 354 | recommend for reusable modules distributed in separate repositories 355 | 356 | - Root module 357 | - a file and directory layout 358 | - or have nested under Root module 359 | - README.md 360 | 361 | ``` 362 | # Dedicated folder for module and README.md 363 | $ tree minimal-module/ 364 | . 365 | ├── README.md 366 | ├── main.tf 367 | ├── variables.tf 368 | ├── outputs.tf 369 | 370 | #Nested 371 | $ tree complete-module/ 372 | . 373 | ├── README.md 374 | ├── main.tf 375 | ├── variables.tf 376 | ├── outputs.tf 377 | ├── ... 378 | ├── modules/ 379 | │ ├── nestedA/ 380 | │ │ ├── README.md 381 | │ │ ├── variables.tf 382 | │ │ ├── main.tf 383 | │ │ ├── outputs.tf 384 | │ ├── nestedB/ 385 | │ ├── .../ 386 | ├── examples/ 387 | │ ├── exampleA/ 388 | │ │ ├── main.tf 389 | │ ├── exampleB/ 390 | │ ├── .../ 391 | ``` 392 | 393 | ## Software provision with Terraform 394 | 395 | Two ways to provision software 396 | - Custom AMI 397 | - Standard AMI with 398 | - File upload 399 | - Remote exec 400 | - Automation tools 401 | - chef (integrated with terraform) 402 | - puppet 403 | - ansible 404 | 405 | ## Built-in Functions 406 | 407 | > The Terraform language includes a number of built-in functions that you can call from within expressions to transform and combine values 408 | 409 | ``` 410 | > max(5, 12, 9) 411 | 12 412 | 413 | > format("Hello, %s!", "Ander") 414 | Hello, Ander! 415 | > format("There are %d lights", 4) 416 | There are 4 lights 417 | ``` 418 | 419 | All the function reference [here](https://www.terraform.io/docs/configuration/functions.html) 420 | 421 | ## Packer 422 | 423 | > Build Automated Machine Images 424 | 425 | [Packer](https://packer.io/) is an open source tool for creating identical machine images for multiple platforms from a single source configuration 426 | 427 | ### Packer Template 428 | 429 | 3 main parts 430 | - variables 431 | - contain the list of variables you need to use or need across other sections on the Packer Template JSON file 432 | - builders 433 | - define what image we are going to create and for which technology/platform we are going to create an image for like AWS, DOCKER, VirtualBox, OpenStack etc. 434 | - provisioners 435 | - the list of built-in or external configuration on management tools like Shell, Ansible, Chef, PowerShell 436 | - adding a necessary software/programs to it 437 | 438 | *reference [Template Structure](https://packer.io/docs/templates/index.html)* 439 | 440 | ``` 441 | { 442 | "variables": { 443 | "aws_access_key": "", 444 | "aws_secret_key": "" 445 | }, 446 | "builders": [{ 447 | "type": "amazon-ebs", 448 | "access_key": "{{user `aws_access_key`}}", 449 | "secret_key": "{{user `aws_secret_key`}}", 450 | "region": "us-east-1", 451 | "source_ami_filter": { 452 | "filters": { 453 | "virtualization-type": "hvm", 454 | "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*", 455 | "root-device-type": "ebs" 456 | }, 457 | "owners": ["099720109477"], 458 | "most_recent": true 459 | }, 460 | "instance_type": "t2.micro", 461 | "ssh_username": "ubuntu", 462 | "ami_name": "packer-example {{timestamp}}" 463 | }] 464 | } 465 | ``` 466 | 467 | ### Interesting Command 468 | - validate 469 | - validate the template 470 | - output should be *Template validated successfully.* 471 | - build 472 | - build your image 473 | - artifacts are the results of a build, and typically represent an ID (such as in the case of an AMI) 474 | -------------------------------------------------------------------------------- /aws/ec2-ebs/instance.tf: -------------------------------------------------------------------------------- 1 | resource "aws_instance" "ec2_ssh_enable" { 2 | ami = var.AMIS[var.AWS_REGION] 3 | instance_type = "t2.micro" 4 | 5 | # the VPC subnet 6 | subnet_id = aws_subnet.main-public-1.id 7 | 8 | # the security group 9 | vpc_security_group_ids = [aws_security_group.hta-sg-ssh.id] 10 | 11 | # the public SSH key 12 | key_name = aws_key_pair.mykeypair.key_name 13 | } 14 | 15 | resource "aws_ebs_volume" "ebs_volume" { 16 | availability_zone = "eu-west-1a" 17 | size = 20 18 | type = "gp2" 19 | tags = { 20 | Name = "20gb general purpose" 21 | } 22 | } 23 | 24 | resource "aws_volume_attachment" "ebs_volume_attachment" { 25 | device_name = "/dev/xvdh" 26 | volume_id = aws_ebs_volume.ebs_volume.id 27 | instance_id = aws_instance.ec2_ssh_enable.id 28 | } 29 | 30 | -------------------------------------------------------------------------------- /aws/ec2-ebs/keypairs.tf: -------------------------------------------------------------------------------- 1 | resource "aws_key_pair" "mykeypair" { 2 | key_name = "mykeypair" 3 | public_key = file(var.PATH_TO_PUBLIC_KEY) 4 | } -------------------------------------------------------------------------------- /aws/ec2-ebs/provider.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = var.AWS_REGION 3 | } 4 | 5 | -------------------------------------------------------------------------------- /aws/ec2-ebs/securitygroup.tf: -------------------------------------------------------------------------------- 1 | resource "aws_security_group" "hta-sg-ssh" { 2 | vpc_id = aws_vpc.main.id 3 | name = "hta-sg-ssh" 4 | description = "security group that allows ssh and all egress traffic" 5 | egress { 6 | from_port = 0 7 | to_port = 0 8 | protocol = "-1" 9 | cidr_blocks = ["0.0.0.0/0"] 10 | } 11 | 12 | ingress { 13 | from_port = 22 14 | to_port = 22 15 | protocol = "tcp" 16 | cidr_blocks = ["0.0.0.0/0"] 17 | } 18 | tags = { 19 | Name = "hta-sg-ssh" 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /aws/ec2-ebs/variables.tf: -------------------------------------------------------------------------------- 1 | variable "AWS_REGION" { 2 | default = "us-east-1" 3 | } 4 | 5 | variable "AWS_ACCESS_KEY" { 6 | } 7 | 8 | variable "AWS_SECRET_KEY" { 9 | } 10 | 11 | variable "PATH_TO_PRIVATE_KEY" { 12 | default = "mykey.ppk" 13 | } 14 | 15 | variable "PATH_TO_PUBLIC_KEY" { 16 | default = "mykey" 17 | } 18 | 19 | variable "AMIS" { 20 | type = map(string) 21 | default = { 22 | us-east-1 = "ami-13be557e" 23 | us-west-2 = "ami-06b94666" 24 | eu-west-1 = "ami-844e0bf7" 25 | } 26 | } -------------------------------------------------------------------------------- /aws/ec2-ebs/versions.tf: -------------------------------------------------------------------------------- 1 | 2 | terraform { 3 | required_version = ">= 0.12" 4 | } 5 | -------------------------------------------------------------------------------- /aws/ec2-ebs/vpc.tf: -------------------------------------------------------------------------------- 1 | # Internet VPC 2 | resource "aws_vpc" "main" { 3 | cidr_block = "10.0.0.0/16" 4 | instance_tenancy = "default" 5 | enable_dns_support = "true" 6 | enable_dns_hostnames = "true" 7 | enable_classiclink = "false" 8 | tags = { 9 | Name = "main" 10 | } 11 | } 12 | 13 | # Subnets 14 | resource "aws_subnet" "main-public-1" { 15 | vpc_id = aws_vpc.main.id 16 | cidr_block = "10.0.1.0/24" 17 | map_public_ip_on_launch = "true" 18 | availability_zone = "eu-west-1a" 19 | 20 | tags = { 21 | Name = "main-public-1" 22 | } 23 | } 24 | 25 | resource "aws_subnet" "main-private-1" { 26 | vpc_id = aws_vpc.main.id 27 | cidr_block = "10.0.2.0/24" 28 | map_public_ip_on_launch = "false" 29 | availability_zone = "eu-west-1a" 30 | 31 | tags = { 32 | Name = "main-private-1" 33 | } 34 | } 35 | 36 | 37 | # Internet GW 38 | resource "aws_internet_gateway" "main-gw" { 39 | vpc_id = aws_vpc.main.id 40 | 41 | tags = { 42 | Name = "main" 43 | } 44 | } 45 | 46 | # route tables 47 | resource "aws_route_table" "main-public" { 48 | vpc_id = aws_vpc.main.id 49 | route { 50 | cidr_block = "0.0.0.0/0" 51 | gateway_id = aws_internet_gateway.main-gw.id 52 | } 53 | 54 | tags = { 55 | Name = "main-public-1" 56 | } 57 | } 58 | 59 | # route associations public 60 | resource "aws_route_table_association" "main-public-1-a" { 61 | subnet_id = aws_subnet.main-public-1.id 62 | route_table_id = aws_route_table.main-public.id 63 | } 64 | 65 | -------------------------------------------------------------------------------- /aws/ec2/instance.tf: -------------------------------------------------------------------------------- 1 | resource "aws_instance" "ec2_ssh_enable" { 2 | ami = var.AMIS[var.AWS_REGION] 3 | instance_type = "t2.micro" 4 | 5 | # the VPC subnet 6 | subnet_id = aws_subnet.main-public-1.id 7 | 8 | # the security group 9 | vpc_security_group_ids = [aws_security_group.hta-sg-ssh.id] 10 | 11 | # the public SSH key 12 | key_name = aws_key_pair.mykeypair.key_name 13 | } 14 | 15 | -------------------------------------------------------------------------------- /aws/ec2/keypairs.tf: -------------------------------------------------------------------------------- 1 | resource "aws_key_pair" "mykeypair" { 2 | key_name = "mykeypair" 3 | public_key = file(var.PATH_TO_PUBLIC_KEY) 4 | } -------------------------------------------------------------------------------- /aws/ec2/provider.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = var.AWS_REGION 3 | } 4 | 5 | -------------------------------------------------------------------------------- /aws/ec2/securitygroup.tf: -------------------------------------------------------------------------------- 1 | resource "aws_security_group" "hta-sg-ssh" { 2 | vpc_id = aws_vpc.main.id 3 | name = "hta-sg-ssh" 4 | description = "security group that allows ssh and all egress traffic" 5 | egress { 6 | from_port = 0 7 | to_port = 0 8 | protocol = "-1" 9 | cidr_blocks = ["0.0.0.0/0"] 10 | } 11 | 12 | ingress { 13 | from_port = 22 14 | to_port = 22 15 | protocol = "tcp" 16 | cidr_blocks = ["0.0.0.0/0"] 17 | } 18 | tags = { 19 | Name = "hta-sg-ssh" 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /aws/ec2/variables.tf: -------------------------------------------------------------------------------- 1 | variable "AWS_REGION" { 2 | default = "us-east-1" 3 | } 4 | 5 | variable "AWS_ACCESS_KEY" { 6 | } 7 | 8 | variable "AWS_SECRET_KEY" { 9 | } 10 | 11 | variable "PATH_TO_PRIVATE_KEY" { 12 | default = "mykey.ppk" 13 | } 14 | 15 | variable "PATH_TO_PUBLIC_KEY" { 16 | default = "mykey" 17 | } 18 | 19 | variable "AMIS" { 20 | type = map(string) 21 | default = { 22 | us-east-1 = "ami-13be557e" 23 | us-west-2 = "ami-06b94666" 24 | eu-west-1 = "ami-844e0bf7" 25 | } 26 | } -------------------------------------------------------------------------------- /aws/ec2/versions.tf: -------------------------------------------------------------------------------- 1 | 2 | terraform { 3 | required_version = ">= 0.12" 4 | } 5 | -------------------------------------------------------------------------------- /aws/ec2/vpc.tf: -------------------------------------------------------------------------------- 1 | # Internet VPC 2 | resource "aws_vpc" "main" { 3 | cidr_block = "10.0.0.0/16" 4 | instance_tenancy = "default" 5 | enable_dns_support = "true" 6 | enable_dns_hostnames = "true" 7 | enable_classiclink = "false" 8 | tags = { 9 | Name = "main" 10 | } 11 | } 12 | 13 | # Subnets 14 | resource "aws_subnet" "main-public-1" { 15 | vpc_id = aws_vpc.main.id 16 | cidr_block = "10.0.1.0/24" 17 | map_public_ip_on_launch = "true" 18 | availability_zone = "eu-west-1a" 19 | 20 | tags = { 21 | Name = "main-public-1" 22 | } 23 | } 24 | 25 | resource "aws_subnet" "main-private-1" { 26 | vpc_id = aws_vpc.main.id 27 | cidr_block = "10.0.2.0/24" 28 | map_public_ip_on_launch = "false" 29 | availability_zone = "eu-west-1a" 30 | 31 | tags = { 32 | Name = "main-private-1" 33 | } 34 | } 35 | 36 | 37 | # Internet GW 38 | resource "aws_internet_gateway" "main-gw" { 39 | vpc_id = aws_vpc.main.id 40 | 41 | tags = { 42 | Name = "main" 43 | } 44 | } 45 | 46 | # route tables 47 | resource "aws_route_table" "main-public" { 48 | vpc_id = aws_vpc.main.id 49 | route { 50 | cidr_block = "0.0.0.0/0" 51 | gateway_id = aws_internet_gateway.main-gw.id 52 | } 53 | 54 | tags = { 55 | Name = "main-public-1" 56 | } 57 | } 58 | 59 | # route associations public 60 | resource "aws_route_table_association" "main-public-1-a" { 61 | subnet_id = aws_subnet.main-public-1.id 62 | route_table_id = aws_route_table.main-public.id 63 | } 64 | 65 | -------------------------------------------------------------------------------- /aws/ecr/ecr.tf: -------------------------------------------------------------------------------- 1 | resource "aws_ecr_repository" "hta_simpleapp" { 2 | name = "hta_simpleapp" 3 | } 4 | 5 | -------------------------------------------------------------------------------- /aws/ecr/output.tf: -------------------------------------------------------------------------------- 1 | output "hta-repository" { 2 | value = aws_ecr_repository.hta_simpleapp.repository_url 3 | } 4 | 5 | -------------------------------------------------------------------------------- /aws/ecr/provider.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = var.AWS_REGION 3 | } 4 | 5 | -------------------------------------------------------------------------------- /aws/ecr/vars.tf: -------------------------------------------------------------------------------- 1 | variable "AWS_REGION" { 2 | default = "eu-west-1" 3 | } 4 | 5 | -------------------------------------------------------------------------------- /aws/ecr/versions.tf: -------------------------------------------------------------------------------- 1 | 2 | terraform { 3 | required_version = ">= 0.12" 4 | } 5 | -------------------------------------------------------------------------------- /aws/simple-vpc/private-vpc.tf: -------------------------------------------------------------------------------- 1 | resource "aws_subnet" "main-private-1" { 2 | vpc_id = aws_vpc.main.id 3 | cidr_block = "10.0.4.0/24" 4 | map_public_ip_on_launch = "false" 5 | availability_zone = "us-east-1a" 6 | 7 | tags = { 8 | Name = "main-private-1" 9 | } 10 | } 11 | 12 | resource "aws_subnet" "main-private-2" { 13 | vpc_id = aws_vpc.main.id 14 | cidr_block = "10.0.5.0/24" 15 | map_public_ip_on_launch = "false" 16 | availability_zone = "us-east-1b" 17 | 18 | tags = { 19 | Name = "main-private-2" 20 | } 21 | } 22 | 23 | resource "aws_subnet" "main-private-3" { 24 | vpc_id = aws_vpc.main.id 25 | cidr_block = "10.0.6.0/24" 26 | map_public_ip_on_launch = "false" 27 | availability_zone = "us-east-1c" 28 | 29 | tags = { 30 | Name = "main-private-3" 31 | } 32 | } 33 | 34 | # nat gw 35 | resource "aws_eip" "nat" { 36 | vpc = true 37 | } 38 | 39 | resource "aws_nat_gateway" "nat-gw" { 40 | allocation_id = aws_eip.nat.id 41 | subnet_id = aws_subnet.main-public-1.id 42 | depends_on = [aws_internet_gateway.main-gw] 43 | } 44 | 45 | # VPC setup for NAT 46 | resource "aws_route_table" "main-private" { 47 | vpc_id = aws_vpc.main.id 48 | route { 49 | cidr_block = "0.0.0.0/0" 50 | nat_gateway_id = aws_nat_gateway.nat-gw.id 51 | } 52 | 53 | tags = { 54 | Name = "main-private-1" 55 | } 56 | } 57 | 58 | # route associations private 59 | resource "aws_route_table_association" "main-private-1-a" { 60 | subnet_id = aws_subnet.main-private-1.id 61 | route_table_id = aws_route_table.main-private.id 62 | } 63 | 64 | resource "aws_route_table_association" "main-private-2-a" { 65 | subnet_id = aws_subnet.main-private-2.id 66 | route_table_id = aws_route_table.main-private.id 67 | } 68 | 69 | resource "aws_route_table_association" "main-private-3-a" { 70 | subnet_id = aws_subnet.main-private-3.id 71 | route_table_id = aws_route_table.main-private.id 72 | } 73 | 74 | -------------------------------------------------------------------------------- /aws/simple-vpc/provider.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = var.AWS_REGION 3 | } 4 | 5 | -------------------------------------------------------------------------------- /aws/simple-vpc/public-vpc.tf: -------------------------------------------------------------------------------- 1 | 2 | # Subnets 3 | resource "aws_subnet" "main-public-1" { 4 | vpc_id = aws_vpc.main.id 5 | cidr_block = "10.0.1.0/24" 6 | map_public_ip_on_launch = "true" 7 | availability_zone = "us-east-1a" 8 | 9 | tags = { 10 | Name = "main-public-1" 11 | } 12 | } 13 | 14 | resource "aws_subnet" "main-public-2" { 15 | vpc_id = aws_vpc.main.id 16 | cidr_block = "10.0.2.0/24" 17 | map_public_ip_on_launch = "true" 18 | availability_zone = "us-east-1b" 19 | 20 | tags = { 21 | Name = "main-public-2" 22 | } 23 | } 24 | 25 | resource "aws_subnet" "main-public-3" { 26 | vpc_id = aws_vpc.main.id 27 | cidr_block = "10.0.3.0/24" 28 | map_public_ip_on_launch = "true" 29 | availability_zone = "us-east-1c" 30 | 31 | tags = { 32 | Name = "main-public-3" 33 | } 34 | } 35 | 36 | 37 | # route tables 38 | resource "aws_route_table" "main-public" { 39 | vpc_id = aws_vpc.main.id 40 | route { 41 | cidr_block = "0.0.0.0/0" 42 | gateway_id = aws_internet_gateway.main-gw.id 43 | } 44 | 45 | tags = { 46 | Name = "main-public-1" 47 | } 48 | } 49 | 50 | # route associations public 51 | resource "aws_route_table_association" "main-public-1-a" { 52 | subnet_id = aws_subnet.main-public-1.id 53 | route_table_id = aws_route_table.main-public.id 54 | } 55 | 56 | resource "aws_route_table_association" "main-public-2-a" { 57 | subnet_id = aws_subnet.main-public-2.id 58 | route_table_id = aws_route_table.main-public.id 59 | } 60 | 61 | resource "aws_route_table_association" "main-public-3-a" { 62 | subnet_id = aws_subnet.main-public-3.id 63 | route_table_id = aws_route_table.main-public.id 64 | } 65 | 66 | -------------------------------------------------------------------------------- /aws/simple-vpc/variables.tf: -------------------------------------------------------------------------------- 1 | variable "AWS_REGION" { 2 | default = "us-east-1" 3 | } 4 | 5 | variable "AWS_ACCESS_KEY" { 6 | } 7 | 8 | variable "AWS_SECRET_KEY" { 9 | } 10 | -------------------------------------------------------------------------------- /aws/simple-vpc/versions.tf: -------------------------------------------------------------------------------- 1 | 2 | terraform { 3 | required_version = ">= 0.12" 4 | } 5 | -------------------------------------------------------------------------------- /aws/simple-vpc/vpc-main.tf: -------------------------------------------------------------------------------- 1 | # Internet VPC 2 | resource "aws_vpc" "main" { 3 | cidr_block = "10.0.0.0/16" 4 | instance_tenancy = "default" 5 | enable_dns_support = "true" 6 | enable_dns_hostnames = "true" 7 | enable_classiclink = "false" 8 | tags = { 9 | Name = "main" 10 | } 11 | } 12 | 13 | # Internet GW 14 | resource "aws_internet_gateway" "main-gw" { 15 | vpc_id = aws_vpc.main.id 16 | 17 | tags = { 18 | Name = "main" 19 | } 20 | } -------------------------------------------------------------------------------- /datasources/provider.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = var.AWS_REGION 3 | } 4 | 5 | variable "AMIS" { 6 | type = map(string) 7 | default = { 8 | us-east-1 = "ami-13be557e" 9 | us-west-2 = "ami-06b94666" 10 | eu-west-1 = "ami-844e0bf7" 11 | } 12 | } -------------------------------------------------------------------------------- /datasources/securitygroup.tf: -------------------------------------------------------------------------------- 1 | # filter with regions and services 2 | data "aws_ip_ranges" "european_ec2" { 3 | regions = ["eu-west-1", "eu-central-1"] 4 | services = ["ec2"] 5 | } 6 | 7 | # use in cidr_blocks based on attribute references https://www.terraform.io/docs/providers/aws/d/ip_ranges.html 8 | resource "aws_security_group" "from_europe" { 9 | name = "from_europe" 10 | 11 | ingress { 12 | from_port = "443" 13 | to_port = "443" 14 | protocol = "tcp" 15 | cidr_blocks = data.aws_ip_ranges.european_ec2.cidr_blocks 16 | ipv6_cidr_blocks = data.aws_ip_ranges.european_ec2.ipv6_cidr_blocks 17 | } 18 | 19 | tags = { 20 | CreateDate = data.aws_ip_ranges.european_ec2.create_date 21 | SyncToken = data.aws_ip_ranges.european_ec2.sync_token 22 | } 23 | } -------------------------------------------------------------------------------- /datasources/variables.tf: -------------------------------------------------------------------------------- 1 | variable "AWS_REGION" { 2 | default = "eu-west-1" 3 | } -------------------------------------------------------------------------------- /datasources/versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">= 0.12" 3 | } 4 | -------------------------------------------------------------------------------- /modules/environments/dev/main.tf: -------------------------------------------------------------------------------- 1 | module "dev-elasticache" { 2 | source = "../../simple_modules" 3 | environment = "dev" 4 | node_count = 1 5 | node_type = "cache.m3.medium" 6 | availability_zones = ["us-east-1a", "us-east-1b"] 7 | } -------------------------------------------------------------------------------- /modules/environments/prod/main.tf: -------------------------------------------------------------------------------- 1 | module "production-elasticache" { 2 | source = "../../simple_modules" 3 | environment = "dev" 4 | node_count = 3 5 | node_type = "cache.m3.large" 6 | availability_zones = ["us-east-1a", "us-east-1b"] 7 | } -------------------------------------------------------------------------------- /modules/main.tf: -------------------------------------------------------------------------------- 1 | module "dev" { 2 | source = "./environments/dev" 3 | } 4 | 5 | module "production" { 6 | source = "./environments/prod" 7 | } -------------------------------------------------------------------------------- /modules/provider.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = var.AWS_REGION 3 | } 4 | 5 | variable "AMIS" { 6 | type = map(string) 7 | default = { 8 | us-east-1 = "ami-13be557e" 9 | us-west-2 = "ami-06b94666" 10 | eu-west-1 = "ami-844e0bf7" 11 | } 12 | } -------------------------------------------------------------------------------- /modules/simple_modules/elasticache-cluster.tf: -------------------------------------------------------------------------------- 1 | resource "aws_elasticache_replication_group" "elasticache-cluster" { 2 | availability_zones = var.availability_zones 3 | replication_group_id = "tf-${var.environment}-rep-group" 4 | replication_group_description = "${var.environment} replication group" 5 | node_type = var.node_type 6 | number_cache_clusters = var.node_count 7 | parameter_group_name = "default.redis3.2" 8 | port = 6379 9 | } 10 | -------------------------------------------------------------------------------- /modules/simple_modules/variables.tf: -------------------------------------------------------------------------------- 1 | variable "environment" {} 2 | variable "node_count" {} 3 | variable "node_type" {} 4 | variable "availability_zones" { type = list(string) } -------------------------------------------------------------------------------- /modules/simple_modules/versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">= 0.12" 3 | } 4 | -------------------------------------------------------------------------------- /modules/variables.tf: -------------------------------------------------------------------------------- 1 | variable "AWS_REGION" { 2 | default = "eu-west-1" 3 | } -------------------------------------------------------------------------------- /packer/README.md: -------------------------------------------------------------------------------- 1 | # Packer Example 2 | 3 | > Build Automated Machine Images 4 | 5 | ## Steps 6 | 7 | - packer validate packer-example.json 8 | - create terraform.tfvars that contains your AWS credentail 9 | - AWS_ACCESS_KEY 10 | - AWS_SECRET_KEY 11 | - AWS_REGION 12 | - packer build packer-example.json 13 | 14 | ## Note 15 | 16 | define aws credentail outside and references by [user variables](/https://packer.io/docs/templates/user-variables.html) -------------------------------------------------------------------------------- /packer/packer-example.json: -------------------------------------------------------------------------------- 1 | { 2 | "variables": { 3 | "aws_access_key": "", 4 | "aws_secret_key": "" 5 | }, 6 | "builders": [{ 7 | "type": "amazon-ebs", 8 | "access_key": "{{user `aws_access_key`}}", 9 | "secret_key": "{{user `aws_secret_key`}}", 10 | "region": "us-east-1", 11 | "source_ami_filter": { 12 | "filters": { 13 | "virtualization-type": "hvm", 14 | "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*", 15 | "root-device-type": "ebs" 16 | }, 17 | "owners": ["099720109477"], 18 | "most_recent": true 19 | }, 20 | "instance_type": "t2.micro", 21 | "ssh_username": "ubuntu", 22 | "ami_name": "packer-hta-example {{timestamp}}" 23 | }] 24 | } -------------------------------------------------------------------------------- /packer/provider.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = var.AWS_REGION 3 | } 4 | 5 | -------------------------------------------------------------------------------- /packer/variables.tf: -------------------------------------------------------------------------------- 1 | variable "AWS_REGION" { 2 | default = "eu-west-1" 3 | } 4 | 5 | variable "AWS_ACCESS_KEY" { 6 | } 7 | 8 | variable "AWS_SECRET_KEY" { 9 | } 10 | -------------------------------------------------------------------------------- /packer/versions.tf: -------------------------------------------------------------------------------- 1 | 2 | terraform { 3 | required_version = ">= 0.12" 4 | } 5 | -------------------------------------------------------------------------------- /simple/terraform.tfvars: -------------------------------------------------------------------------------- 1 | AWS_ACCESS_KEY="key123" 2 | AWS_SECRET_KEY="secret123" 3 | AWS_REGION="us-east-1" -------------------------------------------------------------------------------- /simple/variable.tf: -------------------------------------------------------------------------------- 1 | variable "myvar" { 2 | type = string 3 | default = "hello world" 4 | description = "first variable on terraform" 5 | } 6 | 7 | variable "mylist" { 8 | type = list(string) 9 | default = [1,2,3] 10 | } 11 | 12 | variable "mymap" { 13 | type = map(string) 14 | default = { 15 | mykey = "my value" 16 | } 17 | } 18 | 19 | variable "AWS_ACCESS_KEY" { 20 | 21 | } 22 | 23 | variable "AWS_SECRET_KEY" { 24 | 25 | } 26 | 27 | variable "AWS_REGION" { 28 | default = "eu-west-1" 29 | } 30 | -------------------------------------------------------------------------------- /spinning/instance.tf: -------------------------------------------------------------------------------- 1 | resource "aws_instance" "example" { 2 | ami = var.AMIS[var.AWS_REGION] 3 | instance_type = "t2.micro" 4 | } -------------------------------------------------------------------------------- /spinning/provider.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | access_key = var.AWS_ACCESS_KEY 3 | secret_key = var.AWS_SECRET_KEY 4 | region = var.AWS_REGION 5 | } -------------------------------------------------------------------------------- /spinning/variable.tf: -------------------------------------------------------------------------------- 1 | variable "AWS_ACCESS_KEY" { 2 | } 3 | 4 | variable "AWS_SECRET_KEY" { 5 | } 6 | 7 | variable "AWS_REGION" { 8 | default = "eu-west-1" 9 | } 10 | 11 | variable "AMIS" { 12 | type = map(string) 13 | default = { 14 | us-east-1 = "ami-13be557e" 15 | us-west-2 = "ami-06b94666" 16 | eu-west-1 = "ami-0d729a60" 17 | } 18 | } --------------------------------------------------------------------------------