0
0
JenkinsHow-ToBeginner · 4 min read

How to Deploy to AWS Using Jenkins: Step-by-Step Guide

To deploy to AWS using Jenkins, first configure AWS credentials in Jenkins and create a pipeline that uses AWS CLI commands or plugins to deploy your app. Use Jenkinsfile to automate deployment steps like building, testing, and pushing to AWS services such as EC2 or S3.
📐

Syntax

Here is the basic syntax to deploy to AWS using Jenkins pipeline with AWS CLI commands:

  • pipeline: Defines the Jenkins pipeline.
  • agent any: Runs the pipeline on any available Jenkins agent.
  • environment: Sets environment variables like AWS credentials.
  • stages: Contains steps like build, test, and deploy.
  • sh: Runs shell commands, e.g., AWS CLI commands to deploy.
groovy
pipeline {
  agent any
  environment {
    AWS_ACCESS_KEY_ID = credentials('aws-access-key')
    AWS_SECRET_ACCESS_KEY = credentials('aws-secret-key')
    AWS_DEFAULT_REGION = 'us-east-1'
  }
  stages {
    stage('Deploy to AWS') {
      steps {
        sh 'aws s3 cp ./app.zip s3://my-bucket/app.zip'
        sh 'aws ec2 describe-instances'
      }
    }
  }
}
💻

Example

This example Jenkinsfile uploads a file to an AWS S3 bucket and then lists EC2 instances. It shows how to use AWS credentials securely and run AWS CLI commands in Jenkins.

groovy
pipeline {
  agent any
  environment {
    AWS_ACCESS_KEY_ID = credentials('aws-access-key')
    AWS_SECRET_ACCESS_KEY = credentials('aws-secret-key')
    AWS_DEFAULT_REGION = 'us-east-1'
  }
  stages {
    stage('Upload to S3') {
      steps {
        sh 'aws s3 cp ./app.zip s3://my-bucket/app.zip'
      }
    }
    stage('List EC2 Instances') {
      steps {
        sh 'aws ec2 describe-instances'
      }
    }
  }
}
Output
upload: ./app.zip to s3://my-bucket/app.zip { "Reservations": [ { "Instances": [ { "InstanceId": "i-0123456789abcdef0", "State": {"Name": "running"} } ] } ] }
⚠️

Common Pitfalls

Common mistakes when deploying to AWS using Jenkins include:

  • Not configuring AWS credentials properly in Jenkins, causing authentication failures.
  • Using latest tags or missing version pins in Docker or AWS resources, leading to unpredictable deployments.
  • Not installing or configuring AWS CLI on Jenkins agents.
  • Hardcoding secrets in Jenkinsfiles instead of using Jenkins credentials store.
  • Missing IAM permissions for the AWS user or role Jenkins uses.
groovy
/* Wrong way: Hardcoding AWS keys in Jenkinsfile */
pipeline {
  agent any
  environment {
    AWS_ACCESS_KEY_ID = 'AKIA...'
    AWS_SECRET_ACCESS_KEY = 'secret'
  }
  stages {
    stage('Deploy') {
      steps {
        sh 'aws s3 cp ./app.zip s3://my-bucket/app.zip'
      }
    }
  }
}

/* Right way: Use Jenkins credentials store */
pipeline {
  agent any
  environment {
    AWS_ACCESS_KEY_ID = credentials('aws-access-key')
    AWS_SECRET_ACCESS_KEY = credentials('aws-secret-key')
  }
  stages {
    stage('Deploy') {
      steps {
        sh 'aws s3 cp ./app.zip s3://my-bucket/app.zip'
      }
    }
  }
}
📊

Quick Reference

Tips for deploying to AWS using Jenkins:

  • Store AWS credentials securely in Jenkins Credentials Manager.
  • Install AWS CLI on Jenkins agents or use Docker images with AWS CLI pre-installed.
  • Use Jenkinsfile pipelines to automate deployment steps.
  • Test AWS CLI commands locally before adding to Jenkins.
  • Ensure Jenkins IAM user/role has required permissions for AWS resources.

Key Takeaways

Always store AWS credentials securely in Jenkins Credentials Manager, never hardcode them.
Use Jenkins pipelines with AWS CLI commands to automate deployment to AWS services.
Ensure AWS CLI is installed and configured on Jenkins agents running deployment steps.
Verify IAM permissions for Jenkins AWS user to avoid deployment failures.
Test deployment commands locally before integrating into Jenkins pipelines.