Automate EC2 Instances with Templates

Automate EC2 Instances with Templates

ยท

3 min read

An EC2 instance is like a virtual computer in the cloud. It's where you can run your software and do tasks without needing a physical computer. Just like you use your laptop, an EC2 instance lets you use a computer that's online and ready to work whenever you need it.

Before you start

  1. AWS EC2 setup knowledge is required.

  2. An excellent Internet connection.

If you're unsure about setting up an AWS EC2 Instance, you can find guidance in my blog post. Simply click here to read more.

Launching Template

  1. Head to the AWS Management Console, and in the search bar, choose EC2.

  2. Following that, choose Launch Templates from the instances section and then click on the Create Launch template button.

  3. Provide a name and description for the template.

  4. Choose the operating system (OS) image that you're comfortable with. In my case, I'm selecting Ubuntu.

  5. Go for the t2.micro instance type to avoid bills.

  6. Choose a key that you'll use to SSH into the instance. If you don't have a key, create a new one and then select it.

  7. In the network settings, stick with the default VPC and subnet. Choose the option to Create a security group.

  8. Provide a name and description for the security group.

  9. Click the "Add security group rule" button and add two security groups as depicted below. This is crucial because we'll be automating the installation of Jenkins and NGINX using this template, and these security groups will enable us to access them.

  10. Choose "Advanced Details" and navigate to the last section called "User data."

  11. In "User data," enter a script that automates tasks on instance creation. This makes installations happen automatically when using this template.

  12. Paste the following script to automatically install Jenkins and nginx on your instance.

    #!/bin/bash
    
    #nginx-installation
    sudo apt-get update -y
    sudo apt install nginx -y
    sudo systemctl start nginx
    sudo systemctl enable nginx
    echo "This is automated by a Launch Template" > /var/www/html/index.html
    
    #jenkins-installation
    sudo apt-get update -y
    sudo apt install openjdk-17-jre -y
    sudo curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
      /usr/share/keyrings/jenkins-keyring.asc > /dev/null
    echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
      https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
      /etc/apt/sources.list.d/jenkins.list > /dev/null
    sudo apt-get update -y
    sudo apt-get install jenkins -y
    
  13. Finally, click the "Create launch template" button.

  14. Navigate to the instances section and initiate an instance launch using the template.

  15. Wait for the instance to initialize, and then check if your services are installed by accessing them.

It's evident that both Jenkins and nginx have been successfully installed and accessed.

Conclusion

With this template, you can automate the installation of Jenkins and Nginx. You'll be able to create multiple instances effortlessly with just a single click.

ย