Here is some information about this architecture.
Here are the steps you can follow to build this solution on your own.
In this lesson and lab you’ll learn how to create a VPC that has two subnets, with each subnet being in different Availability Zones within the same region.
If you are new to working with the VPC, we recommend starting at the “VPC With One Subnet” lab and working your way through that.
A Virtual Private Cloud (VPC) in AWS can span all the availability zones in a particular region. Availability Zones (AZs) are isolated locations within a region that provide high availability and fault tolerance for your applications.
By dividing the VPC network space across two (or more) zones, you're enhancing the resilience of your architecture. If one zone experiences an issue, resources in the other zone can continue to operate, providing higher uptime.
The division is done through subnets, where each subnet resides in a different availability zone. By placing different resources in different subnets/zones, you're spreading the risk and utilizing the redundant, isolated nature of the zones.
Now, let's write the Terraform code to create the required resources as per your instructions.
First, we'll create the Terraform block to set the required Terraform version to a minimum of 1.5.0. Append the following code to the main.tf
file:
terraform {
required_version = ">= 1.5.0"
}
Explanation: This code ensures that the Terraform version used is at least 1.5.0. It's essential for maintaining consistency across different environments.
If you're using the Skillmix Labs feature, open the lab settings (the beaker icon) on the right side of the code editor. Then, click the Start Lab button to start hte lab environment.
Wait for the credentials to load. Then run this in the terminal:
$ aws configure --profile smx-lab
AWS Access Key ID [None]: AKIA3E3W34P42CSHXDH5
AWS Secret Access Key [None]: vTmqpOqefgJfse8i6QwzgpjgswPjHZ6h/oiQq4zf
Default region name [None]: us-west-2
Default output format [None]: json
Be sure to name your credentials profile 'smx-lab'.
Note: If you're using your own AWS account you'll need to ensure that you've created and configured a named AWS CLI profile named smx-lab.
We'll do all of our work in a file called main.tf. Create that file in your root project directory now and open it for editing.
Next, we'll create the AWS provider block that sets the region to us-west-2
and the profile to smx-lab
. Append the following code to the main.tf
file:
provider "aws" {
region = "us-west-2"
profile = "smx-lab"
}
Explanation: This block configures the AWS provider with the specific region and AWS CLI profile to be used. The region
is set to Oregon, and the profile
helps to manage credentials.
Now, we'll create the VPC resource and configure the CIDR block. Append the following code to the main.tf
file:
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "my_vpc"
}
}
Explanation: This block defines a VPC with a CIDR range of 10.0.0.0/16
, allowing for 65,536 private IPv4 addresses within this virtual network.
Next, we'll create the subnet resource in zone "a." Append the following code to the main.tf
file:
resource "aws_subnet" "subnet_a" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-west-2a"
tags = {
Name = "subnet_a"
}
}
Explanation: This creates a subnet within the defined VPC, specifically in availability zone us-west-2a
. The CIDR block 10.0.1.0/24
provides 256 IP addresses within this subnet.
Lastly, we'll create the subnet resource in zone "b." Append the following code to the main.tf
file:
resource "aws_subnet" "subnet_b" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.2.0/24"
availability_zone = "us-west-2b"
tags = {
Name = "subnet_b"
}
}
Explanation: This creates another subnet within the defined VPC, specifically in availability zone us-west-2b
. The CIDR block 10.0.2.0/24
provides 256 IP addresses within this subnet, separate from the first subnet, allowing for fault isolation between zones.
By following these steps, we've set up a network architecture that takes advantage of multiple availability zones within a single region. This provides resilience and fault tolerance and is a common practice in real-world cloud environments. The use of Terraform allows for code-based infrastructure management, making it easy to replicate and manage complex configurations.