forked from stacksimplify/terraform-on-aws-ec2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9ad926
commit 2385dd5
Showing
685 changed files
with
31,335 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/ADDITIONAL-TOPICS | ||
FUTURE-TOPICS | ||
#.gitignore | ||
|
||
# macOS Files | ||
.DS_Store | ||
|
||
# Ignore .sh files | ||
#*.sh | ||
|
||
# Terraform Files and Folders | ||
.terraform | ||
*.tfstate | ||
*.tfstate.backup | ||
#.terraform.lock.hcl | ||
#*.tfvars | ||
|
||
# Ignore .pem files | ||
#*.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Infrastructure as Code Basics | ||
|
||
## Step-01: Understand Problems with Traditional way of Managing Infrastructure | ||
- Time it takes for building multiple environments | ||
- Issues we face with different environments | ||
- Scale-Up and Scale-Down On-Demand | ||
|
||
## Step-02: Discuss how IaC with Terraform Solves them | ||
- Visibility | ||
- Stability | ||
- Scalability | ||
- Security | ||
- Audit |
92 changes: 92 additions & 0 deletions
92
02-Terraform-Basics/02-01-Install-Tools-TerraformCLI-AWSCLI-VSCodeIDE/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Terraform & AWS CLI Installation | ||
|
||
## Step-01: Introduction | ||
- Install Terraform CLI | ||
- Install AWS CLI | ||
- Install VS Code Editor | ||
- Install HashiCorp Terraform plugin for VS Code | ||
|
||
|
||
## Step-02: MACOS: Terraform Install | ||
- [Download Terraform MAC](https://www.terraform.io/downloads.html) | ||
- [Install CLI](https://learn.hashicorp.com/tutorials/terraform/install-cli) | ||
- Unzip the package | ||
``` | ||
# Copy binary zip file to a folder | ||
mkdir /Users/<YOUR-USER>/Documents/terraform-install | ||
COPY Package to "terraform-install" folder | ||
# Unzip | ||
unzip <PACKAGE-NAME> | ||
unzip terraform_0.14.3_darwin_amd64.zip | ||
# Copy terraform binary to /usr/local/bin | ||
echo $PATH | ||
mv terraform /usr/local/bin | ||
# Verify Version | ||
terraform version | ||
# To Uninstall Terraform (NOT REQUIRED) | ||
rm -rf /usr/local/bin/terraform | ||
``` | ||
|
||
## Step-03: MACOS: IDE for Terraform - VS Code Editor | ||
- [Microsoft Visual Studio Code Editor](https://code.visualstudio.com/download) | ||
- [Hashicorp Terraform Plugin for VS Code](https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform) | ||
|
||
|
||
### Step-04: MACOS: Install AWS CLI | ||
- [AWS CLI Install](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) | ||
- [Install AWS CLI - MAC](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-mac.html#cliv2-mac-install-cmd) | ||
|
||
``` | ||
# Install AWS CLI V2 | ||
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg" | ||
sudo installer -pkg AWSCLIV2.pkg -target / | ||
which aws | ||
aws --version | ||
# Uninstall AWS CLI V2 (NOT REQUIRED) | ||
which aws | ||
ls -l /usr/local/bin/aws | ||
sudo rm /usr/local/bin/aws | ||
sudo rm /usr/local/bin/aws_completer | ||
sudo rm -rf /usr/local/aws-cli | ||
``` | ||
|
||
|
||
## Step-05: MACOS: Configure AWS Credentials | ||
- **Pre-requisite:** Should have AWS Account. | ||
- [Create an AWS Account](https://portal.aws.amazon.com/billing/signup?nc2=h_ct&src=header_signup&redirect_url=https%3A%2F%2Faws.amazon.com%2Fregistration-confirmation#/start) | ||
- Generate Security Credentials using AWS Management Console | ||
- Go to Services -> IAM -> Users -> "Your-Admin-User" -> Security Credentials -> Create Access Key | ||
- Configure AWS credentials using SSH Terminal on your local desktop | ||
``` | ||
# Configure AWS Credentials in command line | ||
$ aws configure | ||
AWS Access Key ID [None]: AKIASUF7DEFKSIAWMZ7K | ||
AWS Secret Access Key [None]: WL9G9Tl8lGm7w9t7B3NEDny1+w3N/K5F3HWtdFH/ | ||
Default region name [None]: us-east-1 | ||
Default output format [None]: json | ||
# Verify if we are able list S3 buckets | ||
aws s3 ls | ||
``` | ||
- Verify the AWS Credentials Profile | ||
``` | ||
cat $HOME/.aws/credentials | ||
``` | ||
|
||
## Step-06: WindowsOS: Terraform & AWS CLI Install | ||
- [Download Terraform](https://www.terraform.io/downloads.html) | ||
- [Install CLI](https://learn.hashicorp.com/tutorials/terraform/install-cli) | ||
- Unzip the package | ||
- Create new folder `terraform-bins` | ||
- Copy the `terraform.exe` to a `terraform-bins` | ||
- Set PATH in windows | ||
- Install [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) | ||
|
||
## Step-07: LinuxOS: Terraform & AWS CLI Install | ||
- [Download Terraform](https://www.terraform.io/downloads.html) | ||
- [Linux OS - Terraform Install](https://learn.hashicorp.com/tutorials/terraform/install-cli) |
80 changes: 80 additions & 0 deletions
80
02-Terraform-Basics/02-02-Terraform-Command-Basics/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Terraform Command Basics | ||
|
||
## Step-01: Introduction | ||
- Understand basic Terraform Commands | ||
- terraform init | ||
- terraform validate | ||
- terraform plan | ||
- terraform apply | ||
- terraform destroy | ||
|
||
## Step-02: Review terraform manifest for EC2 Instance | ||
- **Pre-Conditions-1:** Ensure you have **default-vpc** in that respective region | ||
- **Pre-Conditions-2:** Ensure AMI you are provisioning exists in that region if not update AMI ID | ||
- **Pre-Conditions-3:** Verify your AWS Credentials in **$HOME/.aws/credentials** | ||
```t | ||
# Terraform Settings Block | ||
terraform { | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
#version = "~> 3.21" # Optional but recommended in production | ||
} | ||
} | ||
} | ||
|
||
# Provider Block | ||
provider "aws" { | ||
profile = "default" # AWS Credentials Profile configured on your local desktop terminal $HOME/.aws/credentials | ||
region = "us-east-1" | ||
} | ||
|
||
# Resource Block | ||
resource "aws_instance" "ec2demo" { | ||
ami = "ami-04d29b6f966df1537" # Amazon Linux in us-east-1, update as per your region | ||
instance_type = "t2.micro" | ||
} | ||
``` | ||
|
||
## Step-03: Terraform Core Commands | ||
```t | ||
# Initialize Terraform | ||
terraform init | ||
|
||
# Terraform Validate | ||
terraform validate | ||
|
||
# Terraform Plan to Verify what it is going to create / update / destroy | ||
terraform plan | ||
|
||
# Terraform Apply to Create EC2 Instance | ||
terraform apply | ||
``` | ||
|
||
## Step-04: Verify the EC2 Instance in AWS Management Console | ||
- Go to AWS Management Console -> Services -> EC2 | ||
- Verify newly created EC2 instance | ||
|
||
|
||
|
||
## Step-05: Destroy Infrastructure | ||
```t | ||
# Destroy EC2 Instance | ||
terraform destroy | ||
|
||
# Delete Terraform files | ||
rm -rf .terraform* | ||
rm -rf terraform.tfstate* | ||
``` | ||
|
||
## Step-08: Conclusion | ||
- Re-iterate what we have learned in this section | ||
- Learned about Important Terraform Commands | ||
- terraform init | ||
- terraform validate | ||
- terraform plan | ||
- terraform apply | ||
- terraform destroy | ||
|
||
|
||
|
21 changes: 21 additions & 0 deletions
21
02-Terraform-Basics/02-02-Terraform-Command-Basics/terraform-manifests/ec2-instance.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Terraform Settings Block | ||
terraform { | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
#version = "~> 3.21" # Optional but recommended in production | ||
} | ||
} | ||
} | ||
|
||
# Provider Block | ||
provider "aws" { | ||
profile = "default" # AWS Credentials Profile configured on your local desktop terminal $HOME/.aws/credentials | ||
region = "us-east-1" | ||
} | ||
|
||
# Resource Block | ||
resource "aws_instance" "ec2demo" { | ||
ami = "ami-0533f2ba8a1995cf9" # Amazon Linux in us-east-1, update as per your region | ||
instance_type = "t2.micro" | ||
} |
53 changes: 53 additions & 0 deletions
53
02-Terraform-Basics/02-03-Terraform-Language-Syntax/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Terraform Configuration Language Syntax | ||
|
||
## Step-01: Introduction | ||
- Understand Terraform Language Basics | ||
- Understand Blocks | ||
- Understand Arguments, Attributes & Meta-Arguments | ||
- Understand Identifiers | ||
- Understand Comments | ||
|
||
|
||
|
||
## Step-02: Terraform Configuration Language Syntax | ||
- Understand Blocks | ||
- Understand Arguments | ||
- Understand Identifiers | ||
- Understand Comments | ||
- [Terraform Configuration](https://www.terraform.io/docs/configuration/index.html) | ||
- [Terraform Configuration Syntax](https://www.terraform.io/docs/configuration/syntax.html) | ||
```t | ||
# Template | ||
<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" { | ||
# Block body | ||
<IDENTIFIER> = <EXPRESSION> # Argument | ||
} | ||
|
||
# AWS Example | ||
resource "aws_instance" "ec2demo" { # BLOCK | ||
ami = "ami-04d29b6f966df1537" # Argument | ||
instance_type = var.instance_type # Argument with value as expression (Variable value replaced from varibales.tf | ||
} | ||
``` | ||
|
||
## Step-03: Understand about Arguments, Attributes and Meta-Arguments. | ||
- Arguments can be `required` or `optional` | ||
- Attribues format looks like `resource_type.resource_name.attribute_name` | ||
- Meta-Arguments change a resource type's behavior (Example: count, for_each) | ||
- [Additional Reference](https://learn.hashicorp.com/tutorials/terraform/resource?in=terraform/configuration-language) | ||
- [Resource: AWS Instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | ||
- [Resource: AWS Instance Argument Reference](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#argument-reference) | ||
- [Resource: AWS Instance Attribute Reference](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#attributes-reference) | ||
- [Resource: Meta-Arguments](https://www.terraform.io/docs/language/meta-arguments/depends_on.html) | ||
|
||
## Step-04: Understand about Terraform Top-Level Blocks | ||
- Discuss about Terraform Top-Level blocks | ||
- Terraform Settings Block | ||
- Provider Block | ||
- Resource Block | ||
- Input Variables Block | ||
- Output Values Block | ||
- Local Values Block | ||
- Data Sources Block | ||
- Modules Block | ||
|
104 changes: 104 additions & 0 deletions
104
...rm-Basics/02-03-Terraform-Language-Syntax/terraform-manifests/top-level-blocks-samples.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
##################################################################### | ||
# Block-1: Terraform Settings Block | ||
terraform { | ||
required_version = "~> 0.14" | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "~> 3.0" | ||
} | ||
} | ||
# Adding Backend as S3 for Remote State Storage with State Locking | ||
backend "s3" { | ||
bucket = "terraform-stacksimplify" | ||
key = "dev2/terraform.tfstate" | ||
region = "us-east-1" | ||
|
||
# For State Locking | ||
dynamodb_table = "terraform-dev-state-table" | ||
} | ||
} | ||
##################################################################### | ||
# Block-2: Provider Block | ||
provider "aws" { | ||
profile = "default" # AWS Credentials Profile configured on your local desktop terminal $HOME/.aws/credentials | ||
region = "us-east-1" | ||
} | ||
##################################################################### | ||
# Block-3: Resource Block | ||
resource "aws_instance" "ec2demo" { | ||
ami = "ami-04d29b6f966df1537" # Amazon Linux | ||
instance_type = var.instance_type | ||
} | ||
##################################################################### | ||
# Block-4: Input Variables Block | ||
variable "instance_type" { | ||
default = "t2.micro" | ||
description = "EC2 Instance Type" | ||
type = string | ||
} | ||
##################################################################### | ||
# Block-5: Output Values Block | ||
output "ec2_instance_publicip" { | ||
description = "EC2 Instance Public IP" | ||
value = aws_instance.my-ec2-vm.public_ip | ||
} | ||
##################################################################### | ||
# Block-6: Local Values Block | ||
# Create S3 Bucket - with Input Variables & Local Values | ||
locals { | ||
bucket-name-prefix = "${var.app_name}-${var.environment_name}" | ||
} | ||
##################################################################### | ||
# Block-7: Data sources Block | ||
# Get latest AMI ID for Amazon Linux2 OS | ||
data "aws_ami" "amzlinux" { | ||
most_recent = true | ||
owners = ["amazon"] | ||
|
||
filter { | ||
name = "name" | ||
values = ["amzn2-ami-hvm-*"] | ||
} | ||
|
||
filter { | ||
name = "root-device-type" | ||
values = ["ebs"] | ||
} | ||
|
||
filter { | ||
name = "virtualization-type" | ||
values = ["hvm"] | ||
} | ||
|
||
filter { | ||
name = "architecture" | ||
values = ["x86_64"] | ||
} | ||
|
||
} | ||
##################################################################### | ||
# Block-8: Modules Block | ||
# AWS EC2 Instance Module | ||
|
||
module "ec2_cluster" { | ||
source = "terraform-aws-modules/ec2-instance/aws" | ||
version = "~> 2.0" | ||
|
||
name = "my-modules-demo" | ||
instance_count = 2 | ||
|
||
ami = data.aws_ami.amzlinux.id | ||
instance_type = "t2.micro" | ||
key_name = "terraform-key" | ||
monitoring = true | ||
vpc_security_group_ids = ["sg-08b25c5a5bf489ffa"] # Get Default VPC Security Group ID and replace | ||
subnet_id = "subnet-4ee95470" # Get one public subnet id from default vpc and replace | ||
user_data = file("apache-install.sh") | ||
|
||
tags = { | ||
Terraform = "true" | ||
Environment = "dev" | ||
} | ||
} | ||
##################################################################### |
Oops, something went wrong.