# Course End Project 1 [toc] ### Project Details #### Project Title :::info **Automating Infrastructure using Terraform** ::: #### Problem Statement :::warning Nowadays, infrastructure automation is critical. We tend to put the most emphasis on software development processes, but infrastructure deployment strategy is just as important. Infrastructure automation not only aids disaster recovery, but it also facilitates testing and development. Your organization is adopting the DevOps methodology and in order to automate provisioning of infrastructure there's a need to setup a centralised server for Jenkins. Terraform is a tool that allows you to provision various infrastructure components. Ansible is a platform for managing configurations and deploying applications. It means you'll use Terraform to build a virtual machine, for example, and then use Ansible to instal the necessary applications on that machine. Considering the Organizational requirement you are asked to automate the infrastructure using Terraform first and install other required automation tools in it. ::: #### Tools required :::warning - Terraform (installed on a controller machine) :bulb: (Hint: You can configure your laptop as the controller) - AWS account with security credentials (Either provided by Simplilearn or your personal AWS account) - AWS Keypair (to ssh into EC2 instance) ::: #### Expected Deliverables / Acceptance criteria :::warning - Launch an EC2 instance using Terraform - Connect to the instance - Install Jenkins, Java and Python in the instance - Validate that the packages are installed ::: ### Proposed Solutions steps #### Step 1: Configure terraform controller :::warning - Make sure you have a machine on which you have terraform installed and configured. :arrow_right: Refer [here](https://hackmd.io/U0V-L9qbS3Oc0fphmQSHKQ#Installation-steps-tested-on-Ubuntu-2004) for installation steps - Terraform can be installed on any of the following machines: - Simplilearn Lab instance - Your laptop (Windows/MacOS/Linux) - Instance on any other platform such as **Digital Ocean** or **AWS**. ::: #### Step 2: Get AWS "access key" and "secret access key" :::warning You would need access key and secret access key in order to authenticate Terraform to AWS. We have discussed it in our terraform session. Please refer to the following sections based on the type of AWS account you are using: 1. [AWS account provided by Simplilearn via LMS Lab page](https://hackmd.io/QwCK6RTsTGK__4bJQyxWRA#Get-AWS-Access-key-id-and-Secret-access-key-via-Simplilearn-LMS) 2. [Your personal AWS account](https://hackmd.io/QwCK6RTsTGK__4bJQyxWRA#Create-AWS-access_key-and-secret_access_key-via-AWS-Console) ::: #### Step 3: Create an AWS Key Pair :::warning AWS supports asymmetric key pair based authentication for SSH access to linux based EC2 instances. Before you can login to a target EC2 instance using SSH, you need to have a asymmetric key pair. Refer to the following AWS official documentation link to create a new key pair. - [Create a key pair using Amazon EC2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/create-key-pairs.html#having-ec2-create-your-key-pair) ::: #### Step 4: Write Terraform file :::warning - Write a terraform policy file to create EC2 instance ::: ##### Example 1: Without User data ````tf= provider "aws" { access_key = "your-key" secret_key = "your-secret-key" token = "your-token" region = "us-west-2" # Set the appropriate region here } resource "aws_instance" "example" { ami = "your-ami-id-here" instance_type = "t2.micro" # put instance type tags = { Name = "my-web-instance" } } ```` ##### Example 2: With User data ````tf= provider "aws" { access_key = "your-key" secret_key = "your-secret-key" token = "your-token" region = "us-west-2" # Set the appropriate region here } resource "aws_instance" "example" { ami = "your-ami-id-here" instance_type = "t2.micro" # put instance type user_data = <<-EOF #!/bin/bash ## your script goes here! EOF tags = { Name = "my-web-instance" } } ```` :arrow_right: *In the user_data section, you can provide a Bash script or any other configuration management tool script that will be executed when the EC2 instance is launched. This section allows you to automate the setup and configuration of the instance. For project purposes you need to write a script to install Java, Jenkins and Python.* :arrow_right: *Remember to replace the region, ami, instance_type, and tags values with your desired configurations. After making the changes, run the Terraform commands (terraform init, terraform plan, and terraform apply) as described below to apply the changes and create the EC2 instance with the specified user data.* #### Step 5: Run appropriate terraform commands Navigate to the directory where you've placed the terraform (.tf) configuration file and run the following commands as needed: ````yaml= Initialize Terraform: terraform init Preview the changes Terraform will make: terraform plan Apply the changes to create the EC2 instance: terraform apply ```` #### Step 6: Connect to the EC2 instance Once the instance has been created, you can connect to the instance using SSH via the following steps: :::warning 1. Obtain the public IP or public DNS name of your EC2 instance. You can find this information in the AWS Management Console by navigating to the EC2 service and selecting your instance. 2. Open a terminal or command prompt on your local machine. 3. Change the permissions of your private key file (.pem file) to be readable only by you. For example: ```` chmod 400 /path/to/your/key.pem ```` 4. Connect to the instance using the SSH command. The command should include the following elements: - "-i" flag to specify the path to your private key file - "username" depending on the operating system of the EC2 instance: - For Amazon Linux 2 or Amazon Linux: ec2-user - For Ubuntu: ubuntu - For CentOS: centos - For Debian: admin - For other distributions, consult the AWS documentation or the AMI provider's documentation. - public-ip or public-dns as obtained in step 1. For example: ```` ssh -i /path/to/your/key.pem username@public-ip-or-dns ```` 5. If you receive a prompt asking whether you want to continue connecting, type yes. ::: :bulb: *You should now be connected to your EC2 instance via SSH. Now it's time to validate our installation and user data commands.* :gem: *Note: If you're using a Windows system, you can utilize an SSH client like PuTTY or the Windows Subsystem for Linux (WSL) to connect to your EC2 instance. The process may differ slightly, but the general concepts remain the same.* #### Step 7: Validation :::warning You can run the following commands to check if the desired tools/softwares have been installed on the EC2 instance: ````yaml java: which java or java -version Jenkins: sudo systemctl status jenkins Python: which python or which python3 ::: :arrow_right: *Important note: Above docomentation should be helpful to complete your project. Make sure to provide additional screenshots, code snippets and any other relevant materials to your document and submit your project before the deadline.* ***All the best!***