Configuring Jenkins to Deploy to AWS EC2 or ECS

Integrating Jenkins with AWS provides a powerful combination for automating the deployment of cloud-native applications. Jenkins, a popular CI and CD tool, can be used to build, test, and deploy applications to AWS. Automating these deployments can accelerate software delivery.

Integrating Jenkins with AWS provides a powerful combination for automating the deployment of cloud-native applications. Jenkins, a popular continuous integration and delivery tool, can be used to build, test, and deploy applications to AWS services such as EC2 (Elastic Compute Cloud) and ECS (Elastic Container Service). By automating these deployments, you can accelerate your software delivery pipeline, increase reliability, and scale applications more easily.

In this guide, we will explore how to configure Jenkins to deploy applications to AWS EC2 and ECS, detailing the steps involved, necessary configurations, and best practices for a successful deployment pipeline.

1. Introduction to AWS EC2 and ECS

  • AWS EC2 (Elastic Compute Cloud): EC2 provides resizable compute capacity in the cloud, enabling users to launch virtual servers (instances) on demand. Jenkins can be configured to deploy applications directly to EC2 instances.
  • AWS ECS (Elastic Container Service): ECS is a highly scalable, fast container orchestration service that allows you to run Docker containers. Jenkins can be integrated with ECS to automate the deployment of containerized applications.

Both services are core components of AWS and provide a foundation for running and deploying applications in a cloud-native environment.

2. Prerequisites

Before proceeding with the configuration, ensure that you have the following:

  1. AWS Account: You need an AWS account to set up EC2 instances and ECS clusters.
  2. Jenkins Installed: Jenkins must be installed and running. This can be done either on-premises, on a local server, or hosted on AWS itself.
  3. AWS CLI Installed: The AWS CLI (Command Line Interface) is required for Jenkins to communicate with AWS.
  4. IAM User or Role: Create an IAM user or role with the appropriate permissions to interact with AWS services such as EC2 and ECS.
  5. Jenkins AWS Plugins: Ensure the AWS SDK for Jenkins and Amazon EC2 Plugin are installed in Jenkins for EC2 integration.

3. Setting up Jenkins on AWS

If Jenkins is not already set up, you can either install Jenkins locally or run Jenkins on an AWS EC2 instance for a fully cloud-based CI/CD solution. For detailed instructions on how to install Jenkins on EC2, refer to the official Jenkins documentation.

Once Jenkins is set up, log in to the Jenkins dashboard to begin the configuration.

4. Configuring Jenkins to Deploy to AWS EC2

4.1 Creating an EC2 Instance

  1. Log in to the AWS Management Console.
  2. Navigate to the EC2 Dashboard and click Launch Instance.
  3. Select the Amazon Machine Image (AMI) that suits your application's requirements.
  4. Choose an appropriate Instance Type (e.g., t2.micro for lightweight applications).
  5. Configure the Security Group to allow incoming traffic on the required ports (e.g., HTTP/HTTPS for web applications).
  6. Launch the instance and note down the Public IP Address or Public DNS of the EC2 instance.

You will use this instance to deploy your application from Jenkins.

4.2 Installing and Configuring AWS CLI on Jenkins

To enable Jenkins to communicate with AWS, you'll need to install and configure the AWS CLI on the Jenkins server.

    • SSH into your Jenkins server and run the following commands to install the AWS CLI:
    • Run aws configure and provide the necessary credentials (AWS Access Key, Secret Key, region, and output format).
    • To ensure the AWS CLI is working correctly, run the following command to list EC2 instances:

Verify AWS CLI:

aws ec2 describe-instances

Configure AWS CLI:

aws configure

Install AWS CLI:

sudo apt-get update
sudo apt-get install awscli -y

If the list of EC2 instances is returned, the AWS CLI is configured properly.

4.3 Configuring Jenkins Job to Deploy to EC2

  1. Create a New Jenkins Job:
    • In the Jenkins dashboard, click New Item and create a Freestyle Project.
  2. Configure Source Code Management:
    • Configure Jenkins to pull the code from your version control system (e.g., GitHub). Under Source Code Management, select Git and provide the repository URL.
    • Under the Build section, add a new build step to Execute shell.
    • Use the AWS CLI in the shell script to deploy the application to EC2. For example, you can use scp to copy your application files to the EC2 instance and then execute deployment scripts via SSH:
  3. Save and Build:
    • Save the Jenkins job and click Build Now to trigger the deployment.

Add Build Steps:

scp -i /path/to/key.pem ./app-files/* ec2-user@<EC2-Instance-IP>:/var/www/html/
ssh -i /path/to/key.pem ec2-user@<EC2-Instance-IP> 'sudo systemctl restart httpd'

At this point, your application should be deployed to the EC2 instance, and Jenkins will report the build and deployment status.

5. Configuring Jenkins to Deploy to AWS ECS

5.1 Setting Up an ECS Cluster

  1. Log in to AWS Management Console and navigate to ECS.
  2. Click Create Cluster and select the appropriate cluster template (e.g., EC2 or Fargate).
  3. Name your cluster and configure the instance size and capacity.
  4. Once created, navigate to the Clusters section to view your newly created cluster.

5.2 Creating an ECS Task Definition

The ECS Task Definition describes the Docker container that will be deployed.

  1. Navigate to Task Definitions in the ECS Console and click Create New Task Definition.
  2. Choose either EC2 or Fargate as the launch type.
  3. Define your container configurations, including the Docker image, memory, CPU, networking, and ports.

For example, if you’re deploying a web app, you might configure the task definition with the following parameters:

  • Image: The Docker image from your registry (e.g., Amazon ECR).
  • Memory and CPU: Resource limits for your container.
  • Ports: Expose the necessary ports (e.g., 80 or 443 for web applications).

5.3 Setting Up Jenkins to Deploy to ECS

  1. Install the AWS ECS Plugin:
    • In Jenkins, go to Manage Plugins and install the Amazon ECS plugin.
  2. Configure Jenkins with AWS Credentials:
    • Go to Manage Jenkins > Manage Credentials and add AWS credentials (IAM Access Key and Secret Key) that Jenkins can use to interact with ECS.
    • Create a new Pipeline Job in Jenkins.
    • Write a Jenkinsfile to deploy your container to ECS. You can use the ecsDeploy plugin to deploy your Docker container to an ECS cluster.
  3. Trigger the Pipeline:
    • Trigger the pipeline by committing

Create a Jenkins Pipeline:Example Jenkinsfile for ECS Deployment:

pipeline {
    agent any
    environment {
        AWS_DEFAULT_REGION = 'us-west-2'
        AWS_ACCESS_KEY_ID = credentials('aws-access-key')
        AWS_SECRET_ACCESS_KEY = credentials('aws-secret-key')
    }
    stages {
        stage('Deploy to ECS') {
            steps {
                ecsDeploy(
                    cluster: 'my-ecs-cluster',
                    service: 'my-ecs-service',
                    taskDefinition: 'my-task-def:1',
                    forceNewDeployment: true
                )
            }
        }
    }
}

code or manually starting the job. Jenkins will build the Docker container and deploy it to ECS.

6. Using Jenkins with AWS Credentials and IAM Roles

To securely manage AWS credentials in Jenkins, it is recommended to use either:

  • Jenkins Credentials Plugin: Store your AWS credentials securely using the Jenkins Credentials Plugin, which allows you to reference credentials in Jenkinsfiles or Freestyle jobs.
  • IAM Roles: If Jenkins is running on an AWS EC2 instance, you can assign an IAM role to the instance that provides the necessary permissions, eliminating the need to manage long-lived AWS Access Keys.

By using either of these methods, Jenkins can securely access AWS resources during deployments.

7. Best Practices for Jenkins Deployments to AWS

  1. Use IAM Roles and Policies: Always use IAM roles with the principle of least privilege. Ensure that Jenkins has only the permissions required to deploy applications to AWS.
  2. Automate Everything: Automate the provisioning of EC2 instances and ECS clusters using tools like CloudFormation or Terraform in combination with Jenkins.
  3. Monitor and Log Deployments: Use AWS CloudWatch and CloudTrail to monitor the health of your EC2 instances, ECS containers, and Jenkins jobs. This will help you troubleshoot issues faster.
  4. Dockerize Jenkins: For better portability, consider running Jenkins itself in a Docker container on AWS EC2 or ECS, which simplifies deployment and scaling of the Jenkins master.

Conclusion

By configuring Jenkins to deploy to AWS EC2 or ECS, you can streamline your cloud-native application deployments and make your CI/CD pipeline more efficient. With the right setup, Jenkins can handle the entire lifecycle of your applications—from building and testing to deployment on the cloud.

Whether you’re using EC2 for virtual servers or ECS for containerized workloads, Jenkins integrates seamlessly with AWS, making it an ideal choice for automating cloud deployments.

Make sure to follow the best practices outlined here to ensure your deployments are secure, reliable, and scalable as your infrastructure grows.

Read next

Using Version Control to Manage Jenkins Configurations

As Jenkins grows into an integral part of your CI/CD process, managing Jenkins configurations becomes crucial for maintaining consistency, tracking changes, and ensuring collaboration across teams. As we use version control systems to manage codebases, it’s best practice to version control Jenkin.