CHAPTER 09
Terraform with AWS
Updated: May 15, 2026
30 min read
# CHAPTER 9
Terraform with AWS
1. Introduction
Amazon Web Services (AWS) is the dominant force in cloud computing, and the AWS Provider is the most heavily utilized plugin in the Terraform ecosystem. While Terraform is cloud-agnostic, the code you write is cloud-specific. You cannot use anaws_instance block to build a server in Microsoft Azure. Therefore, mastering Terraform requires mastering the specific syntax and resource relationships of your chosen cloud provider. In this chapter, we will focus exclusively on AWS, provisioning core infrastructure including IAM users, EC2 virtual machines, and S3 storage buckets.
2. Learning Objectives
By the end of this chapter, you will be able to:- Authenticate the AWS Provider securely.
- Provision an S3 Bucket and configure security policies.
- Create an IAM User and assign permission policies.
- Provision an EC2 instance using a dynamic Data Source.
- Understand the relationship between Security Groups and EC2.
3. Beginner-Friendly Explanation
Imagine AWS as a massive, heavily guarded hotel.- IAM (Identity and Access Management): The hotel security desk. It gives you a keycard and determines if you are allowed to enter the gym or only your room.
- S3 (Simple Storage Service): A secure locker in the basement where you can throw millions of files, pictures, and documents.
- EC2 (Elastic Compute Cloud): A hotel room with a computer in it that you can rent by the minute.
- Security Group: The bouncer standing directly outside your specific hotel room door, deciding who is allowed to knock.
4. Provisioning S3 (Storage)
Let's look at the HCL required to build a private S3 bucket.
hcl
5. Provisioning IAM (Security)
Creating a user and giving them "Read Only" access.
hcl
6. Mini Project: Deploy AWS Infrastructure
Let's build a secure Web Server. We need a Security Group (Firewall) to allow web traffic (Port 80) and an EC2 instance. We will use a Data Source to dynamically find the latest Ubuntu Operating System image, rather than hardcoding an ID that will expire next month.Step-by-Step Architecture Concept:
hcl
7. Real-World Scenarios
A company launched a new web application. They hardcoded theami (Operating System ID) in their Terraform code to a specific Amazon Linux image. Six months later, they tried to scale up and run terraform apply to create 10 more servers. Terraform failed. AWS had deprecated and removed that specific AMI ID because it was too old and contained vulnerabilities. The DevOps team had to rewrite the code during an outage. They replaced the hardcoded string with an awsami Data Source, ensuring Terraform would dynamically fetch the newest, most secure OS version every time it ran, permanently solving the problem.
8. Best Practices
-
Data Sources over Hardcoding: As demonstrated, a
datablock is the opposite of aresourceblock. Aresource*creates* something. Adatablock *reads* something that already exists in the cloud. Always use Data Sources to dynamically fetch IDs (like VPC IDs, Subnet IDs, or AMIs) rather than hardcoding magic strings into your code.
9. Security Recommendations
-
0.0.0.0/0 (The Danger Zone): In our Security Group example, we allowed
0.0.0.0/0(The entire public internet) to access Port 80. This is fine for a public web server. NEVER use0.0.0.0/0for Port 22 (SSH) or Port 3306 (MySQL). Databases and administrative ports must be strictly restricted to your company's specific IP addresses, or placed in private subnets.
10. Troubleshooting Tips
-
Eventual Consistency: Sometimes you run
terraform applyto create an IAM Role, and instantly try to attach it to an EC2 instance in the next block. AWS might throw an error saying "Role not found." Cloud APIs take a few seconds to sync globally. If this happens, you may need to add a small sleep timer or rerun the apply.
11. Exercises
-
1.
What is the fundamental difference between a
resourceblock and adatablock in Terraform?
-
2.
In the AWS Provider, why must the
awss3bucketname be globally unique, not just unique within your own account?
12. FAQs
Q: Do I have to define the VPC (Network) every time I make an EC2 instance? A: No. If you omit the networking arguments (likesubnetid), the AWS Provider will automatically place your EC2 instance inside your account's "Default VPC."
13. Interview Questions
-
Q: Explain the operational advantage of utilizing an
awsamidata source with dynamic filtering rather than hardcoding an AMI ID string in anawsinstanceresource block.
- Q: Describe the HCL structure required to provision an AWS EC2 instance and securely attach an explicitly defined Security Group. How does Terraform handle the dependency between these two resources?
14. Summary
In Chapter 9, we applied our Terraform knowledge to the world's leading cloud platform. We explored the syntax required to provision foundational AWS resources: secure S3 storage, identity and access management (IAM) policies, and virtual machines (EC2). We mastered the critical concept ofdata sources, learning how to dynamically query the AWS API for real-time information to prevent our code from becoming brittle and outdated. Finally, we demonstrated how to weave these distinct resources together, dynamically linking firewalls to servers to create a cohesive, automated cloud architecture.