2. Elastic Compute Cloud
During these workshops, we will use the default local backend. A backend is a place where the state of your infrastructure is stored.
The state is kept in JSON format in a file with tfstate extension. It stores all information about your infrastructure, including sensitive data like database credentials. Due to this fact, the state shouldn't be kept in a version control system. An example .gitignore file for Terraform is available here.
Create a directory on your computer for these workshops. I will refer to this directory as a root directory.
In your root directory create terraform directory. Inside it, create webserver directory with main.tf file and add the following code to it:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.62.0"
}
}
required_version = ">= 1.0.8"
}
provider "aws" {
region = "eu-central-1"
}
resource "aws_instance" "webserver" {
ami = "ami-091f21ecba031b39a"
instance_type = "t2.micro"
tags = {
Name = "TerraformWorkshops"
}
}The terraform {} block contains settings, including AWS provider installed from Terraform Registry. Providers are plugins that implement resource types. We will use AWS provider to create resources on AWS Cloud in eu-central-1 region (Europe, Frankfurt).
In the webserver directory, run terraform fmt command to format the code.
Next, run terraform init command to install providers.
Now you can use terraform validate command to validate the configuration
Once validation succeeded you can use terraform plan command to see what Terraform needs to do to achieve described infrastructure.
Run terraform apply command to deploy your resources. Verify displayed execution plan and type yes to confirm.
Depending on the type of change you want to do, Terraform will perform an update in-place (e.g tag change) or destroy and then create a replacement (e.g AMI change).
Go to EC2 Dashboard on AWS Console to see created EC2 instance.
Let's create SSH key pair and use it to connect to the EC2 instance.
Make the following update to add key pair and security group with ingress and egress rules and use them with the EC2 instance:
Run terraform apply command to update your resources.
Once changes are done, go to AWS Console and find the public IP address of your instance and connect via SSH (make sure to use your EC2 instance IP address instead of 3.120.139.14):
Verify if the instance can connect to the Internet by running sudo apt-get update command:
Now let's fire up a simple webserver:
From another terminal window use curl to send a GET request at the public IP address of your EC2 instance and port 5000:
Exit EC2 instance:
Let's make life easier and create:
variableto define server port and use it in security group's ingress rule and user data scriptuser_datascript that will fire up webserver when an EC2 instance is upoutputthat will give us a public IP address of an instance
Apply changes.
Next, we can polish the config by using:
data sourceto get the latest Ubuntu Amazon Machine Image ID valuetemplatefilefunction to move bash script to a separate file
Apply changes and verify if everything works. Run terraform output command to get the EC2 instance public IP address:
To get a list of created resources run terraform state list command:
Finally, execute terraform destroy command in order to delete your resources.
Last updated
Was this helpful?