Deploy Python App on AWS ECS Service by Jenkins CI/CD Pipeline
Let us create our first ci/cd pipeline to deploy our python application on AWS ECS Service. With this pipeline you can deploy your app to any other AWS service as well like EKS or AWS WebApp. You can check our other article about this if you want and its url is at the end of this article. If you don’t have a Jenkins server, you can find installation steps in our article about it which has url at the en of this article. Go to Dashboard and click on βNew Itemβ to create a pipeline. Write a pipeline name and choose Pipeline and click Ok.
Write a description if you wish and tick on GitHub hook and Poll SCM options and write β*/1 * * * *β in Schedule text box to check every minute github repository for new codes. You can change Poll SCM settings according to your design configuration.
Now write your own Pipeline stages for your deployment requirements. Here is my pipeline stage codes for deploying a python application on AWS ECS service. You can download my code from this url
https://raw.githubusercontent.com/semiharsan/devops/main/jenkinspipelineforawsecs
To run this pipeline seamless, we need to add github repository along with its account name and password or ssh token and also aws account so that pipeline can connect to github and aws. We have already added aws jenkins user at the beginning so you just need to change credentialsID value with your user name for aws account.
stage(‘Create ECR Repository’) {
steps {
script {
withCredentials([[
$class: ‘AmazonWebServicesCredentialsBinding’,
accessKeyVariable: ‘AWS_ACCESS_KEY_ID’,
secretKeyVariable: ‘AWS_SECRET_ACCESS_KEY’,
credentialsId: ‘WRITE_HERE_YOUR_AWS_IAM_USERNAME’
]])
Click on Pipeline Syntax under the Pipeline Code Block to add github user or Go to Manage Jenkins-Credentials and click on (global) like we did for aws Jenkins user. After you added github user just change the values below stage in your pipeline with yours. After you have got pipeline script from syntax, paste it inside script {} block.
stage(‘Checkout’) {
steps {
script {
// Checkout the code from GitHub
checkout scmGit(branches: [[name: ‘*/main’]], extensions: [], userRemoteConfigs: [[credentialsId: ‘b66dea26-cddc-4ad5-bc33-15595ceebe9f’, url: ‘https://github.com/semiharsan/pythondemoapp.git’]])
}
}
}
Now we are ready to run pipeline. Before you run this pipeline please create a folder with “terraform” name under /var/lib/jenkins/workspace and change owner with jenkins with this command “chown -R jenkins:jenkins /var/lib/jenkins/workspace/terraform”. Because we don’t want to lose our terraform codes and tfstate files in case of we may want to destroy the resources that we created by terraform. If you download terraform code under your pipeline folder, then jenkins might remove it after pipeline has finished. To prevent this possibility, it is better to save terraform files under another folder. Now let us run pipeline with clicking “Build Now” to deploy python app to AWS ECS Service. After I have changed hello.py file you can see step 2 started automatically and deployed new container and updated ecs service.
Congrats !!! We have created a ci/cd pipeline to deploy python application on AWS ECS Service. I hope this article would provide a good information for your need.
<< GO TO SETUP JENKINS ON UBUNTU LINUX SERVER >>
<< GO TO DEPLOY PYTHON APP ON AWS EKS CLUSTER SERVICE BY JENKINS PIPELINE >>