In the first part of this blog, we covered how to implement Continuous Integration (CI) for a Flask application using AWS CodePipeline and AWS CodeBuild. Now, we’ll dive into the next crucial step: Continuous Deployment (CD). By the end of this part, your Flask application will be automatically deployed to an EC2 instance every time you push new code to the GitHub repository.
Overview of Continuous Deployment
Imagine you’ve approved every scene for your movie through the editing process. Now, you want to release the movie to theaters worldwide without manually sending each scene to every theater. This is where Continuous Deployment comes in. It ensures that every approved scene is automatically sent to theaters, ready for the audience.
Here's How It Works:
Release to Theaters:
- Once the editing and review process (CI) is complete, the approved movie scenes (Docker images) are automatically sent to the theaters (deployed to EC2).
Automated Deployment Process:
AWS CodePipeline triggers AWS CodeDeploy to handle the deployment.
AWS CodeDeploy ensures the application is correctly updated on all target instances without downtime.
Technical Breakdown
AWS CodePipeline Orchestration:
AWS CodePipeline continues the process from where CI left off.
It triggers AWS CodeDeploy for deploying the Docker image to the EC2 instance.
AWS CodeDeploy Tasks:
Application Specification File (appspec.yml):
- Defines how the deployment should be conducted.
Deployment Group:
- Specifies the EC2 instances to which the application should be deployed.
Practical Implementation:
Setting Up CodeDeploy:
Create an Application in AWS CodeDeploy.
Define a Deployment Group targeting your EC2 instances.
Creating target EC2 instances:
Create the EC2 instances are targets to deploy the Flask app on.
Create tags for it, which will be used later by CodeDeploy for identification
Now, ssh into the created instance and install CodeDeploy Agent on it - https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install.html
Creating IAM roles:
IAM role for CodeDeploy-
Access to manage EC2 instances and perform deployments.
Permissions to read/write to S3 for deployment artifacts.
IAM role for the EC2 instance-
Access to interact with AWS CodeDeploy.
Access to pull Docker images from the Docker registry.
Permissions to execute scripts during deployment.
Now, attach these roles.
Restarting the CodeDeploy Agent-
For these changes to be reflected, restart the CodeDeploy Agent on the EC2 instance using the following command.
sudo service codedeploy-agent restart
(for Ubuntu)
Configure Deployment Group-
Create a Deployment Group:
- Within your newly created application, click on "Create deployment group."
Set Deployment Group Details:
Enter a name for the deployment group (e.g.,
FlaskAppDeploymentGroup
).Select a service role (IAM role) that allows CodeDeploy to access your EC2 instances (e.g.,
CodeDeployServiceRole
).
Configure Environment:
Deployment type: Select
In-place
orBlue/Green
depending on your strategy.Environment configuration: Choose how CodeDeploy should identify your target EC2 instances. Options include using Amazon EC2 Auto Scaling groups or tagging your instances. We have created tags for this purpose.
Configure Deployment Settings:
- Choose the deployment settings that best fit your needs, such as deployment configuration (e.g.,
CodeDeployDefault.OneAtATime
).
- Choose the deployment settings that best fit your needs, such as deployment configuration (e.g.,
Additional Settings:
Add any load balancer configuration if needed (especially important for Blue/Green deployments).
Configure alarms or rollback settings if required.
Create Deployment Group:
- Click on "Create deployment group" to save.
Create a Deployment on CodeDeploy-
Create a Deployment:
Click on "Create deployment."
Deployment group: Select your deployment group.
Revision type: Choose
My application is stored in GitHub
(depending on where your code is stored).AppSpec file: Specify the location of the
appspec.yml
file. Make sure it is present in the root of the directory, doesn't work otherwise.
Start Deployment:
- Click "Create deployment" to initiate the deployment.
Link CodeDeploy with CodePipeline-
Edit the Pipeline:
- Click on "Edit" to modify your pipeline.
Add a Deploy Stage:
- Click on "Add stage" and name it (e.g.,
Deploy
).
- Click on "Add stage" and name it (e.g.,
Add an Action to the Deploy Stage:
Click on "Add action group."
Enter the action name (e.g.,
DeployAction
).Action provider: Select
AWS CodeDeploy
.Region: Select the appropriate AWS region where your application is deployed.
Input artifacts: Select the output artifact from the previous stage (e.g.,
BuildArtifact
).Application name: Select your CodeDeploy application (e.g.,
FlaskApp
).Deployment group: Select your CodeDeploy deployment group (e.g.,
FlaskAppDeploymentGroup
).
Trigger the Pipeline
After linking CodeDeploy to your pipeline:
Commit Changes:
- Push a new commit to your GitHub repository to trigger the pipeline.
Monitor Pipeline Execution:
Go to the CodePipeline console and monitor the pipeline execution.
Ensure that the deployment stage runs successfully and that the application is deployed to the specified EC2 instances.
By following these steps, you will have configured a deployment group and deployments on AWS CodeDeploy, and linked it with AWS CodePipeline. This setup ensures that your Flask application is automatically built, tested, and deployed to your EC2 instances whenever new code is pushed to the GitHub repository, streamlining your CI/CD process.