AWS CICD Pipeline with Container
Containers :
Containers are a method of operating system virtualization that allow you to run an application and its dependencies in resource-isolated processes.Running containers in the AWS Cloud allows you to build robust, scalable applications and services by leveraging the benefits of the AWS Cloud such as elasticity, availability, security, and economies of scale. You also only pay for as many resources as you use.
Elastic Container Service (ECS) :
Amazon Elastic Container Service (Amazon ECS) is a fully managed container orchestration service.ECS has been a foundational pillar for key Amazon services, it can natively integrate with other services such as Amazon Route 53, Secrets Manager, AWS Identity and Access Management (IAM), and Amazon CloudWatch providing you a familiar experience to deploy and scale your containers.
Elastic Kubernetes Service (EKS) :
Amazon Elastic Kubernetes Service (Amazon EKS) is a fully managed Kubernetes service.Customers such as Intel, Snap, Intuit, GoDaddy, and Autodesk trust EKS to run their most sensitive and mission critical applications because of its security, reliability, and scalability.EKS is deeply integrated with services such as Amazon CloudWatch, Auto Scaling Groups, AWS Identity and Access Management (IAM), and Amazon Virtual Private Cloud (VPC), providing you a seamless experience to monitor, scale, and load-balance your applications.
CodeCommit :
AWS CodeCommit is a fully-managed source control service that hosts secure Git-based repositories. It makes it easy for teams to collaborate on code in a secure and highly scalable ecosystem. CodeCommit eliminates the need to operate your own source control system or worry about scaling its infrastructure. You can use CodeCommit to securely store anything from source code to binaries, and it works seamlessly with your existing Git tools.
CodeBuild :
AWS CodeBuild is a fully managed continuous integration service that compiles source code, runs tests, and produces software packages that are ready to deploy. With CodeBuild, you don’t need to provision, manage, and scale your own build servers. CodeBuild scales continuously and processes multiple builds concurrently, so your builds are not left waiting in a queue. You can get started quickly by using prepackaged build environments, or you can create custom build environments that use your own build tools.
CodeDeploy :
AWS CodeDeploy is a fully managed deployment service that automates software deployments to a variety of compute services such as Amazon EC2, AWS Fargate, AWS Lambda, and your on-premises servers. AWS CodeDeploy makes it easier for you to rapidly release new features, helps you avoid downtime during application deployment, and handles the complexity of updating your applications.
CodePipeline :
AWS CodePipeline is a fully managed continuous delivery service that helps you automate your release pipelines for fast and reliable application and infrastructure updates. CodePipeline automates the build, test, and deploy phases of your release process every time there is a code change, based on the release model you define.AWS CodePipeline with third-party services such as GitHub or with your own custom plugin.
Simple Web-application deployment on EC2 using AWS CodeCommit, AWS CodeDeploy & AWS CodePipeline
Prerequisite :
- AWS CLI
Steps to create CodeCommit Repository
- Open Console -> CodeCommit -> Create repository.
We have created Codecommit Repository. Now, download this repository into local machine using HTTPS git. For that we have to download and configure git and AWS CLI in our local machine. To configure HTTPS git we need credentials.
2. IAM -> users -> select your user name.
3. Open AWS CLI -> git clone <your HTTPS URL>
4. Go into your repository
5. git config — local user.name “YOUR GITHUB USERNAME”
6. git config — local user.email “YOUR GITHUB EMAIL”
7. git add .
8. git commit -m “done”
9. git push -u origin master
while pushing code to repository you will be asked for HTTPS git credentials which u have generated.
Steps to create Amazon EC2 Instance
First we have to create instance role.
- IAM -> Roles -> Create Role -> Choose EC2 -> Next
- Search for AmazonEC2RoleforAWSCodeDeploy -> Next : Tags -> Next : Review
- Enter Role name -> Create Role
to launch EC2 instance
- EC2 -> Launch instance ->choose Amazon Linux 2 AMI (HVM), SSD Volume Type -> Select
- Choose an Instance Type : t2.micro -> Next
- Configure Instance Details :
· In IAM role, choose the IAM role you created.
· Expand Advanced Details, and in the User data field, enter the following:
#!/bin/bash
yum -y update
yum install -y ruby
yum install -y aws-cli
cd /home/ec2-user
aws s3 cp s3://aws-codedeploy-us-east-2/latest/install . — region us-east-2
chmod +x ./install
./install auto
4. Next : Add Storage -> Next : Add Tags -> Key : Name, Value : MyCodePipelineDemo (You can give any name) -> Next
5. Add Rule -> Select HTTP -> Review and Launch -> Launch -> choose Proceed without a key pair
instance will be launched in couple of minutes.
Steps to create Application in CodeDeploy
First we have to create role for CodeDeploy.
- IAM -> Roles -> Create Role -> Choose CodeDeploy-> Next
- The AWSCodeDeploy managed policy is already attached to the role -> Next : Tags -> Next : Review
- Enter Role name -> Create Role
to create an application in CodeDeploy
- CodeDeploy -> Application -> Create Application
- In Compute Platform -> choose EC2/On-premises -> Create application
to create a deployment group in CodeDeploy
- Create deployment group
- In Service Role -> choose the service role you created earlier
- Deployment type -> choose In-place.
if you want to test your application then go with blue-green deployment
4. Environment configuration -> choose Amazon EC2 Instances -> In the Key field, enter the name you used to tag the instance
5. Deployment configuration -> choose CodeDeployDefault.OneAtaTime
6. Load Balancer -> clear Enable load balancing
7. Create deployment group
Steps to create CodePipeline
- CodePipeline -> Create Pipeline
- Service role -> choose New service role to allow CodePipeline to create a service role in IAM -> Next
- In Source provider -> choose AWS CodeCommit
- In Repository name -> choose the name of the CodeCommit repository you created
- Branch name -> choose master -> Next
- skip build stage
- In Deploy provider-> choose AWS CodeDeploy
- Choose your Application name and Deployment Group -> Next
- Review -> Create pipeline
You have created 2 stage simple pipeline in codepipeline.
to verify that your pipeline ran successfully
Open EC2 console -> On the Description tab, in Public DNS, copy the address and then paste it into your web browser.
Now, you can easily modify your website in few clicks. You just need to change code in your local machine and push it in CodeCommit Repository and refresh website page, changes will be directly reflected in your page.
Deployment of Single Docker Container in AWS Elastic Beanstalk
Prerequisite :
- AWS EB CLI
to create Docker Container
- Open terminal -> mkdir dockercontainer
- nano index.html
3. nano Dockerfile
to build and run docker container locally on our machine we have to start docker services first.
4. sudo systemctl start docker
5. docker build -t dockercontainer
6. docker run — name nginx -p 8080:80 dockercontainer
to deploy docker container in Elastic Beanstalk environment
- Intialize EB environment
eb init -> choose your region -> Select your application
It appears you are using docker, Is this correct? : Y
select a platform branch : Docker running on 64bit Amazon Linux
2. eb create
give environment name and DNS prefix
choose load balancer : application
enable spot fleet requests : N
So, our instance has been ready to use and it has also configured all the services by itself which is needed to host the website. Such as, EC2 instance, S3 bucket, Security group, Auto scaling group, CloudWatch Alarm, Load Balancer.
Now, we can check our website in web browser using public DNS which is provided with EC2 instance as well as we can use AWS CLI.
Command : eb open
So now if we want to make any changes in the website we need to change our code and just one command we have to give is eb deploy. It will create second application version, stop docker container and launch another docker container on the same instance.
We can also terminate EB Environment and all the services which had been configured automatically with eb instance while creating.
Command : eb terminate — force
Conclusion :
Independently of our CI/CD pipeline, we may want to use containers in production because containers can help us to reduce the risks associated with a new release. When we start a new version of our app/website, if something goes wrong, rolling back is very easy. All we have to do is just stop the container, and restart the previous version. The image for the previous version will still be around and will start immediately. So, we can deploy with more confidence.