Terraform Basics (AWS)

Install AWS CLI: https://aws.amazon.com/cli/

Install Terraform: https://www.terraform.io/downloads

Create a User on AWS: Open AWS Console → Go to IAM →Create User →Download user credentials csv

Configure AWS CLI: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html

  1. terraform init: Is used to initialize a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. It is safe to run this command multiple times.
  2. terraform validate: Runs checks that verify whether a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state. It is thus primarily useful for general verification of reusable modules, including correctness of attribute names and value types.
  3. terraform plan: creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure. It reads the current state of any already-existing remote objects to make sure that the Terraform state is up-to-date. It compares the current configuration to the prior state and noting any differences. It proposes a set of change actions that should, if applied, make the remote objects match the configuration.
  4. terraform apply: executes the actions proposed in a Terraform plan.
  5. terraform destroy: destroys all remote objects managed by a particular Terraform configuration.

Create a directory <Terraform> and create a file example1.tf.

File Contents: This file creates a EC2 instance in the us-east-1 region. The EC2 instance will be the same as the one listed below. You can see it uses the ami of that instance and the instance type “t2.micro”.

Run: Terraform init

This will initialize your directory and download all of the provider files needed to run your code.

Run: Terraform validate

This will validate your file and check for syntax errors.

Run: Terraform plan

This will create the execution plan and check for any inconsistencies in the AMI definition.

Run: Terraform apply

This will create the resource you specified in your terraform file.

All terraform files are stored as text with a .tf extention. The directory the file is stored in is called the working directory. This is the directory where the terraform commands are executed.

The basic objects inside a terraform document are:

  • Blocks: Container for other content. Each block has a type (including resource, input variable, output values, data sources, etc..)
  • Arguments: Assigns a value to a particular name. Always preceded by “=” symbol.
  • Identifiers: Argument names, block type names, and the names of most Terraform-specific constructs like resources, input variables, etc. are all identifiers. Identifiers can contain letters, digits, underscores (_), and hyphens (-). The first character of an identifier must not be a digit, to avoid ambiguity with literal numbers.
  • Comments: The Terraform language supports three different syntaxes for comments:# begins a single-line comment, ending at the end of the line.// also begins a single-line comment, as an alternative to #./* and */ are start and end delimiters for a comment that might span over multiple lines.The # single-line comment style is the default comment style and should be used in most cases. Automatic configuration formatting tools may automatically transform // comments into # comments, since the double-slash style is not idiomatic.
  • Terraform Block: Specifies Terraform version, required cloud providers and backend settings.
  • Resources Block: Resources are the most important element in the Terraform language. This block describes one or more infrastructure objects, such as virtual networks, compute instances, or higher-level components such as DNS records.
  • Input Variable Block: Input variables let you customize aspects of Terraform modules without altering the module’s own source code. This allows you to share modules across different Terraform configurations, making your module composable and reusable. You can set the values using the default argument.
  • Output Values Block: Output values make information about your infrastructure available on the command line, and can expose information for other Terraform configurations to use. Output values are similar to return values in programming languages.
  • Local Values Block: A local value assigns a name to an expression, so you can use it multiple times within a module without repeating it.Local values are like a function’s temporary local variables in traditional programming languages.
  • Data Sources Block: allow Terraform to use information defined outside of Terraform, defined by another separate Terraform configuration, or modified by functions.
  • Modules Block: Modules are containers for multiple resources that are used together. A module consists of a collection of .tfand/or .tf.json files kept together in a directory. Modules are the main way to package and reuse resource configurations with Terraform.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store