AWS, Cloud Computing, DevOps

5 Mins Read

Mounting Amazon S3 Buckets Across Amazon EC2 Instances Using Terraform

Voiced by Amazon Polly

Overview

Shared storage is a cornerstone for building scalable and efficient applications in cloud computing. Amazon S3 is a durable and scalable object storage service often paired with Amazon EC2 instances to separate compute from storage, enabling cloud-native applications. In this blog, we will explore how to provision an Amazon S3 bucket and mount it across three Amazon EC2 instances using Terraform as the Infrastructure as Code (IaC) tool.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

Introduction

Amazon Web Services (AWS) provides powerful cloud services like Amazon S3 and Amazon EC2 that help build scalable and efficient applications. Amazon S3 is a highly durable and scalable object storage service designed to store and retrieve any amount of data. Amazon EC2 offers resizable compute capacity, allowing users to run virtual servers on demand. Combining Amazon S3 with Amazon EC2 enables separation of storage and compute, supporting flexible, cloud-native architectures.

Why use Amazon S3 with Amazon EC2 instances?

Integrating Amazon S3 with Amazon EC2 offers several benefits:

  • Scalability: Amazon S3 provides unlimited storage, allowing applications to scale seamlessly.
  • Durability: Data stored in Amazon S3 is replicated across multiple facilities, ensuring high availability.
  • Cost Efficiency: With pay-as-you-go pricing, Amazon S3 minimizes costs for storing large datasets.
  • Flexibility: Amazon EC2 instances can access Amazon S3 buckets to store application data, make backups, or host static assets.

Prerequisites

  • AWS Account: Ensure you have an AWS account with the necessary permissions to create resources like Amazon VPCs, Amazon EC2 instances, Amazon S3 buckets, and AWS IAM roles.
  • Terraform: Install Terraform on your machine to manage infrastructure as code.

Solution Overview

The goal is to create a shared storage system where three Amazon EC2 instances in separate availability zones can access a single Amazon S3 bucket. Each instance will mount the bucket using s3fs, an open-source FUSE file system that enables Amazon S3 buckets to appear as local file systems.

Infrastructure Setup

Terraform Scripts

  • VPC.tf: Creates Amazon VPC with three subnets in different availability zones.
  • EC2.tf: Provisions three Amazon EC2 instances and attaches AWS IAM roles for Amazon S3 access.
  • S3.tf: Creates an Amazon S3 bucket for shared storage.
  • Variable.tf: Centralizes configuration variables for easy management.
  • Userdata.sh: Configures each Amazon EC2 instance to mount the Amazon S3 bucket during boot.

VPC.tf

step1

EC2.tf

step2

S3.tf

step3

Variable.tf

Userdata.sh

AWS IAM role

An AWS IAM role is attached to each Amazon EC2 instance to grant permissions for accessing the Amazon S3 bucket. This ensures secure and seamless integration without hardcoding credentials.

Attached is the AWS IAM role to Amazon EC2 instances:

Solution verification

Checking the Amazon EC2 instance has mounted the Amazon S3 bucket:

step4

Create a new file named test3.txt, add content, and verify its presence in the Amazon S3 bucket.

step5

step6

step7

step8

Key Benefits

  1. High Availability: Deploying instances in separate availability zones ensures resilience against failures.
  2. Centralized Storage: The Amazon S3 bucket is a single source of truth for all instances.
  3. Automation: Terraform simplifies resource provisioning and ensures repeatability.

Common use cases

  • Hosting static website assets like HTML, CSS, and images directly from Amazon S3
  • Backing up application data from Amazon EC2 instances into durable Amazon S3 storage.
  • Storing large datasets for processing by containerized applications running on Amazon EC2.

Conclusion

By leveraging Terraform and AWS services like Amazon EC2 and Amazon S3, you can build resilient, cost-effective, scalable shared storage solutions. This architecture empowers developers to focus on application logic while benefiting from the scalability and durability of Amazon’s cloud infrastructure.

Drop a query if you have any questions regarding Amazon EC2, Amazon S3 or Terraform and we will get back to you quickly.

Making IT Networks Enterprise-ready – Cloud Management Services

  • Accelerated cloud migration
  • End-to-end view of the cloud environment
Get Started

About CloudThat

CloudThat is an award-winning company and the first in India to offer cloud training and consulting services worldwide. As a Microsoft Solutions Partner, AWS Advanced Tier Training Partner, and Google Cloud Platform Partner, CloudThat has empowered over 850,000 professionals through 600+ cloud certifications winning global recognition for its training excellence including 20 MCT Trainers in Microsoft’s Global Top 100 and an impressive 12 awards in the last 8 years. CloudThat specializes in Cloud Migration, Data Platforms, DevOps, IoT, and cutting-edge technologies like Gen AI & AI/ML. It has delivered over 500 consulting projects for 250+ organizations in 30+ countries as it continues to empower professionals and enterprises to thrive in the digital-first world.

FAQs

1. Can I mount an Amazon S3 bucket on Windows-based EC2 instances?

ANS: – Yes, although mounting is more straightforward on Linux-based instances using s3fs, Windows users can interact with Amazon S3 using AWS CLI or third-party tools like TntDrive.

2. What are the security considerations when integrating Amazon EC2 with Amazon S3?

ANS: – Use AWS IAM roles instead of hardcoding credentials on your instances. Configure bucket policies and access control lists (ACLs) to restrict access.

WRITTEN BY Abhishek Dubey

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!