🚀Unlocking Efficiency: AWS EC2 Deployment with Jenkins

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

Sai Manasa
8 min readApr 21, 2024

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

Overview:

  • What is Jenkins?
  • What is Jenkinsfile?
  • What AWS EC2 Server is?
  • What is a User Data script?
  • What is Dockerfile?
  • Creating the Dockerfiles
  • Building, Tagging, and Pushing an image
  • Deploying a container
  • Setup of Jenkins
  • Install Plugins
  • Configuring AWS Access Keys
  • Configuring the PEM/Secret Key
  • Simple hands-on guide: using the PEM key
  • Simple hands-on guide: using User Data script
  • Architectural Diagram
  • Tips
  • Source Code

What is Jenkins?

  • Jenkins is an open-source automation server that is used for building, testing, and deploying software.
  • It is a widely used continuous integration and continuous delivery (CI/CD) tool that automates the process of integrating code changes from multiple contributors, running automated tests, and then delivering the changes to a production environment.
  • A Jenkins Pipeline is a collection of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins.
  • A Jenkins Pipeline is a powerful set of tools for modeling simple and complex delivery pipelines ‘as code’

What is Jenkinsfile?

  • The definition of a pipeline is typically written into a text file called a Jenkinsfile, which can be checked into a project’s source control repository.
  • Supports two types of syntaxes: Declarative Pipeline and Scripted Pipeline.

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.

What is Dockerfile?

  • A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
  • It’s a set of instructions used to create a Docker image.

Creating the Dockerfiles:

  • The below Dockerfile uses Jenkins as a base image, installs the required tools, and copies the index.html from local to the container.
  • Used this Dockerfile for the PEM key case.
  • The below Dockerfile uses Jenkins as a base image, installs the required tools, and copies the user data script from local to the container.
  • Used this Dockerfile for the User Data script case.

Building, Tagging, and Pushing an Image:

  • Command to build an image using Dockerfile:
docker build -t jenkins -f <Dockerfile_name> .
  • Command to create/update the tag of an image:
docker tag <image_name>:<old_associated_tag> <new_image_name>:<new_tag>
  • Command to push an image to a registry like Docker Hub:
docker push <registry_URL>/<repository_name>:<tag>
  • Before pushing an image into the registry, login to the appropriate registry using the command:
docker login <registry_URL>

Deploying a container:

  • Command to create/run a container from the above-created image:
docker run -dt --name <container_name> -p 8080:8080 -p 50000:50000 -u root <image-name>:<tag>
  • The container above is running with the specified name, operating on ports 8080 (for the master) and 50000 (for the slave).
  • It is configured to run as a root user using the desired image.

Setup of Jenkins:

  • We can get the initial admin password from the logs of the container (at the end) Command to get the logs of a container:
docker logs <container_name/id>
  • Next, install the suggested plugins:
  • Next, you can either create a first admin user or you can skip and continue as admin:
  • Next, we can configure the Jenkins URL here:
  • Save and Finish.
  • Start using Jenkins.

Install Plugins:

  • Select the Manage Jenkins option from the dashboard.
  • Then select the Plugins option from the System Configuration category.
  • Available Plugins → Select AWS Credentials and SSH Agent plugins → Install

Configuring AWS Access Keys:

  • Once the above plugins are installed, then configure the AWS access keys in Jenkins Credentials.
  • Select the Manage Jenkins option from the dashboard.
  • Then select the Credentials option from the Security category.
  • Credentials → System → Global Credentials (unrestricted) → Add Credentials → Kind (AWS Credentials) → Scope (Global) → ID (any desired ID) → Access Key ID (AWS access key ID) → Secret Access Key (AWS secret access key) → Create
  • Done with configuring the AWS credentials.

Configuring the PEM Key:

  • Now, need to store the PEM key in Jenkins.
  • Select the Manage Jenkins option from the dashboard.
  • Then select the Credentials option from the Security category.
  • Credentials → System → Global Credentials (unrestricted) → Add Credentials → Kind (SSH Username with private key) → Scope (Global) → ID (any desired ID) → Username (any desired username) → Enter directly (Private Key) → Add → Enter the secret (i.e., private key) → Passphrase (any desired password) → Create
  • Done with configuring the PEM key.

Simple hands-on guide: using the PEM key

Task Overview: The objective is to use a Jenkins 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: Setup Jenkins

  • Launching Jenkins.
  • Install required plugins.
  • Storing AWS credentials and PEM key.
  • All these steps are mentioned above.

Step 2: Write a pipeline definition in Jenkinsfile.

  • 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 3: Create a simple web app.

Step 4: Create a pipeline and build.

  • Below snap is the process of how to create a pipeline and run it:
Flow: How to create a pipeline and run it

Step 5: Results

  • Upon the successful completion of the pipeline run…
  • 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 the PEM key… 🙌

Simple hands-on guide: using User Data script

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

Step 1: Setup Jenkins

  • Launching Jenkins.
  • Install required plugins.
  • Storing AWS credentials and PEM key.
  • All these steps are mentioned above.

Step 2: Write a pipeline definition in Jenkinsfile.

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

Step 3: Create a User Data script.

Step 4: Create a pipeline and build.

  • Below snap is the process of how to create a pipeline and run it:
Flow: How to create a pipeline and run it

Step 5: Results

  • Upon the successful completion of the pipeline run…
  • 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:

Tips:

  • Error: Waiter InstanceRunning failed: Waiter encountered a terminal failure state
    Solution: Instead of using aws ec2 wait instance-running use this aws ec2 wait instance-status-ok
  • Error: AWS was not able to validate the provided access credentials.
    Solution: Try the below flow —
    IAM → Account Settings → Security Token Service → Make Global endpoint valid for all regions
    or follow this link — AWS Credentials Error

Source Code:

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

Docker Images: Link

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 Jenkins-ing! Have a Delightful Deployments🚀

Happy Learning 💻

--

--