🚀Unlocking Efficiency: AWS EC2 Deployment with GitLab

>>> A complete guide to Launching an AWS EC2 instance and deploying a simple application using GitLab pipelines

Sai Manasa
5 min readMay 22, 2024

Hello World… Welcome to the ultimate hands-on guide for launching an AWS EC2 instance and deploying a simple application using GitLab pipelines, starting from scratch! So, let’s get started…

Overview:

  • What is GitLab?
  • What AWS EC2 Server is?
  • What is a User Data script?
  • Simple hands-on guide: using the PEM key
  • Simple hands-on guide: using User Data script
  • Architectural Diagram
  • Source Code

What is GitLab?

  • GitLab is a comprehensive DevOps platform that provides an integrated solution for managing the entire software development lifecycle.
  • From version control with Git repositories to robust CI/CD pipelines, issue tracking, project management, and security features, GitLab offers a single application for teams to collaborate, automate, and deliver software efficiently.
  • With its scalability, flexibility, and vibrant community, GitLab empowers organizations to streamline their development processes, ensure code quality, and accelerate innovation.

What AWS EC2 Server is?

  • EC2, Amazon Elastic Compute Cloud, is a web service that is provided by Amazon Web Services (AWS) that allows users to rent virtual servers, also known as instances, in the cloud.
  • These instances provide scalable computing capacity and enable users to run applications and workloads without investing in and managing physical hardware.

What is a User Data script?

  • User data script refers to a script or a set of commands we provide when launching an instance in a cloud computing environment, such as AWS EC2.
  • The script is executed automatically when the instance starts up and allows to perform various initialization tasks or configurations on the instance.

Simple hands-on guide: using the PEM key

Task Overview: The objective is to use a GitLab pipeline to automate launching an EC2 instance and deploying a simple web app onto the server, utilizing a PEM key for secure access.

Step 1: Store the AWS access keys.

  • The AWS access keys must be stored as ‘Pipeline Variables’ or ‘Environment Variables’ for securely managing and accessing AWS resources during pipeline execution.
  • Below snap is the process of how to store them:

Step 2: Store the PEM key.

  • A PEM (Privacy Enhanced Mail) key, often referred to as a PEM file or PEM certificate, is a file format used to store cryptographic keys and certificates.
  • In the context of AWS EC2 instances, a PEM key is typically used for securely accessing the instance over SSH (Secure Shell) protocol.
  • Below snap is the process of how to store it:

Step 3: Write a pipeline definition in YAML.

  • The above pipeline definition launches an EC2 instance in AWS, logs in to the server using the PEM key, and then the app is deployed into the server.

Step 4: Create a simple web app.

Step 5: Create a pipeline and run.

  • Below snap is the process of how to create a pipeline and run it:
  • So, here we need to change the configuration file then whenever this file is changed then the pipeline gets triggered. The configuration file can be changed in the flow:

Step 6: Results.

  • Upon the successful completion of the pipeline.
  • Check the status EC2 server and access the web app.
  • Steps to check the EC2 server:
  • Steps to access the web app:
  • And it’s done successfully using PEM key… 🙌

Simple hands-on guide: using User Data script

Task Overview: The objective is to use GitLab pipeline to automate launching an EC2 instance and deploying a simple web app onto the server, using a User Data script.

Step 1: Store the AWS access keys.

  • The AWS access keys must be stored as ‘Pipeline Variables’ or ‘Environment Variables’ for securely managing and accessing AWS resources during pipeline execution.
  • These variables can be used at various scopes within GitLab Pipelines.
  • Below snap is the process of how to store them:

Step 2: Write a User Data script.

  • The script above installs, starts, and enables the Apache service, while also writing the application code to the server.

Step 3: Write a pipeline definition in YAML.

  • The provided pipeline definition launches an EC2 instance in AWS, then accesses the server and deploys the application using a user data script.

Step 4: Create a pipeline and run.

  • Below snap is the process of how to create a pipeline and run it:
  • So, here we need to change the configuration file then whenever this file is changed then the pipeline gets triggered. The configuration file can be changed in the flow:

Step 5: Results.

  • Upon the successful completion of the pipeline.
  • Check the status EC2 server and access the web app.
  • Steps to check the EC2 server:
  • Steps to access the web app:
  • And it’s done successfully using User Data script… 🙌

Architectural Diagram:

Source Code:

Step into my GitHub Repo, where I’ve compiled a comprehensive collection of source code.

Let’s Connect:

Feel free to reach out, share your thoughts, or ask any questions. I’m excited to engage with you and learn from each other as we navigate this exciting field!

LinkedIn: Sai Manasa

GitHub: Sai Manasa

Happy GitLab-ing! Have Delightful Deployments🚀

Happy Learning 💻

--

--

Sai Manasa
Sai Manasa

No responses yet