Adding a production stage and environment
In this section, we will add a production stage to the existing pipeline and will also modify the CloudFormation template to spin up a separate production environment with two EC2 instances via an ASG under a load balancer.
Modifying the pipeline
Currently, our main pipeline looks like the one shown in Figure 1.26. The Elastic Beanstalk environment spun up by this pipeline is named northstarapp
, and we need to change it to northstarappstaging
. After that, we need to add a manual approval stage, and then a production deployment stage. In the production deployment stage, we will add a configuration parameter in CloudFormation to spin up a production environment with the name northstarappprod
and deploy the application in this new environment.
To modify the pipeline, follow these next steps:
- Go to northstar-Pipeline CodePipeline project (see Figure 1.27) and click on Edit.
- Click on Edit stage in the Edit: Deploy screen, as illustrated in the following screenshot:
- Edit the GenerateChangeSet action group, go to Advanced | Parameter overrides, and add one key value in JSON format:
"Stage":"Staging"
, as illustrated in the following screenshot. Also, copy and paste the entire JSON config into a separate note because we will be using that in the production parameter. Click on Done to save the configuration. Click on Done to save the Deploy stage:
- Add a new stage by clicking on Add stage. Give a stage name of
Approval
, as illustrated in the following screenshot:
- Click on Add action group, then enter
ManualApproval
under Action name andManual approval
under Action provider, as illustrated in the following screenshot. You can configure a Simple Notification Service (SNS) topic, but we are skipping this here. Click on Done to save the action group:
- Click on Add stage to add a production deploy stage. Name the stage
ProdDeploy
. Click on Add action group. EnterGenerateChangeSet
under Action name,AWS CloudFormation
under Action provider, andnorthstar-BuildArtifact
under Input artifacts. Then, click on Create or replace a change set under Action mode and enterawscodestar-northstar-infrastructure-prod
under Stack name,pipeline-changeset
under Change set name,northstar-BuildArtifact
under Template | Artifact name, andtemplate-export.yml
under File name. Select Use configuration file, then enternorthstar-BuildArtifact
under Template configuration | Artifact name,template-configuration.json
under File name,CAPABILITY_NAMED_IAM
under Capabilities, andCodeStarWorker-northstar-CloudFormation
under Role name. Click on the Advanced section and paste the JSON content that we copied in Step 3. Set the lastStage
value toProd
and click on Done.
The process is illustrated in the following screenshot:
- Click again on Add action group. For the action name, enter
ExecuteChangeSet
, and enterAWS CloudFormation
under Action provider. Under Action mode, we need to select Execute a change set. Under Stack name, we need to enterawscodestar-northstar-infrastructure-prod
. Under Change set name, we need to enterpipeline-changeset
.
The process is illustrated in the following screenshot:
- Save the pipeline. Now, raise a PR from
develop
tomaster
and merge the code tomaster
, which will run thenorthstar-Pipeline
pipeline. This pipeline will rename the existing environment fromnorthstarapp
tonorthstarappStaging
and deploy the application. Then, we manually need to check the application. If the application is working fine, then we need to approve it to proceed to theProdDeploy
stage. In theProdDeploy
stage, CloudFormation will spin up anorthstarappProd
Elastic Beanstalk production environment then deploy the application in thenorthstarappProd
production environment.
You can see the PR being raised in the following screenshot:
- The pipeline will get triggered after the merge process, as illustrated in the following screenshot:
- The initially created
northstarapp
environment will be terminated and a newnorthstarappStaging
environment will be created, as illustrated in the following screenshot:
- You can access the staging application by navigating to the
northstarappStaging
Elastic Beanstalk environment, as illustrated in the following screenshot:
- In the pipeline, it's waiting for approval. Approve it by entering a comment, as illustrated in the following screenshot:
- Once the
ProdDeploy
stage is successful, you can go to Elastic Beanstalk and search fornorthstarappProd
, as illustrated in the following screenshot:
- You can access the application by clicking the endpoint, as illustrated in the following screenshot:
- You can also go to Load balancer to check the new ELB with two EC2 instances attached to it, as illustrated in the following screenshot:
So, we just saw how to modify a pipeline and add a production stage. You can also make it more comprehensive by creating an SNS topic during the approval stage, and also by adding an ELB Domain Name System (DNS) in Route 53. You can make this change via a CloudFormation template.