-
Notifications
You must be signed in to change notification settings - Fork 7
Terraform #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Terraform #109
Changes from all commits
965da6c
490a6c8
04866de
df8a4d3
f719b1e
0f4f03d
4c7444c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| Big Empty | ||
| No Read ME currently written. |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,93 @@ | ||||||||
| # About | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably want to make the title of the document more descriptive with the following section being an implicit about section for the document. |
||||||||
| This a helpfile listing the components used in the creation of a teraform based infrastructure. This uses aws-provider based examples, but are general to any terraform provider. This is organized into 2 groups, components & files. | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is different from the name of the document, which includes help-commands where it is more of an broader introduction? Also Terraform is spelled incorrectly and should be capitalized. |
||||||||
|
|
||||||||
| *Of course, this is not every single aspect of terraform, but should be a decent start to creating a full terraform-based infrastructure* | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
|
||||||||
|
|
||||||||
| ## Components | ||||||||
| ### Providers: | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some sections have the |
||||||||
| Providers are like toolboxes for your infrastructure. They define which cloud service or infrastructure you're working with (e.g., AWS, Azure, Google Cloud). If you're building on AWS, for example, you need the AWS provider. | ||||||||
| ```hcl | ||||||||
| provider "aws" { | ||||||||
| region = "us-west-2" | ||||||||
| } | ||||||||
| ``` | ||||||||
| ```hcl | ||||||||
| provider "<provider-name>" { | ||||||||
|
|
||||||||
| } | ||||||||
| ``` | ||||||||
|
Comment on lines
+11
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is obvious which is which, but I think it would be better to clearly annotate which code block represents a real example vs which is a more general one. Also I would comment in the body of the provider that additional attributes can be added? |
||||||||
|
|
||||||||
| ### Resources: | ||||||||
| Resources are the actual components you are building, like EC2 instances or S3 buckets. | ||||||||
| A resource is like telling the worker to build a single unit in your skyscraper — an EC2 instance in AWS. | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure the example here is helping much, I am not thinking of a good replacement. Maybe something like: "Resources are like drawings on a blueprint, they describe individual components of the overall infrastructure - it is up to you to connect and coordinate those resources to make a coherent blueprint" |
||||||||
| ```hcl | ||||||||
| resource "aws-instance" "example" { | ||||||||
| ami = "ami-####" # Specific to AWS EC2 instance | ||||||||
| instance_type = "t2.micro" # Specific to AWS EC2 instance | ||||||||
| } | ||||||||
| ``` | ||||||||
| ```hcl | ||||||||
| resource "<resource-type>" "<resource-name>" { | ||||||||
| <resouce specific key required by aws/provider> | ||||||||
| } | ||||||||
| ``` | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same feedback as previous code blocks |
||||||||
| #### keyword vs key | ||||||||
| keys are configuration attributes/fields (usually specific to the provider) | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The example provided is a little unclear. Keywords are understandable but if keys are "attributes/fields" then are they parts of a resource like ... { -- Like an attribute or field. Or the provided example makes it seem like keys are the values we assign attributes of a resource? .. { |
||||||||
| keywords are reserved words specific to the programming language (ITC. Terraform) | ||||||||
| - resource = keyword | ||||||||
| - ami = key | ||||||||
| - instance_type = key | ||||||||
|
|
||||||||
| ### Modules: | ||||||||
| Reusable chunks of code. | ||||||||
| Sub-blueprint. | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would tie this in so it is more structured. something like "Modules are reusable chunks of code, they are smaller reusable blueprints... etc" |
||||||||
| Instead of writing the same set of instructions over and over again, you create a module that you can reuse across your projects. | ||||||||
| If you need multiple identical floors in your skyscraper, a module is a reusable floor plan. | ||||||||
|
|
||||||||
| ```hcl | ||||||||
| module "web_server" { | ||||||||
| source = "./modules/web_server" | ||||||||
| instance_count = 3 # a custom variable defined within the web_server module | ||||||||
| } | ||||||||
| ``` | ||||||||
| ```hcl | ||||||||
| module "web_server" { | ||||||||
| source = "<path to find code for a specifc module>" | ||||||||
| <you can specifc parameters that relate to the code here> | ||||||||
| } | ||||||||
| ``` | ||||||||
|
|
||||||||
| ### State | ||||||||
| Terraform keeps track of the infrastructure using a statefile. | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would expand on this, what does the statefile do, why should we not modify it. Are there any security considerations with it? |
||||||||
|
|
||||||||
| ### Output | ||||||||
| Outputs are like a report card after the construction is done. They tell you useful things like where the main door is (the public IP of the instance), so you know where to go or connect to after your infrastructure is built. | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would we want to use outputs, are there times where we would not want to use them or could they be dangerous? |
||||||||
| ```yaml | ||||||||
| output "instance_ip" { | ||||||||
| value = aws_instance.example.public_ip | ||||||||
| } | ||||||||
|
|
||||||||
| ``` | ||||||||
|
|
||||||||
|
|
||||||||
| ### Data | ||||||||
| data keyword like a scout who goes out to gather information about things that already exist. **You’re not creating anything new**; you’re just finding what’s already there (like discovering where roads or pipelines already exist before you build). | ||||||||
| ```yaml | ||||||||
| data "aws_vpc" "default" { | ||||||||
| default = true | ||||||||
| } | ||||||||
| ``` | ||||||||
|
|
||||||||
|
|
||||||||
| # Files & Directories | ||||||||
| Note: All the entire terraform component can be written in 1 main.tf file. Convention is to split up these components into logical component files. | ||||||||
| ## main.tf | ||||||||
| - used to declare resources | ||||||||
|
|
||||||||
| ## variables.tf | ||||||||
| - a file to hold resuables variable names that you may use through out the terraform creation | ||||||||
|
|
||||||||
|
|
||||||||
| ## modules | ||||||||
| A higher level Directory used to section off, organize and/or seperate often general components, for example, subnets, & a vpc files/details. | ||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,156 @@ | ||||||
| # Table of Contents <!-- omit from toc --> | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably move |
||||||
| - [About](#about) | ||||||
| - [Workflow](#workflow) | ||||||
| - [Install Terraform on linux](#install-terraform-on-linux) | ||||||
| - [Configure the Terraform provider](#configure-the-terraform-provider) | ||||||
| - [Write configuration files](#write-configuration-files) | ||||||
| - [Initialize Terraform](#initialize-terraform) | ||||||
| - [Run terraform plan](#run-terraform-plan) | ||||||
| - [Create resources with terraform apply](#create-resources-with-terraform-apply) | ||||||
| - [Delete resources using terraform destroy](#delete-resources-using-terraform-destroy) | ||||||
| - [Notes:](#notes) | ||||||
|
|
||||||
| # About | ||||||
| This file describes the general flow I use for starting and creating a aws-based teraform infrastructure | ||||||
|
|
||||||
| Author: Chisom Ukaegbu | ||||||
|
|
||||||
| # Workflow | ||||||
| ## Install Terraform on linux | ||||||
| 1. Install Terraform (ubuntu debian ver.) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe link to the docs too? |
||||||
|
|
||||||
| - ` wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings hashicorp-archive-keyring.gpg` | ||||||
|
|
||||||
| - `echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list` | ||||||
|
|
||||||
| - `sudo apt update && sudo apt install terraform ` | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
| ## Configure the Terraform provider | ||||||
| For our example we will focus on using aws as the cloud provider aka the place where out machines will be created & hosted | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Capitalize AWS |
||||||
|
|
||||||
| AWS will be our cloud provider and set up an account on aws cloud provider | ||||||
|
|
||||||
| 0. You will need AWS Access KEY ID & AWS Secret Key ID | ||||||
| This is gathered from the aws web console. Create a user or use a preexisting user. | ||||||
|
|
||||||
|  | ||||||
|
|
||||||
|
|
||||||
| Grab there access and secret key ids. | ||||||
|
|
||||||
|  | ||||||
|
|
||||||
| 1. Run `Aws Configure` in your terminal. | ||||||
| Input your keys | ||||||
| ```sh | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably want to tab-in this code block |
||||||
| blueteam@cyber-range tf-tuts % aws configure | ||||||
| AWS Access Key ID [****************PYVK]: ****PYVK | ||||||
| AWS Secret Access Key [****************duMt]: ****duMt | ||||||
| Default region name [eu-central-1]: <same region where you want to place the machines> | ||||||
| Default output format [None]: | ||||||
| blueteam@cyber-range tf-tuts % | ||||||
| ``` | ||||||
|
|
||||||
| ## Write configuration files | ||||||
| 1. Setup Cloud Provider | ||||||
|
|
||||||
| Convention says to place this config in a file name provider.tf. It does not matter aslong as the file has the .tf extension and is unique in name. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ```yaml | ||||||
| terraform { | ||||||
| required_providers { | ||||||
| aws = { | ||||||
| source = hashicorp/aws | ||||||
| version = " ~> 4.19.0" | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| ``` | ||||||
| 2. Create instances | ||||||
|
|
||||||
| Create a main.tf file. Convention is to name the file "main.tf" | ||||||
|
|
||||||
| This is where the block of the cofiguration for the virtual machines will be deployed | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ```yaml | ||||||
| # creating the code to create an EC2 instance in AWS using Terraform. | ||||||
| resource "aws_instance" "my_vm" { | ||||||
|
|
||||||
| ami = "ami-065deacbcaac64cf2" //Ubuntu AMI | ||||||
| instance_type = "t2.micro" | ||||||
|
|
||||||
| tags = { | ||||||
| Name = "My EC2 instance", | ||||||
| } | ||||||
|
|
||||||
| ########### | ||||||
| # declared a resource block of type “aws_instance”. | ||||||
| ### This instructs Terraform that we want to create an EC2 instance resource in AWS with the given attributes | ||||||
|
|
||||||
| # second parameter is “`my_vm`”, an internal identifier that refers to this ##particular EC2 instance elsewhere in the code. We can assign any name to this identifier | ||||||
|
|
||||||
|
|
||||||
| # assigned a `tag` “Name” with the value “My EC2 Instance”. | ||||||
| ``` | ||||||
|
|
||||||
| ## Initialize Terraform | ||||||
| 1. Intialize terraform | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| Run this command in your terminal of the same directory your provider is. | ||||||
| ```sh | ||||||
| terraform init | ||||||
| ``` | ||||||
| You should see these hidden files. when running | ||||||
| ```sh | ||||||
| ls -l | ||||||
| . .. .terraform .terraform.lock.hcl provider.tf | ||||||
| ``` | ||||||
|
|
||||||
| 2. Format the code | ||||||
| This command will auto fixed syntax and indentation of your configuration code | ||||||
| ```sh | ||||||
| terraform fmt | ||||||
| ``` | ||||||
|
|
||||||
|
|
||||||
| ## Run terraform plan | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section is tabbed in too much making it into a code block |
||||||
| This command will output 2 scenarios | ||||||
| output: identify and highlight resources that will be created, updated, or deleted if we choose to execute the current version of the code | ||||||
|
|
||||||
| or | ||||||
|
|
||||||
| Show issues regarding your terraform file | ||||||
|
|
||||||
| ```sh | ||||||
| terraform plan` | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is an extra `? |
||||||
| ``` | ||||||
|  | ||||||
|  | ||||||
| ## Create resources with terraform apply | ||||||
| Running the command `terraform apply` will begin to create | ||||||
| ```sh | ||||||
| terraform apply | ||||||
| ``` | ||||||
| Now if you navigate to aws, you will see the instances created. | ||||||
| Make sure you are in the same region as the provider you selected. | ||||||
| ## Delete resources using terraform destroy | ||||||
| ```sh | ||||||
| terraform destory | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think destroy is spelled wrong |
||||||
| ``` | ||||||
| Will delete any resources provisioned by your terraform script. | ||||||
| Virtual machines, vpcs, subnets etc are considered resources | ||||||
| # Notes: | ||||||
| There is more you can do with terraform but this is a quick start guide for creating an instance or network for the first time. | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably want to separate out any cleanup things like this into a separate pull request just to make sure things are more streamlined and clean. Its fine for this one, but something to keep in mind for the future.
I do this type of thing too. I am trying to get better...