π This is not properly displayed? Read all of our issues online! π‘
Hi Reader ππ½
In this issue, we will explore the basics of Terraform, an open-source Infrastructure-as-Code (IaC) tool, and how it can simplify and automate the provisioning and management of your cloud infrastructure.
Join us as we uncover the key concepts and best practices to help you get started with Terraform and unlock its full potential.
π¨ P.S.: If you make it through this issue, we'll farewell you with some fascinating facts about Terraform that could definitely blow your mind even more and two sweet discounts π¬ you'll hopefully enjoy! π
First of all, some news from us. We decided to tackle the next project which will be π₯ the CloudWatch Book. If you're interested in the project check out the link.
Without further ado, let's get going! π
β
Before jumping in, have a look at the most important things about Terraform in our infographic below.
β
Before the cloud, changing the hardware your application runs on was difficult. But now, you can easily scale your application up or out, without any upfront costs.
You can provision infrastructure globally and delete it quickly when you no longer need it.
This has created a need for tools that can manage infrastructure resources programmatically.
With Infrastructure-as-Code tools, you can define all your infrastructure resources as code, alongside your application code.
Terraform stands out as one of the leading and highly adaptable IaC tools available today.
P.S.: If you want to delve deeper into the foundational aspects of IaC on AWS, we recommend checking out our previous blog posts about the history of IaC.
β
There are two types of IaC tools: declarative and imperative.
Declarative tools focus on defining the desired state of the infrastructure, while imperative tools explicitly list the commands needed to achieve that state. Each approach has its own benefits and considerations to take into account when choosing an IaC tool.
β
Terraform stands out as one of the most flexible options available. To fully grasp its capabilities and utilize it effectively, it's important to familiarize ourselves with its key concepts.
Terraform offers support for a wide range of cloud providers and services through its provider plugins. In addition to AWS, it also supports GCP, Azure, and other services like NewRelic.
This flexibility allows you to build a multi-cloud or multi-platform environment without the need for multiple tools.
One of the key features of Terraform is its ability to keep track of the state of your infrastructure. Whenever you make changes to your infrastructure by creating, updating, or deleting resources, Terraform saves the current state.
This means that Terraform remembers which resources are currently created and their configurations. This enables Terraform to determine the necessary changes required to reach the desired state, ensuring consistency and accuracy.
The infrastructure state can be stored remotely, such as in an S3 bucket. This allows you to run Terraform from different locations while maintaining the same results.
Terraform modules provide a way to create blueprints for your infrastructure and encapsulate configurations.
This allows you to create complex infrastructure setups hidden behind an abstraction layer. Modules also enable you to reuse these setups in different environments.
Terraform offers two important commands: plan and apply.
The plan command previews the changes that Terraform would execute. It helps prevent unintended changes and allows for safer and more controlled updates. The previewed plan can be saved locally and executed as-is.
The apply command executes all the changes indicated by Terraform to achieve the desired state of the infrastructure.
β
Terraform comes with a lot of features. Let's explore some of the most important ones!
Terraform modules provide a way to encapsulate and reuse infrastructure configurations, simplifying the management of complex environments and promoting the adoption of best practices.
In the example graphic below, we have two separate modules that define blueprints for different components:
By modularizing these configurations, we can easily reuse them, promote code sharing, and ensure adherence to best practices.
Each cloud platform has its own unique way of identifying resources, which is important for coupling components together.
For instance, AWS uses Amazon Resource Names (ARNs) to identify resources. If you want to grant access to a DynamoDB table while following the principle of least privilege (granting only necessary permissions for a user or service to perform its tasks), you need to include the corresponding ARN in the IAM policy.
To address this, Terraform provides outputs. You can reference the outputs of resources that are only available after the resource is created before Terraform executes.
Terraform determines resource dependencies by creating a Dependency Graph. This graph helps Terraform understand which resources rely on others and in what order they need to be created.
Letβs take a look at an example:
In this case, our dependency graph starts with the Lambda function and ends with the DynamoDB table. The table needs to be created first, and the Lambda function should be created last.
Terraform automatically detects and handles these dependencies, ensuring the correct order of resource creation.
Terraform files utilize HashiCorpβs configuration language HCL (HashiCorp Configuration Language) for defining their structure. This language incorporates expression functions that provide a range of powerful capabilities for creating resources and values dynamically, manipulating data structures, and performing simple or complex calculations within your Terraform code.
Some examples of included expression functions are:
The Terraform documentation contains a comprehensive list of all available expression functions.
By using expression functions, you can enhance the dynamism and flexibility of your Terraform code, thereby reducing the need for repetitive or manual tasks.
In Terraform, a resource block typically corresponds to a single infrastructure resource. However, there are cases where we need to create multiple objects that share similar characteristics without duplicating the code.
To address this properly, we can utilize meta arguments such as for_each .
The for_each function allows us to dynamically generate multiple resources. This functionality can also be applied to blocks within our resources by combining them with dynamic blocks.
For instance, we can attach multiple EBS volumes to an EC2 instance based on a variable:
In this example, we provide a list of complex objects (a map in Terraform) to configure our EC2 instance. We iterate over each value using the for_each function and assign the corresponding field values to the EBS configuration.
We also utilize the lookup expression, which retrieves the value for a given key (in this case, the IOPS value via "iops") or returns a fallback value of null.
If an empty list is passed, the dynamic block will be empty, resulting in no EBS volumes being added.
Terraform provides a convenient way to efficiently handle multiple environments, such as development, staging, and production, by utilizing its module-based approach.
To create multiple environments, you have two options: utilizing different workspaces or employing separate state files. This approach not only simplifies the deployment process but also allows for the seamless configuration of various AWS regions. By duplicating the main region, each region can receive an exact replica of the original setup.
β
Useful References for Terraform Terraform is a complex and extensive ecosystem that thrives on community collaboration. Here are some valuable resources to explore:
For a comprehensive list of curated resources, visit the awesome-terraform π repository on Github. This repository serves as a centralized hub for discovering high-quality tools, modules, and community contributions.
β
Besides Terraform, there are many other IaC tools that are worth exploring:
β
As promised, there's even more to know.
After working with Terraform for five consecutive years, we love it even more than in the beginning!
If you haven't started to use an Infrastructure-as-Code tool yet: today is the day and Terraform will be an awesome choice! π
β
This week, we're introducing Yan Cui, a well-known AWS Serverless Hero and the brilliant mind behind the Production-Ready Serverless* class as well as the books Testing Serverless Architectures* & The AppSync Masterclass*.
Yan also has a podcast realworldserverless where our one and only Sandro Volpicella just recently talked about AWS Fundamentals and our upcoming project, the CloudWatch Book. Watch for an upcoming episode.
With his expertise, clients have achieved remarkable results by accelerating their projects and achieving more with less effort. If you are eager to enhance your AWS skills and gain insights into building exceptional serverless applications, make sure to join his newsletter! π
You'll receive valuable tips, step-by-step tutorials, and practical advice, all conveniently delivered straight to your inbox every week.
π Bonus #1: if you use the code HALLOWEEN23 in the checkout, you'll get a 30% discount - applicable for every product by Yan! π If you're reading this newsletter issue well after Halloween, you can also use AWSFUNDAMENTALS to get a 15% discount!
π Bonus #2: with the code NEWSLETTER15 you'll get a 15% discount for all of our AWS Fundamentals products π
* if you purchase one of Yan's books or courses through our links, we'll receive a small portion of the revenue which helps to keep this newsletter and other content free π
β
That's all for today!
We wish you all the best and a great start to the week!
Tobi & Sandro π
β
P.S.: If you've liked this issue and you're interested in learning more about Terraform, be sure to check out our starter guide which also includes hands-on examples! π§βπ» π©βπ»
Still hungry for AWS content? Have a look at our blog! π β
Join our community of over 8,800 readers delving into AWS. We highlight real-world best practices through easy-to-understand visualizations and one-pagers. Expect a fresh newsletter edition every two weeks.
β Reading time: 11 minutes π Main Learning: Step Functions - Express vs. Standard πΎ GitHub Code βοΈ Blog Post Hey Reader while Sandro is learning something new at the AWS Community Day in Munich today, we'll explore Express and Standard Step Functions, the two types of workflows offered by AWS Step Functions. Weβll break down their differences, when to use each, and the benefits of both. Example Application: running both workflow types to see their performance differences If you want to try...
Hey Reader First things first: we apologize for not providing updates on The CloudWatch Book for a while! π’ Sometimes, things don't go as planned and unexpected obstacles arise. But now, we're back in action, creating videos and putting the final touches on the book's content! π₯ Don't just take our word for it! As an early subscriber, here's a free video from one of our favorite chapters: Anomaly Detection π In this deep-dive, you'll learn how to detect unusual patterns in metrics without...
β Reading time: 11.5 minutes π Main Learning: Host Web Applications on AWS with the CloudFront Hosting Toolkit π¨π½π» GitHub Code π Blog Post Hey Reader ππ½ Happy New Week! I (Sandro) will attend the Serverless Days in Milano next week where Jeremy Daley will hold the keynote. I look forward to meeting many of you and the overall AWS community. This week's newsletter is all about hosting your frontend on AWS. AWS launched a new way of deploying your frontend to it: The CloudFront Hosting Toolkit....