You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Static web application deployment on container using Terraform and AWS EC2
The DevOps project involves deploying a static web application on a container using Terraform and AWS EC2. Terraform is used to provision and manage the AWS infrastructure, while AWS EC2 instances are used to host the containerized web application. The web application is built using static files, which are served by a web server running inside the container. By using containers, the application can be easily deployed and scaled across multiple instances. The use of Terraform enables the infrastructure to be defined as code, making it easier to manage and reproduce the environment. Overall, this project demonstrates how DevOps practices can be used to efficiently deploy and manage a static web application on AWS infrastructure.
Steps
1. Login to the root account and create IAM user account with administrative access
Log in to your AWS Management Console using the root account credentials.
Open the IAM console.
Click on the "Users" option in the left-hand navigation menu.
Click on the "Add user" button.
Enter a name for the user, and select "Programmatic access" and "AWS Management Console access" as the access types.
Set a password for the user or select "Autogenerated password".
Choose "Attach existing policies directly" in the "Set permissions" section.
Search for "AdministratorAccess" and select the policy.
Review the user details and click on the "Create user" button.
Note down the "Access key ID" and "Secret access key" for the user.
2. Create a key pair “devops_01.ppk” in IAM user account
3. Create access keys for IAM account add the access_key and secret_key in .tf file
Log in to your AWS Management Console using the IAM user account credentials with administrative access.
Open the EC2 console.
In the left-hand navigation menu, click on "Key Pairs".
Click on the "Create Key Pair" button.
Enter a name for your key pair in the "Key pair name" field.
Select the "ppk" format from the "Key pair file format" dropdown.
Click on the "Create Key Pair" button.
The key pair file will be downloaded automatically to your local machine. Make sure to store the key pair file in a secure location.
4. Create project directory
5. Create main.tf file
Open a text editor of your choice, such as Notepad, Sublime Text, or Visual Studio Code.
Create a new file and name it "main.tf".
Add the necessary code to configure the AWS resources you want to create using Terraform.
6. Open cmd prompt, go to project directory
7. Run terraform init, plan apply
Open a terminal or command prompt.
Navigate to the directory where the main.tf file is located using the cd command.
Run the terraform init command to initialize the Terraform working directory and download the necessary provider plugins. This command only needs to be run once for a particular configuration, or if the provider plugins have been updated. The output should show "Terraform has been successfully initialized!" if everything went well.
Run the terraform apply command to create the AWS resources defined in the main.tf file. Terraform will ask for confirmation before creating any resources. Type "yes" to proceed with the resource creation. The output will show the plan of what resources will be created, modified or deleted, and will ask for confirmation again. Type "yes" to confirm and create the resources.
Wait for the resources to be created. The output will show the status of the resources being created, and the time taken to create them. Once completed, you will see a message showing the creation of the resources and their status.
To destroy the resources, you can run the terraform destroy command.
8. Once the ec2 instance gets started, copy its public dns address
9. Open putty.exe to connect through ssh client
10. Using the dns address and keypair file connect to the instance
Open your preferred terminal application (e.g. PuTTY, Terminal, or Git Bash).
Navigate to the directory where the private key file (.pem or .ppk file) is stored.
Set the file permissions to 400 to ensure that only the owner can read the file. For example, on a Unix-based system, you can use the command chmod.
Use the ssh command to connect to the EC2 instance, specifying the DNS address and private key file.
11. Run the command ‘docker ps’ to check whether docker is running or not
Connect to your EC2 instance using the method described in the previous answer.
Once connected, run the command docker ps.
If Docker is running, you should see a list of active Docker containers along with their container ID, image name, status, and other details. If Docker is not running, you will see an error message indicating that the Docker daemon is not running.
12. Use docker pull nginx:latest
Connect to your EC2 instance using the method described in a previous answer.
Once connected, run the command docker pull nginx:latest.
Docker will download the latest version of the Nginx image from the Docker Hub registry. This may take a few minutes depending on your network speed.
Once the download is complete, you can run the Nginx container using the docker run command.
13. Create a new folder for html file
14. Run the following commands:
mkdir nginx-html
cd nginx-html
touch index.html
nano index.html
15. Write in the html file and save in the putty.
16. Run below command to run the nginx image on docker host on port 8000
Instance Type: The EC2 instance type should be selected based on the expected traffic and workload of the web application. The instance type should have enough CPU, memory, and disk space to handle the anticipated workload.