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 a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.

CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery PartnerAmazon CloudFront Service Delivery PartnerAmazon OpenSearch Service Delivery PartnerAWS DMS Service Delivery PartnerAWS Systems Manager Service Delivery PartnerAmazon RDS Service Delivery Partner, AWS CloudFormation Service Delivery Partner and many more.

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!