<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nazrul Hassan</title>
    <description>The latest articles on DEV Community by Nazrul Hassan (@nazrul7711).</description>
    <link>https://dev.to/nazrul7711</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2166104%2F3c6b13ed-2d36-4834-a1d4-ed6a88b4057a.jpeg</url>
      <title>DEV Community: Nazrul Hassan</title>
      <link>https://dev.to/nazrul7711</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nazrul7711"/>
    <language>en</language>
    <item>
      <title>Building a Multi-User Environment in AWS with Terraform</title>
      <dc:creator>Nazrul Hassan</dc:creator>
      <pubDate>Fri, 18 Oct 2024 13:40:04 +0000</pubDate>
      <link>https://dev.to/nazrul7711/building-a-multi-user-environment-in-aws-with-terraform-32a7</link>
      <guid>https://dev.to/nazrul7711/building-a-multi-user-environment-in-aws-with-terraform-32a7</guid>
      <description>&lt;p&gt;In this blog post, we'll explore a project where we created a multi-user environment in AWS using Terraform. This setup aims to establish a structured permissions model that accommodates different user groups and roles within an AWS account. Specifically, we have three categories of users: administrators, cloud engineers, and developers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project Overview&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The primary aim of this project is to create distinct user groups with tailored permissions, ensuring that each group has the necessary access rights to perform their specific tasks. Here's a breakdown of the groups we are setting up:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Administrator Group&lt;/strong&gt;: This group will have full rights to manage all resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cloud Engineer Group&lt;/strong&gt;: Members will have permissions related to cloud infrastructure management.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Developer Role&lt;/strong&gt;: Developers can assume this role to perform specific actions without granting them full access.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let's delve into the Terraform code that accomplishes this setup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Explanation&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Below is the complete code for setting up the multi-user environment in AWS using Terraform. Each line is commented to provide clear explanations of what each part does.&lt;/p&gt;

&lt;p&gt;Start by creating a project folder and a Terraform file named main.tf, then place the code below in that file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# When we run terraform init, Terraform will download the AWS provider plugin from this line of code
provider "aws" {
  region = "us-east-1"  # Specify the AWS region
}

# Administrator Group
resource "aws_iam_group" "administrator-group" {
  name = "administrator-group"  # Create a group for administrators
}

# User Creation
resource "aws_iam_user" "user-1" {
  name = "user-1"  # Create a user named user-1
}

# AWS Managed Policy Attachment for Administrator Group
resource "aws_iam_policy_attachment" "administrator-access" {
  name       = "administrator-access"
  policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"  # Attach the AdministratorAccess policy
  groups     = [aws_iam_group.administrator-group.name]  # Add to the administrator group
  users      = ["your-user"]  # Include the existing admin user. If not included, this user will lose admin rights.
}

# Group Membership for Administrators
resource "aws_iam_group_membership" "add-to-administrators-group" {
  name  = "add-to-administrators-group"
  group = aws_iam_group.administrator-group.name  # Add user-1 to the admin group
  users = [aws_iam_user.user-1.name]
}

# Cloud Engineer Group
resource "aws_iam_group" "cloud-engineer-group" {
  name = "cloud-engineer-group"  # Create a group for cloud engineers
}

# User for Cloud Engineers
resource "aws_iam_user" "user-2" {
  name = "user-2"  # Create a user named user-2
}

# Group Membership for Cloud Engineers
resource "aws_iam_group_membership" "add-to-cloud-engineer-group" {
  name  = "add-to-cloud-engineer-group"
  group = aws_iam_group.cloud-engineer-group.name  # Add user-2 to the cloud engineer group
  users = [aws_iam_user.user-2.name]
}

# Custom JSON Policy Document for Cloud Engineers Group Policy
data "aws_iam_policy_document" "cloud-engineer-policy" {
  statement {
    effect    = "Allow"
    actions   = [ 
      "cloudformation:CreateStack",
      "cloudformation:ListStacks",
      "cloudformation:DeleteStack",
      "ec2:RunInstances",
      "ec2:StopInstances",
      "ec2:StartInstances"
    ]
    resources = ["*"]  # Allow actions on all resources
  }
}

# Inline Policy for Cloud Engineers
resource "aws_iam_group_policy" "cloud-engineer-policy" {
  policy = data.aws_iam_policy_document.cloud-engineer-policy.json 
  group  = aws_iam_group.cloud-engineer-group.name  # Attach policy to the cloud engineer group
}

# Developer Role Trust Policy JSON Document
data "aws_iam_policy_document" "developer-role" {
  statement {
    effect = "Allow"
    actions = ["sts:AssumeRole"]  # Allow assumption of this role
    principals {
      type        = "AWS"
      identifiers = ["*"]  # Allow any AWS entity to assume this role
    }
  }
}

# Developer Role
resource "aws_iam_role" "developer-role" {
  name                 = "developer-role"
  assume_role_policy   = data.aws_iam_policy_document.developer-role.json  # Trust policy for the developer role
}

# Developer Permissions Policy JSON Document
data "aws_iam_policy_document" "developer-access" {
  statement {
    effect = "Allow"
    actions = [
      "lambda:CreateFunction",
      "lambda:DeleteFunction",
      "s3:GetObject",
      "s3:ListBucket"
    ]
    resources = [
      "arn:aws:lambda:us-east-1:471112828017:*",  # Specify resources for Lambda
      "arn:aws:s3:::*",
      "arn:aws:s3:::*/*"
    ]
  }
}

# Customer Managed Policy for Developer Role
resource "aws_iam_policy" "developer-policy" {
  policy = data.aws_iam_policy_document.developer-access.json  # Use the defined permissions
}

# Attach Developer Policy to Role
resource "aws_iam_role_policy_attachment" "attach-developer-policy" {
  role       = aws_iam_role.developer-role.name
  policy_arn = aws_iam_policy.developer-policy.arn  # Attach policy to the developer role
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Initialize Terraform&lt;/strong&gt;: Run the following command in your console. This step will download the required plugins:&lt;br&gt;
&lt;code&gt;terraform init&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Plan the Infrastructure&lt;/strong&gt;: This command will show what infrastructure this code will create in AWS:&lt;br&gt;
&lt;code&gt;terraform plan&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Apply the Changes&lt;/strong&gt;: Run this command, and it will prompt you for confirmation. Type yes to proceed. If everything is correct, this will create the groups, users, and roles in AWS:&lt;br&gt;
&lt;code&gt;terraform apply&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This should create resources in AWS,feel free to tweak any sections further.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Hosting React static website on AWS S3 with terraform</title>
      <dc:creator>Nazrul Hassan</dc:creator>
      <pubDate>Tue, 15 Oct 2024 07:58:39 +0000</pubDate>
      <link>https://dev.to/nazrul7711/hosting-react-static-website-on-aws-s3-with-terraform-11hn</link>
      <guid>https://dev.to/nazrul7711/hosting-react-static-website-on-aws-s3-with-terraform-11hn</guid>
      <description>&lt;p&gt;Nowadays, hosting a static website is fairly easy, thanks to AWS S3 and infrastructure-as-code tools like Terraform. In this post, I will walk you through the entire process of building a static React application and hosting it on AWS S3 while managing our infrastructure with Terraform.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Make sure you have the following set up:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Node.js and npm&lt;/strong&gt;: To create and build React applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AWS Account&lt;/strong&gt;: To host your static website in AWS S3.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Terraform&lt;/strong&gt;: For infrastructure management.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AWS Cli&lt;/strong&gt;: To interact with AWS resources.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Build Static Application With React&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First we create our react application using create react app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app my-static-site
cd my-static-site
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once your application is ready, run it locally with &lt;br&gt;
&lt;code&gt;&lt;br&gt;
npm start&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
When your application is ready , build your static files:&lt;br&gt;
&lt;code&gt;&lt;br&gt;
npm run build&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
This command will create a build directory in your React project that will contain your static files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Set up Terraform&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lets set up our Terraform configuration to create s3 bucket and host our React static application.&lt;/p&gt;

&lt;p&gt;Create a main.tf(by convention we call it main.tf you could name it anything you like) in your project directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;provider "aws" {
  region = "us-east-1"  // any region you want your aws configured to
  access_key = "your-aws-access-key" //if aws cli is configured then you dont need this
  secret_key = "your-aws-secret-key" //if aws cli is configured then you dont need this 
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;run this command in your console, this command will initialize the working directory and download required plugins.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;terraform init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now include the following commands in your main.tf:&lt;br&gt;
for creating a S3 bucket.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_s3_bucket" "example" {
  bucket = "your-unique-s3-bucket-name" 

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unblock your S3 bucket to allow public access. This is necessary for the next step, where we will create a bucket policy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_s3_bucket_public_access_block" "example" {
  bucket = aws_s3_bucket.example.id 
  block_public_acls = false
  block_public_policy = false
  ignore_public_acls = false
  restrict_public_buckets = false  
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We now utilize the aws_iam_policy_document data source, which allows us to create IAM JSON policies. Notably, in the resources section, we specify both the bucket itself and "/*" to encompass all objects within that bucket. This distinction is important because our actions include both s3:ListBucket and s3:GetObject. The s3:GetObject action applies to individual objects, while the s3:ListBucket action pertains to the entire S3 bucket.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data "aws_iam_policy_document" "example" {
  statement{

    principals {
      type = "AWS"
      identifiers = ["*"]
    }    

    effect = "Allow"
    actions = [
      "s3:GetObject",
      "s3:ListBucket"
    ]

    resources = [
      aws_s3_bucket.example.arn ,
      "${aws_s3_bucket.example.arn}/*"
    ]
  }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we create our bucket policy, utilizing the JSON policy generated by the aws_iam_policy_document data source from above step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_s3_bucket_policy" "example" {
  bucket = aws_s3_bucket.example.id 
  policy = data.aws_iam_policy_document.example.json

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now above commands in your main.tf run this command in console, this will  generate an execution plan and show you the changes that this script will bring about.&lt;br&gt;
&lt;code&gt;terraform plan&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now run &lt;br&gt;
&lt;code&gt;terraform apply&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;type yes to create the architecture&lt;/p&gt;

&lt;p&gt;Now, we will upload the react static application that we created above to our AWS s3 bucket(run npm run build command before running below command).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws s3 sync build s3://your-bucket-name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once your file is uploaded, go to your main.tf and add these block&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_s3_bucket_website_configuration" "example" {
  bucket = aws_s3_bucket.example.id 
  index_document {
    suffix = "index.html"    
  }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will enable your bucket for static website hosting. If everything is set up correctly, you will see a link in the static website section under the permissions of your bucket.&lt;/p&gt;

&lt;p&gt;It will look something like this&lt;br&gt;
&lt;code&gt;&lt;br&gt;
http://my-static-site-bucket.s3-website-us-east-1.amazonaws.com&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

</description>
      <category>terraform</category>
      <category>s3</category>
      <category>react</category>
      <category>iam</category>
    </item>
  </channel>
</rss>
