Deploy an application with a PostgreSQL Database on AWS within 3 Environments (Production, Staging, Development).??
In the DevOps world, we sometimes struggle with terminology.?[Chapter 4]
If you have missed Chapter 1, 2 & 3, where the purpose of the project, a 3 Tier Web Architecture for a single region and a multi region application with Bespoke AWS Cross-Region CI/CD Pipeline, were discussed with the planning. Click here to review:?Chapter 1 ?,?Chapter 2 & Chapter 3
Note:?A stage is a stack that you deploy for a specific purpose.
Stages are used in code pipelines to provide an on-ramp to push code changes from smaller to wider audiences. For example, a change will typically start with a dev stage (either a team or a personal dev stack). If the change works on the dev stage, a team may next push it to a test stage where internal stakeholders can access it, followed by a staging stage where it's available to a limited number of external customers. Finally, if the change passes all automated and manual tests, it'll be pushed to the production stage.
Stages can also be referred to as "environments" - for example, the dev environment, test environment, etc.
>????Thank you for your interest in my work.???
Using this solution, you can easily set up and manage an entire CI/CD pipeline in AWS accounts using the native AWS suite of CI/CD services, where a commit or change to code passes through various automated stage gates all the way from building and testing to deploying applications, from development to production environments.
The project is supported by several managed services including?Amazon Route 53 ,?Amazon CloudFront ,?AWS WAF ,?Elastic Load Balancing (ELB) ,?AWS Shield ,?Amazon ElastiCache ,?Amazon RDS ,?Amazon S3 , etc.
Full Production, Staging and Dev environments on AWS with Kubernetes and RDS???
Click on the below example like to see how to deploy a containerized app with PostgreSQL on AWS in Development, Staging and Production and makes it accessible via HTTPS. All of that in just a few lines of Terraform file.
Behind the Scene
?????Creates 3 Kubernetes clusters (Dev,?Staging,?Production) on the AWS account (VPC, Security Groups, Subnet, EKS/Kubernetes...).
?????Creates resources:
?????Builds?ops app?application for?Production,?Staging?and?Dev?environments in parallel.
?????Pushes?ops app?container image in the ECR registry for?Production,?Staging?and?Dev?environments in parallel.
?????Deploys your PostgreSQL database for?Production?(AWS RDS),?Staging?(AWS RDS) and?Dev?(Container) environments in parallel.
?????Deploys?ops app?on your?Production,?Staging?and?Dev?EKS clusters.
?????Creates an AWS Network Load Balancer for all your clusters and apps.
?????Generates a TLS certificate for your app for all your apps.
?????Exposes publicly via HTTPS your ops app from?Production,?Staging?and?Dev?through different endpoints.
It takes approximately?20 minutes to create the infrastructure?and?less than 10 minutes to deploy the application?for each environment.
AWS services
This solution uses the following AWS services:
Overview of solution
This solution uses three separate AWS accounts: a development account (1111), a stage account, and a production account (2222) in Region eu-central-1.
We use the development account to deploy and set up the CI/CD pipeline, along with the source code repository. It also builds and tests the code locally and performs a test deploy.
The production account is any other account where the application is required to be deployed from the pipeline in the dev account.
In summary, the solution has the following workflow:
The following diagram illustrates the workflow:
Failsafe deployments
This example of CodeDeploy uses the IN_PLACE type of deployment. However, to minimize the downtime, CodeDeploy inherently supports multiple deployment strategies. This example makes use of following features: rolling deployments and automatic rollback.
CodeDeploy provides the following three predefined deployment configurations, to minimize the impact during application upgrades:
For OneAtATime and HalfAtATime, CodeDeploy monitors and evaluates instance health during the deployment and only proceeds to the next instance or next half if the previous deployment is healthy. For more information, see Working with deployment configurations in CodeDeploy.
You can also configure a deployment group or deployment to automatically roll back when a deployment fails or when a monitoring threshold you specify is met. In this case, the last known good version of an application revision is automatically redeployed after a failure with the new application version.
How CodePipeline in the dev account deploys apps in the prod account?
In this post, the deployment pipeline using CodePipeline is set up in the dev account, but it has permissions to deploy the application in the staging and prod account. We create a special cross-account role in the prod account, which has the following:
CodePipeline in the dev account assumes this cross-account role in the prod account to deploy the app.
领英推荐
Setting up the prod account
To set up the prod account, complete the following steps:
?????Download and launch the AWS CloudFormation template from the GitHub repo: cicd-codedeploy-prod.json
?????Update the existing EC2 IAM instance profile (cicd_ec2_instance_profile):
Setting up the dev account
To set up your dev account, complete the following steps:
?????Download and launch the CloudFormation template from the GitHub repo: cicd-aws-code-suite-dev.json The stack deploys the following services in the dev account:
?????You should have created all the existing resources and roles beforehand as part of the prerequisites.
?????Update the existing Amazon EC2 IAM instance profile (cicd_ec2_instance_profile):
Deploying the application
You’re now ready to deploy the application via your desktop or PC.
?????Assuming you have the required HTTPS Git credentials for CodeCommit as part of the prerequisites, clone the CodeCommit repo that you created earlier as part of the dev account setup. Obtain the name of the CodeCommit repo to clone, from the CodeCommit console. Enter the Git user name and password when prompted. For example:
$ git clone https://git-codecommit.eu-central-1.amazonaws.com/v1/repos/MyWebAppRepo my-web-app-repo
Cloning into 'my-web-app-repo'...
Username for 'https://git-codecommit.eu-central-1.amazonaws.com/v1/repos/MyWebAppRepo': xxxx
Password for 'https://[email protected]/v1/repos/MyWebAppRepo': xxxx
?????Download the MyWebAppRepo.zip file containing a sample Java application, CodeBuild configuration to build the app, and CodeDeploy config file to deploy the app.
?????Copy and unzip the file into the my-web-app-repo Git repository folder created earlier.
?????Assuming this is the sample app to be deployed, commit these changes to the Git repo. For example:
$ cd my-web-app-repo?
$ git add -A?
$ git commit -m "initial commit"?
$ git push
After you commit the code, the CodePipeline will be triggered and all the stages and your application should be built, tested, and deployed all the way to the production environment!
Cleaning up
To avoid incurring future charges or to remove any unwanted resources, delete the following:
FAQs
In this section, I answered some frequently asked questions:
?????Can I expand this deployment to more than two accounts?
?????Can I ensure the application isn’t automatically deployed in the prod account via CodePipeline and needs manual approval?
?????Can I use a CodeDeploy group with an Auto Scaling group?
?????Can I use this pattern for EC2 Windows instances?
Have fun, and build on!???
Want to learn more about this project? Click?here .??
About Me???