Previous: Configuration Management (CM), Up: Infrastructure Provisioning [Contents]
Cloud infrastructure contrasts with the on-premise solution (see Infrastructure as Code (IaC/IaaC/IaaS)) by renting a collection of virtualized resources, including servers, storage, networks, and services, that are provided by cloud service providers over the Internet.
This is an example of a cloud-based infrastructure desired state using Terraform’s HCL to utilize Amazon’s AWS cloud service, in this case deploying an Elastic Compute Cloud (EC2) node with 1 vCPU and 1GB RAM (t2.micro).
This file (‘./secrets/aws.tfvars’) will host all variables that will be used later on to configure and deploy resources. Typically, they contain credentials and/or sensitive information, thus, it must be ignored when committing to a repository. |
aws_access_key = "<ACCESS_KEY>" aws_secret_key = "<SECRET_KEY>" aws_key_pair_name = "<KEY_PAIR_NAME>"
This file (‘./providers/aws/aws.tf’) will configure and establish the connection with the service provider (in this case, AWS), specifying both the region to connect to as well as the credential information needed to authenticate us. |
variable "aws_access_key" { default = "NOT_FOUND" sensitive = true type = string } variable "aws_secret_key" { default = "NOT_FOUND" sensitive = true type = string } provider "aws" { region = "eu-south-2" access_key = var.aws_access_key secret_key = var.aws_secret_key }
This file (‘./providers/aws/os-ubuntu.tf’) serves as a data holder for certain information that will be used ofter while deploying instances with AWS, in this case (i.e. an OS specific version). |
data "aws_ami" "ubuntu" { most_recent = true # Ubuntu Server Jammy 22.04 LTS filter { name = "name" values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-*"] } # Hardware Virtual Machine filter { name = "virtualization-type" values = ["hvm"] } # Canonical owners = ["099720109477"] }
This file (‘./nodes/test-node.tf’) is the one where a test node is defined, as an AWS EC2 t2.micro instance in this case. |
variable "aws_key_pair_name" { default = "NOT_FOUND" sensitive = true type = string } resource "aws_instance" "test-node" { ami = data.aws_ami.ubuntu.id instance_type = "t2.micro" key_name = var.aws_key_pair_name tags = { Name = "test-node" } } output "public_ip" { value = aws_instance.test-node.public_ip }
From this point, all needed infrastructure resources can be described and deployed using Terraform, from a simple web server to an entire Kubernetes cluster, and manage all from within a Git repository.
Previous: Configuration Management (CM), Up: Infrastructure Provisioning [Contents]