- Access the AWS Console:
- Go to the AWS Console and log in to your account.
- Navigate to the Security Credentials Page:
- In the upper-right corner, click on your account name or the "Services" dropdown and select "My Security Credentials."
- Access User Security Credentials:
- On the left navigation bar, click on "Users."
- Select the user for which you want to create credentials.
- Go to the "Security credentials" tab.
- Create or View Access Credentials:
- Under "Access keys," you can create a new key or view an existing one.
- If creating a new one, click on "Create access key."
- Take Note of the Access Credentials:
- After creation or viewing, take note of the "Access key ID" and "Secret access key." These details are needed to configure the AWS CLI.
- Open Terminal or Command Prompt:
- Open the terminal on your operating system.
- Run the aws configure Command:
- Execute the following command and input the information when prompted:
aws configureYou will be prompted to enter the access key, secret access key, default region, and output format.
Estimated time to complete: 20 minutes
- CloudShell
- Terraform
- Ansible
- Download Terraform binary for Linux (64-bit):
curl -LO https://releases.hashicorp.com/terraform/1.0.8/terraform_1.0.8_linux_amd64.zip- Unzip the downloaded Terraform binary:
unzip terraform_1.0.8_linux_amd64.zip- Create the directory where you'll store the Terraform binary:
mkdir -p ~/.local/bin- Move the Terraform binary to the newly created directory:
mv terraform ~/.local/bin/- Add Terraform to your PATH by appending it to the .bashrc file:
echo 'export PATH=$HOME/.local/bin:$PATH' >> ~/.bashrc- Source the .bashrc file to apply the changes immediately:
source ~/.bashrc- Verify the Terraform installation by checking the version:
terraform --version- The above commands will install the required packages and then install Ansible using pip.
- To install Ansible using pip, run the following commands:
sudo apt-get install -y python3-pip
sudo pip3 install ansible- Once installed, you can verify the Ansible installation by running:
ansible --version- Run the following AWS CLI command to create a new key pair and save the private key to a local file named kp_devops_fest.pem:
aws ec2 create-key-pair --key-name kp_devops_fest --query 'KeyMaterial' --output text > kp_devops_fest.pem
chmod 400 kp_devops_fest.pem- Create the folder structure for the project
mkdir mdc-terraformEstimated time to complete: 20 minutes
Work directory: 01-mdc-terraform
- Create a folder named "01-mdc-terraform" and navigate into it:
mkdir 01-mdc-terraform
cd 01-mdc-terraform
vi main.tf- Create a file named "main.tf" inside the "01-mdc-terraform" folder.
- In the "main.tf" file, add the following code to configure the Microsoft Azure provider and create a S3:
provider "aws" {
region = "us-east-1"
}
provider "random" {}
resource "random_id" "bucket_id" {
byte_length = 4
}
resource "aws_s3_bucket" "example_bucket" {
bucket = "mentoriadevops-${random_id.bucket_id.hex}"
acl = "private"
tags = {
Name = "mentoria-devops"
Environment = "Production"
}
}
- Initialize the Terraform project directory to download the required plugins:
terraform init- Preview the execution plan to see the changes that will be applied to the infrastructure:
terraform plan- Apply the changes and create the resource group:
terraform apply- To clean up and destroy all resources created by Terraform, run:
terraform destroyEstimated time to complete: 40 minutes
Work directory: 02-mdc-terraform
- Create a folder named "02-mdc-terraform" and navigate into it:
mkdir 02-mdc-terraform
cd 02-mdc-terraform
touch main.tf- Create a file named "main.tf" inside the "02-mdc-terraform" folder.
- In the "main.tf" file, add the following code to configure the AWS provider and create a EC2:
provider "aws" {
region = var.region
}
resource "aws_instance" "control_instance" {
ami = var.ami_id
instance_type = "t2.micro"
key_name = "kp_devops_fest"
tags = {
Name = "control-ec2-mdc"
}
vpc_security_group_ids = [
aws_security_group.allow_ssh_control.id,
aws_security_group.allow_http_control.id
]
}
resource "aws_instance" "worker_instance" {
ami = var.ami_id
instance_type = "t2.micro"
key_name = "kp_devops_fest"
tags = {
Name = "worker-ec2-mdc"
}
vpc_security_group_ids = [
aws_security_group.allow_ssh_worker.id,
aws_security_group.allow_http_worker.id
]
}
resource "aws_security_group" "allow_ssh_control" {
name = "allow_ssh_control"
description = "Allow inbound SSH traffic for control instance"
vpc_id = var.vpc_id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "allow_http_control" {
name = "allow_http_control"
description = "Allow inbound HTTP traffic for control instance"
vpc_id = var.vpc_id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "allow_ssh_worker" {
name = "allow_ssh_worker"
description = "Allow inbound SSH traffic for worker instance"
vpc_id = var.vpc_id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "allow_http_worker" {
name = "allow_http_worker"
description = "Allow inbound HTTP traffic for worker instance"
vpc_id = var.vpc_id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
- Crete the var.tf file:
variable "region" {
default = "us-east-1"
}
variable "ami_id" {
default = "ami-0e731c8a588258d0d"
}
variable "vpc_id" {
default = "vpc-f1639e8c"
}- Open/Create the "outputs.tf"
vi output.tf- Add the desired outputs, such as the instance's public IP.
output "control_instance_public_ip" {
value = aws_instance.control_instance.public_ip
}
output "worker_instance_public_ip" {
value = aws_instance.worker_instance.public_ip
}- Initialize the Terraform project directory to download the required plugins:
terraform init- Preview the execution plan to see the changes that will be applied to the infrastructure:
terraform plan- Apply the changes and create the resource group:
terraform apply- AWS Console:
- Go to the AWS Management Console.
- Navigate to the "EC2" service.
- EC2 Dashboard:
- In the EC2 Dashboard, click on "Instances" in the left navigation pane.
- Locate Worker Instance:
- Find your worker EC2 instance in the list of instances.
- Retrieve Public DNS:
- Click on the row corresponding to your worker instance to select it.
- At the bottom of the page, you should see details about your instance. Look for the "Public DNS (IPv4)" field. Copy this value.
ec2-3-91-150-100.compute-1.amazonaws.com # Exemple WOKER an- SSH Connection:
- Open a terminal on your local machine.
- Use the following SSH copy control EC2 instance.
# Copy the key pair to the worker EC2 instance
scp -i "/Users/iesodias/Documents/Projetos/workshop-bootcamp/mdc-terraform/kp_devops_fest.pem" "/Users/iesodias/Documents/Projetos/workshop-bootcamp/mdc-terraform/kp_devops_fest.pem" ec2-user@ec2-54-211-206-27.compute-1.amazonaws.com:~Estimated time to complete: 30 minutes
Work directory: 02-mdc-terraform
- Connect to the Control Instance:
# SSH into the worker EC2 instance
ssh -i "/Users/iesodias/Documents/Projetos/workshop-bootcamp/mdc-terraform/kp_devops_fest.pem" ec2-user@ec2-54-211-206-27.compute-1.amazonaws.com- Install Ansible:
- Install Ansible on your CONTROL EC2 to enable remote management of other instances. Run the following commands:
sudo yum install -y python3-pip
sudo pip3 install ansible- Copy Worker Machine's IP:
- Save the Worker EC2 instance's IP address into an Ansible inventory file. This file is crucial for Ansible to know which machines to manage. Run this command:
echo -e "mdc-target1 ansible_host=34.227.16.33 ansible_user=ec2-user ansible_ssh_private_key_file=/home/ec2-user/kp_devops_fest.pem" > inventory.txt- Test Connection to Worker Machine:
- Ensure Ansible can communicate with the Worker EC2 instance:
ansible mdc-target1 -i inventory.txt -m ping- Create the YAML Playbook (playbook.yaml):
- Craft an Ansible playbook to install Nginx, start its service, and create a simple webpage:
- hosts: mdc-target1
become: yes
tasks:
- name: Instalar o Nginx
yum:
name: nginx
state: present
- name: Iniciar o serviço Nginx
service:
name: nginx
state: started
- name: Habilitar o serviço Nginx para iniciar na inicialização
service:
name: nginx
enabled: yes
- name: Criar arquivo index.html
copy:
content: "Bem Vindos a mentoria devops cloud"
dest: /usr/share/nginx/html/index.html- Run the Playbook:
- Execute the playbook to apply the configurations on the Worker EC2:
ansible-playbook -i inventory.txt playbook.yaml- Verify the Application on Port 80:
- Open your web browser and enter your Worker EC2 instance's IP address followed by ":80" (e.g., http://YOUR_IP:80). This should display the welcome message.
- Make sure your Worker EC2 instance is reachable on port 80 and adjust the playbook as needed for your specific application requirements.