DEV Community

Jeshlin P V
Jeshlin P V

Posted on

Blog: Creating, Modifying, and Destroying an EC2 Instance and Hosting a Static Website Using S3 in AWS with Terraform

Introduction

Hello Friends! As part of my internship, I’ve been exploring the capabilities of Terraform and AWS. Recently, I worked on creating, modifying, and destroying an EC2 instance, and hosting a static website using S3 with Terraform. This blog will detail the steps I followed, the challenges I faced, and the solutions I found.

Understanding the tools

Terraform

Terraform is an open-source tool that allows you to define and provision infrastructure as code. Today, I learned some of the basics of Terraform and used it to create an EC2 instance and an S3 bucket.

Amazon EC2 (Elastic Compute Cloud)

Amazon EC2 provides scalable virtual servers in the cloud, allowing you to run applications and services with flexible compute power.

Amazon S3 (Simple Storage Service

Amazon S3 is a scalable object storage service used for storing and retrieving any amount of data. It’s ideal for hosting static websites due to its simplicity and reliability.

Here’s a brief overview of my Terraform journey:

● Installation: I started by installing Terraform on my local machine. This was straightforward, requiring just a few commands to download and install the binary.

● Writing Configuration Files: Terraform uses configuration files written in HashiCorp Configuration Language (HCL). I created a simple configuration file to define an EC2 instance and an S3 bucket.

● Applying the Configuration: Using the “terraform apply” command, I provisioned the resources defined in my configuration file. Terraform handled the creation of the EC2 instance and S3 bucket, showing me the power of infrastructure as code.

Here's how I did it:

To start, I created a new Terraform configuration file and defined the AWS provider to specify the region. Then, I configured an EC2 instance resource with details like the AMI ID and instance type. After initializing Terraform to download the necessary plugins, I applied the configuration to create the EC2 instance. To modify the instance, I updated the instance type in the configuration file and re-applied the changes, which Terraform detected and prompted me to confirm. Finally, to destroy the instance, I used the terraform destroy command, which also required confirmation.

For hosting a static website on S3, I configured an S3 bucket resource in the same Terraform configuration file, specifying the bucket name, access control settings, and website configuration. I included the necessary resources to upload the HTML files to the bucket. After initializing and applying the configuration again, the static website was accessible via the S3 bucket’s website endpoint. Throughout this process, I ensured unique bucket names and appropriate IAM permissions.

Challenges and Solutions

During this process, I encountered a few challenges:

  1. Unique Bucket Names: S3 bucket names must be globally unique. I had to experiment with different names until I found one that was available.

  2. IAM Permissions: Ensuring the IAM user had the necessary permissions to create S3 buckets and upload objects was crucial for smooth operations.

Conclusion

Working with Terraform to create, modify, and destroy an EC2 instance, and host a static website on S3, was an enlightening experience. This hands-on approach demonstrated the power of IaC in managing cloud resources efficiently and consistently. I'm excited to continue exploring Terraform and AWS, learning more about automating and managing infrastructure.

Top comments (0)