Lädt...


🔧 Deploying Your First Server with Terraform: A Beginner's Guide


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Now you have installed Terraform, configured your AWS account to use the CLI, and installed VS Code. What next? Let us get deploy our first webserver.

  • On VS Code, create a directory to store your files with mkdir tf-files in the terminal
  • Next create a main.tf file with touch main.tf. You will write your configuration files here in Harshicorp Configuration Language.

code to make create directory

  • Here is the block of code that you can add in your main.tf file

Provider Block

This shows that the provider is AWS and the region to be used is us-east-2

provider "aws" {
    region = "us-east-2"
}

Resource block

The resource block is used to define the resource Terraform would create. One interesting part is that Terraform would try to figure out which order to create these resources. In this resource block, we define the following:

  1. Amazon Machine Image (AMI)
  2. Instance type
  3. Security group id
  4. User data script
  5. Tag
resource "aws_instance" "webserver" {
    ami = "ami-0fb653ca2d3203ac1"
    instance_type = "t2.micro"
    vpc_security_group_ids = [aws_security_group.instance.id]
    user_data = <<-EOF
    #!/bin/bash
    echo "Hello, World. Welcome to the use of Terraform in deploying infrastructure" > index.html
    nohup busybox httpd -f -p 8080 &
    EOF
    user_data_replace_on_change = true
    tags = {
        Name = "tf-webserver"
    }
}

This other resource block declares the security group to allow traffic from port 8080 from any IPv4 address

resource "aws_security_group" "instance" {
    name = "tf-sg"

    ingress {
        from_port = 8080
        to_port = 8080
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    } 
}

We can now save our main.tf file. Next, let us now deploy the webserver. First, we need to download the required provider plugins. Run terraform init

running terraform init

You may wonder, what are the provider plugin? These are executable binaries that the Terraform Core install on our laptop uses to communicate with the APIs of the cloud provider that has the resources we need. Notice that the screenshot uses the expression "Reusing previous version of . . ." This happens when you are running the terraform init command after the first time.

As a quick check to see if our file follows the right syntax, run terraform validate

Terraform validate

What would our infrastructure look like? You can now run terraform plan

terraform plan

This information contains what resources would be created on the AWS. Now, let us proceed to deploy these resources. Run terraform apply. A confirmation will pop-up to ensure you really want to deploy these resources. The only acceptable answer to proceed is yes

terraform apply

So quick! You now have a webserver online. Did you notice how long it took to get the EC2 instance ready? 14 seconds. That's amazing!
instance ready

How do I know that my webserver is actually working? Let us check some information about the webserver. Run terraform show

Information about your instance

Did you notice the last entry displayed? That is the public IP address of the instance. Now, let us check the web content. Run curl http://<public_ip>:8080

webcontent
Isn't that beautiful? Why not let us check out our EC2 instance on the AWS Console. Here you go!

EC2 page on console

What about our webpage? Type in your browser http://<public_ip>:8080

webpage on browser

To clean up the resources and delete everything, run terraform destroy and enter yes as a confirmation to destroy the infrastructure.

Here is the architecture of what we built

Architecture diagram

Congratulations! We have deployed our first single web server, tested it and destroyed it. Can we improve on this? A future post will show us how. Feel free to ask questions regarding this post.

...

🔧 Deploying Your First Server with Terraform: A Beginner's Guide


📈 52.52 Punkte
🔧 Programmierung

🔧 Deploying Your First Server with Terraform: A Beginner's Guide #Day3of30days


📈 52.52 Punkte
🔧 Programmierung

🔧 Deploying Your First Server with Terraform: A Beginner's Guide


📈 52.52 Punkte
🔧 Programmierung

🔧 Deploying a Static Website on AWS S3 with Terraform: A Beginner's Guide


📈 40.17 Punkte
🔧 Programmierung

🔧 Automate Your Infrastructure with Terraform: A Beginner's Guide


📈 30.9 Punkte
🔧 Programmierung

🔧 Deploying a Flask App on Cloud Run with Terraform: A Comprehensive Guide


📈 30.22 Punkte
🔧 Programmierung

🔧 A Step-by-Step Guide to Easily Deploying EKS Infrastructure and Applications Using Terraform


📈 30.22 Punkte
🔧 Programmierung

🔧 Terraform Tactics: A Guide to Mastering Terraform Commands for DevOps


📈 29.89 Punkte
🔧 Programmierung

🔧 Terraform Your Way to High Availability: Deploying a Full Stack AWS Architecture


📈 27.99 Punkte
🔧 Programmierung

🔧 Deploying your Azure Infrastructure with Terraform | The DevOps Lab


📈 27.99 Punkte
🔧 Programmierung

🔧 Deploying a Single Page Application (SPA) on AWS: A Beginner's Guide. Part 5. Connecting dots


📈 27.97 Punkte
🔧 Programmierung

🔧 Deploying a Single Page Application (SPA) on AWS: A Beginner's Guide. Part 1. Theory


📈 27.97 Punkte
🔧 Programmierung

🔧 Deploying Web Applications on Kubernetes: A Beginner's Guide


📈 27.97 Punkte
🔧 Programmierung

🔧 Building Your First AWS Web Server: A Beginner's Guide to Cloud Computing


📈 27.78 Punkte
🔧 Programmierung

🔧 Setting Up an AWS EKS Cluster Using Terraform: A Beginner-Friendly Guide


📈 27.64 Punkte
🔧 Programmierung

🔧 A Beginner’s Guide to Terraform: Managing Cloud Infrastructure


📈 27.64 Punkte
🔧 Programmierung

🔧 Building and Deploying Your First Serverless Azure Function with .NET 8: A Step-by-Step Guide


📈 27.05 Punkte
🔧 Programmierung

🔧 Deploying to Azure with Terraform and GitHub Actions


📈 24.73 Punkte
🔧 Programmierung

🔧 Deploying an E-commerce Website on Amazon EKS Using Terraform


📈 24.73 Punkte
🔧 Programmierung

🔧 Terraform in Action: Deploying an Ubuntu EC2 Instance with Nginx on AWS


📈 24.73 Punkte
🔧 Programmierung

🔧 Deploying Apps to AWS with Terraform - Network Setup 01


📈 24.73 Punkte
🔧 Programmierung

🔧 Deploying a NestJS Application to Amazon ECS using GitHub Actions and Terraform


📈 24.73 Punkte
🔧 Programmierung

🔧 Deploying a Highly Available Web Application on AWS Using Terraform


📈 24.73 Punkte
🔧 Programmierung

📰 Deploying SageMaker Endpoints With Terraform


📈 24.73 Punkte
🔧 AI Nachrichten

🔧 Deploying a Highly Available Web App on AWS Using Terraform


📈 24.73 Punkte
🔧 Programmierung

🔧 Deploying MySQL Group Replication With Terraform


📈 24.73 Punkte
🔧 Programmierung

🔧 Deploying Basic Infrastructure on AWS with Terraform


📈 24.73 Punkte
🔧 Programmierung

🔧 Deploying Percona Monitoring and Management With Terraform


📈 24.73 Punkte
🔧 Programmierung

matomo