Day 3 : Provisioning on AWS Made Easy with Terraform
Provisioning resources on AWS has never been easier and more straightforward thanks to Terraform. In this blog post, we will walk through the steps required to get started with Terraform and provision AWS resources effortlessly. Let's dive in!
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
AWS CLI Installed: The AWS Command Line Interface (AWS CLI) is a powerful tool that allows you to manage your AWS services from the command line. If you haven't installed it yet, go ahead and get it set up on your machine.
AWS IAM User: To connect your AWS account with Terraform, you'll need an IAM user. IAM (Identity and Access Management) enables you to securely control access to your AWS resources. Make sure you have an IAM user created with the necessary permissions.
Export AWS Access Keys
To connect Terraform with your AWS account, you'll need to export your AWS access keys and secret access keys to your machine. Use the following commands to export them:
export AWS_ACCESS_KEY_ID=<access key>
export AWS_SECRET_ACCESS_KEY=<secret access key>
Install Required Terraform Providers
In your Terraform configuration file, specify the required providers and versions. Here's an example of how to set it up for AWS:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.6.2"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "us-east-1"
}
AWS EC2 Instance Provisioning
Now, let's provision an EC2 instance on AWS using Terraform. Add the following code to your Terraform configuration file:
resource "aws_instance" "aws_ec2_test" {
count = 4
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
tags = {
Name = "TerraformTestServerInstance"
}
}
Getting the Public IPs for Provisioned Instances
To retrieve the public IPs of the provisioned instances, use the following output block in your Terraform configuration file:
output "instance_pub_ip" {
value = aws_instance.aws_ec2_test[*].public_ip
}
Creating an S3 Bucket
Let's create an S3 bucket using Terraform. Add the following code to your configuration file:
resource "aws_s3_bucket" "b" {
bucket = "tutrisbutris-tf-test-bucket"
tags = {
Name = "bubu-bucket"
Environment = "Tutris"
}
}
Here's how the main.tf file looks like:
That's it! You're now ready to provision AWS resources with Terraform. Simply run the following commands:
terraform init
terraform plan
Terraform Apply: Execution Plan
During the Terraform apply process, Terraform generates an execution plan that outlines the actions it will perform. Let's take a look at the execution plan for provisioning AWS EC2 instances using Terraform:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_instance.aws_ec2_test[0] will be created
+ resource "aws_instance" "aws_ec2_test" {
...
}
# aws_instance.aws_ec2_test[1] will be created
+ resource "aws_instance" "aws_ec2_test" {
...
}
# aws_instance.aws_ec2_test[2] will be created
+ resource "aws_instance" "aws_ec2_test" {
...
}
# aws_instance.aws_ec2_test[3] will be created
+ resource "aws_instance" "aws_ec2_test" {
...
}
}
The execution plan indicates that Terraform will create four instances of the AWS EC2 resource type (aws_instance
). Each instance is represented by an aws_instance.aws_ec2_test
resource block.
The +
symbol signifies that Terraform will create the specified resources. In this case, it will create four EC2 instances based on the defined configuration.
The plan provides an overview of the attributes and settings for each instance, such as the AMI ID, instance type, tags, and more. However, certain attributes are displayed as (known after apply)
since their values will be determined at runtime.
By executing the terraform apply
command, Terraform will interact with the AWS API and provision the EC2 instances according to the defined configuration. The execution plan serves as a preview of the changes that will be made.
terraform apply
Terraform Destroy
Terraform destroy is a command used to tear down provisioned infrastructure. It executes a destruction plan, deletes resources in a controlled manner, updates the state file, and ensures dependencies are handled properly. Use with caution and confirm intentional deletions.
In this blog post, we covered the prerequisites for using Terraform with AWS, installing the required providers, provisioning an EC2 instance, retrieving instance IPs, creating an S3 bucket and destroying the provisioned infrastructure. With Terraform, infrastructure provisioning becomes a breeze.
Start exploring Terraform and take advantage of its capabilities to streamline your AWS infrastructure management. Happy provisioning!
#Terraform #AWS #InfrastructureAsCode #DevOps