Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added solution.md for Day 1 of TerraWeek: Introduction to Terraform Basics and AWS Setup #21

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Amitabh-DevOps
Copy link

@Amitabh-DevOps Amitabh-DevOps commented Dec 2, 2024

This PR covers the Day 1 activities for TerraWeek, focusing on introducing Terraform and setting up the basics for managing infrastructure as code (IaC) with AWS.

Key Highlights

  1. Introduction to Terraform

    • Overview of Terraform and its advantages in managing IaC.
    • Benefits such as consistency, automation, and cross-cloud compatibility.
  2. Installation and Setup on AWS

    • Step-by-step guide to launching an Ubuntu EC2 instance for executing Terraform commands.
    • Installation of Terraform and AWS CLI on the EC2 instance.
    • Configuring AWS credentials for Terraform using IAM.
  3. Core Concepts in Terraform

    • Provider: Defines cloud services (e.g., AWS, Azure).
    • Resource: Describes individual components like S3 buckets or EC2 instances.
    • State: Tracks the infrastructure's current state.
    • Module: Logical grouping of reusable configurations.
    • Data Source: Retrieves data from external sources for use in configurations.
  4. Terraform Configuration Example

    • Setting up AWS as a provider.
    • Sample .tf configurations for S3 buckets and AMIs.

References

This submission serves as the foundation for upcoming days of TerraWeek, focusing on practical implementation and advanced concepts.

Next Steps

  • Implementing first Terraform resources on AWS.
  • Exploring advanced configurations and state management.

Looking forward to feedback and suggestions!

Summary by CodeRabbit

  • New Features
    • Introduced a comprehensive guide on Terraform basics and installation for AWS.
    • Included step-by-step instructions for setting up Terraform on an Ubuntu EC2 instance.
    • Explained key Terraform terminologies with examples for enhanced understanding.
    • Added a reference video for further learning on Terraform concepts.

Copy link

coderabbitai bot commented Dec 2, 2024

Walkthrough

The changes introduce an updated file, solution.md, which serves as a comprehensive guide on Terraform, focusing on its foundational concepts, installation on an Ubuntu EC2 instance, AWS setup, and essential terminologies. The document details Terraform's role as an Infrastructure as Code tool, outlines the installation process, and provides instructions for configuring AWS credentials and adding AWS as a provider.

Changes

File Change Summary
day01/solution.md Updated the guide on Terraform to include its introduction, installation steps on Ubuntu EC2, AWS setup instructions, and definitions of key terminologies.

Poem

In the land of cloud and code,
A guide for Terraform we bestowed.
With steps so clear, and terms defined,
A rabbit's wisdom, for all to find.
Hop along to learn and play,
Infrastructure dreams, come what may! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Outside diff range and nitpick comments (3)
day01/solution.md (3)

3-3: Adjust heading hierarchy for proper document structure

The document starts with an h3 heading (###). For proper document hierarchy, the main sections should start with h2 (##) and then increment by one level at a time.

Apply this change to fix the heading hierarchy:

-### **Day 1: Introduction to Terraform and Terraform Basics**
+## **Day 1: Introduction to Terraform and Terraform Basics**
🧰 Tools
🪛 Markdownlint (0.35.0)

3-3: Expected: h2; Actual: h3
Heading levels should only increment by one level at a time

(MD001, heading-increment)


133-137: Consider using a more flexible version constraint

The AWS provider version is pinned to a very specific version (5.65.0). Consider using a more flexible version constraint to allow for minor updates and patches while maintaining compatibility.

 required_providers {
   aws = {
     source  = "hashicorp/aws"
-    version = "5.65.0"
+    version = "~> 5.65"
   }
 }

49-51: Enhance SSH command example clarity

The SSH command example could be more clear about the placeholders and provide guidance on finding the necessary information.

-ssh -i "your-key-pair.pem" ubuntu@<EC2-Public-IP>
+ssh -i "/path/to/your-key-pair.pem" ubuntu@your-instance-public-ip
+# Note: Replace:
+# - /path/to/your-key-pair.pem with the path to your downloaded key pair
+# - your-instance-public-ip with the Public IPv4 address from your EC2 instance details
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 6a69991 and 4b22e6f.

📒 Files selected for processing (1)
  • day01/solution.md (1 hunks)
🧰 Additional context used
🪛 Markdownlint (0.35.0)
day01/solution.md

3-3: Expected: h2; Actual: h3
Heading levels should only increment by one level at a time

(MD001, heading-increment)

Comment on lines +96 to +98
- Create a new IAM user with **programmatic access**.
- Attach the necessary policies (e.g., `AdministratorAccess` or policies tailored to your use case).
- Note down the **Access Key ID** and **Secret Access Key**.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Revise IAM permissions recommendation

Recommending AdministratorAccess policy is not aligned with the principle of least privilege. Instead, suggest creating custom IAM policies that grant only the necessary permissions for the specific infrastructure being managed.

Consider updating the documentation to recommend:

  1. Creating a custom IAM policy based on the resources being managed
  2. Using AWS managed policies specific to the services being used
  3. Following the principle of least privilege

Comment on lines +107 to +119
- **Configure AWS CLI Credentials:**

Run the following command and enter your IAM user details:
```bash
aws configure
```

Provide:
- **Access Key ID**
- **Secret Access Key**
- **Default region** (e.g., `us-east-1`)
- Output format (default: `json`)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Recommend using IAM roles instead of access keys

When working with Terraform on EC2 instances, it's more secure to use IAM roles instead of storing AWS credentials directly on the instance.

Consider updating this section to:

  1. Create an IAM role with the necessary permissions
  2. Attach the IAM role to the EC2 instance during creation
  3. Remove the AWS credentials configuration steps as they won't be needed with IAM roles

Comment on lines +207 to +214
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/*"]
}
}
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance AMI data source example with additional filters

The current AMI data source example is missing important filters that are typically needed for proper AMI selection, such as virtualization-type and architecture.

 data "aws_ami" "ubuntu" {
   most_recent = true
+  owners      = ["099720109477"] # Canonical
   
   filter {
     name   = "name"
     values = ["ubuntu/images/*"]
   }
+  filter {
+    name   = "virtualization-type"
+    values = ["hvm"]
+  }
+  filter {
+    name   = "architecture"
+    values = ["x86_64"]
+  }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/*"]
}
}
```
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"] # Canonical
filter {
name = "name"
values = ["ubuntu/images/*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
}

Comment on lines +70 to +73
```bash
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve security of GPG key installation

The current GPG key installation pipes wget directly to sudo, which is not the most secure approach. Consider downloading the key first and then importing it.

-wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
+# Download the GPG key first
+wget -O hashicorp.asc https://apt.releases.hashicorp.com/gpg
+# Verify the fingerprint (optional but recommended)
+gpg --show-keys hashicorp.asc
+# Import the key
+sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg hashicorp.asc
+# Clean up
+rm hashicorp.asc

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (2)
day01/solution.md (2)

1-4: Fix heading hierarchy

The document structure jumps from h1 to h3, which violates markdown best practices. Consider restructuring the headings to maintain proper hierarchy:

-# TerraWeek Day 1
-
-### **Day 1: Introduction to Terraform and Terraform Basics**
+# TerraWeek Day 1: Introduction to Terraform and Terraform Basics
+
+## Overview
🧰 Tools
🪛 Markdownlint (0.35.0)

3-3: Expected: h2; Actual: h3
Heading levels should only increment by one level at a time

(MD001, heading-increment)


132-140: Document provider version pinning strategy

While pinning the AWS provider version is good practice, consider adding a comment explaining:

  1. Why this specific version was chosen
  2. How to handle version updates
  3. The importance of reviewing the changelog before updating

Example addition:

 terraform {
   required_providers {
     aws = {
       source  = "hashicorp/aws"
       version = "5.65.0"  # Pinned for stability
+      # Review changelog at https://github.com/hashicorp/terraform-provider-aws/releases
+      # before updating this version
     }
   }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4b22e6f and 1d74acc.

📒 Files selected for processing (1)
  • day01/solution.md (1 hunks)
🧰 Additional context used
🪛 Markdownlint (0.35.0)
day01/solution.md

3-3: Expected: h2; Actual: h3
Heading levels should only increment by one level at a time

(MD001, heading-increment)

🔇 Additional comments (4)
day01/solution.md (4)

70-73: Improve security of GPG key installation

The current GPG key installation method is not following security best practices.


96-98: Revise IAM permissions recommendation


107-119: Recommend using IAM roles instead of access keys


209-216: Enhance AMI data source example with additional filters

Comment on lines +35 to +44
1. Go to the **EC2 Dashboard** on AWS.
2. Click **Launch Instance**.
3. Configure:
- **Name:** Terraform-Setup
- **AMI:** Ubuntu Server 22.04 LTS
- **Instance Type:** t2.micro (free tier eligible)
- **Key Pair:** Create or select an existing key pair.
- **Network Settings:** Allow SSH traffic (port 22) from your IP address.
- **Storage:** Keep the default 8 GiB or adjust as needed.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance EC2 instance security recommendations

The EC2 instance setup instructions should include additional security best practices:

  1. Use VPC with private subnet and NAT Gateway
  2. Enable IMDSv2
  3. Apply security group rules with minimum required permissions
  4. Enable detailed monitoring
  5. Add proper resource tags

Consider adding these security configurations to the EC2 launch instructions.

Comment on lines +181 to +188
3. **State:**

- A file that tracks the current state of your infrastructure. It helps Terraform understand the desired state versus the actual state.

- Example:

Terraform generates a `terraform.tfstate` file during execution.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add state file security warnings

The state file section should include critical security considerations:

  1. Warning about sensitive data in state files
  2. Recommendation for remote state storage (e.g., S3 with encryption)
  3. State file backup strategies
  4. Access control for state files
  5. State locking mechanisms

Consider expanding this section to cover these important aspects of state management.

Would you like me to provide a detailed example of secure state configuration?

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
day01/solution.md (2)

1-4: Fix heading hierarchy

The document structure violates markdown heading hierarchy by jumping from h1 to h3. Headers should only increment by one level at a time.

Apply this diff:

-# TerraWeek Day 1
-
-### **Day 1: Introduction to Terraform and Terraform Basics**
+# TerraWeek Day 1
+
+## Introduction to Terraform and Terraform Basics
🧰 Tools
🪛 Markdownlint (0.35.0)

3-3: Expected: h2; Actual: h3
Heading levels should only increment by one level at a time

(MD001, heading-increment)


136-149: Add version constraint best practices

While specifying an exact version is precise, it's recommended to use version constraints that allow for compatible updates while preventing breaking changes.

Consider updating the provider block:

 terraform {
   required_providers {
     aws = {
       source  = "hashicorp/aws"
-      version = "5.65.0"
+      version = "~> 5.65.0"  # Allows minor and patch updates within 5.x
     }
   }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1d74acc and 26a9088.

📒 Files selected for processing (1)
  • day01/solution.md (1 hunks)
🧰 Additional context used
🪛 Markdownlint (0.35.0)
day01/solution.md

3-3: Expected: h2; Actual: h3
Heading levels should only increment by one level at a time

(MD001, heading-increment)

🔇 Additional comments (6)
day01/solution.md (6)

35-44: Enhance EC2 instance security recommendations

The EC2 instance setup instructions should include additional security best practices:

  1. Use VPC with private subnet and NAT Gateway
  2. Enable IMDSv2
  3. Apply security group rules with minimum required permissions
  4. Enable detailed monitoring
  5. Add proper resource tags

Consider adding these security configurations to the EC2 launch instructions.


70-73: Improve security of GPG key installation

The current GPG key installation pipes wget directly to sudo, which is not the most secure approach. Consider downloading the key first and then importing it.


96-98: Revise IAM permissions recommendation

Recommending AdministratorAccess policy is not aligned with the principle of least privilege. Instead, suggest creating custom IAM policies that grant only the necessary permissions for the specific infrastructure being managed.


112-124: Recommend using IAM roles instead of access keys

When working with Terraform on EC2 instances, it's more secure to use IAM roles instead of storing AWS credentials directly on the instance.


186-193: Add state file security warnings

The state file section should include critical security considerations:

  1. Warning about sensitive data in state files
  2. Recommendation for remote state storage (e.g., S3 with encryption)
  3. State file backup strategies
  4. Access control for state files
  5. State locking mechanisms

214-221: Enhance AMI data source example with additional filters

The current AMI data source example is missing important filters that are typically needed for proper AMI selection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant