EC2 Deployment
Learn the concepts, configuration, commands, and practical workflow required to securely deploy applications to Amazon EC2 virtual servers using Jenkins.
What is EC2 Deployment?
EC2 Deployment is the process of taking a compiled application artifact (such as a JAR, ZIP, or Docker container image) and distributing it onto an **Amazon Elastic Compute Cloud (EC2)** instance.
Within a modern Phase 5 CI/CD pipeline, this mechanism replaces manual server patching and interactive terminal updates with auditable, zero-downtime automation scripts that manage application process lifecycles.
Key Concepts
SSH Agent Authentication
Using tools like the **SSH Agent Plugin**, Jenkins opens temporary encrypted connections to remote EC2 targets via private SSH keys stored safely in the global credential vault.
Artifact Staging (SCP/SFTP)
Jenkins relies on **Secure Copy Protocol (SCP)** steps to copy fresh application builds over network subnets before triggering actual startup commands on the target host machine.
Remote Lifecycle Control
Pipelines issue direct shell execution commands (such as systemctl restart or docker-compose up) on the virtual machine to gracefully swap older runtimes without losing application state.
Deployment Security
Production instances remain tucked away inside a private VPC subnet. Jenkins uses secure security groups or **AWS Systems Manager (SSM) Run Command** interfaces to complete deployments without public endpoints.
Practical Jenkins Example
The following Declarative Pipeline demonstrates how to pull compiled artifacts from a staging folder, securely authenticate via SSH, copy a target package, and restart an system service on an EC2 instance:
pipeline {
agent any
environment {
SSH_KEY_ID = 'ec2-prod-ssh-key'
TARGET_HOST = '10.0.1.45'
REMOTE_USER = 'ubuntu'
DOCKER_REGISTRY = 'docker.io/mycompany'
IMAGE_NAME = 'web-application'
IMAGE_TAG = "$${BUILD_NUMBER}"
}
stages {
stage('Pull & Compile') {
steps {
checkout scm
echo "Packaging distribution application modules..."
}
}
stage('Secure Multi-Phase EC2 Deployment') {
steps {
sshagent(credentials: ["$${SSH_KEY_ID}"]) {
script {
echo "Opening secure tunnel channel to virtual node: $${TARGET_HOST}"
sh """
ssh -o StrictHostKeyChecking=no $${REMOTE_USER}@$${TARGET_HOST} '
docker stop $${IMAGE_NAME} || true &&
docker rm $${IMAGE_NAME} || true
'
"""
sh """
ssh -o StrictHostKeyChecking=no $${REMOTE_USER}@$${TARGET_HOST} '
docker login -u "\$$REGISTRY_USER" -p "\$$REGISTRY_PASS" &&
docker run -d --name $${IMAGE_NAME} -p 80:8080 $${DOCKER_REGISTRY}/$${IMAGE_NAME}:$${IMAGE_TAG}
'
"""
}
}
}
}
}
post {
cleanup {
cleanWs()
}
}
}
Practice Exercise
- Open Jenkins and identify the relevant configuration area.
- Create or update a small test job or pipeline for this lesson.
- Run the job and inspect the Console Output.
- Record what changed and verify the result.
Summary
You have completed the EC2 Deployment lesson. Continue through the syllabus to build the Jenkins skills required for real CI/CD automation.