Here we will trigger an automated rollback by returning a 500 response code. The code deployment group has the CloudWatch alarm configured to detect and rollback a deployment with 5xx errors.
nginx.conf
to return a 500 error instead of default index.html
Modify the / location directive to look like the following:
listen 80;
root /usr/share/nginx/html;
include /etc/nginx/mime.types;
location / {
return 500;
}
# Run as a less privileged user for security reasons.
user nginx;
# Number of worker_threads to run;
# "auto" sets it to the #CPU_cores available in the system, and
# offers the best performance.
worker_processes auto;
events { worker_connections 1024; }
http {
server {
# Hide nginx version information.
server_tokens off;
listen 80;
root /usr/share/nginx/html;
include /etc/nginx/mime.types;
location / {
return 500;
}
gzip on;
gzip_vary on;
gzip_http_version 1.0;
gzip_comp_level 5;
gzip_types
application/atom+xml
application/javascript
application/json
application/rss+xml
application/vnd.ms-fontobject
application/x-font-ttf
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/opentype
image/svg+xml
image/x-icon
text/css
text/plain
text/x-component;
gzip_proxied no-cache no-store private expired auth;
gzip_min_length 256;
gunzip on;
}
}
cd ~/environment/nginx-sample
git add .
git commit -m "Returning 500 error"
git push
80
Here is the command to get the url:
echo "http://$ALB_DNS"
private static create5xxMetric(targetGroup: ApplicationTargetGroup, alb: ApplicationLoadBalancer) {
return new cloudWatch.Metric({
namespace: 'AWS/ApplicationELB',
metricName: 'HTTPCode_Target_5XX_Count',
dimensions: {
TargetGroup: targetGroup.targetGroupFullName,
LoadBalancer: alb.loadBalancerFullName
},
statistic: cloudWatch.Statistic.SUM,
period: Duration.seconds(300)
});
}
private createAlarm(metric: Metric, targetGroupName: string, errorType: string, evaluationPeriods: number) {
const alarmName = this.prefix.concat(targetGroupName).concat(errorType).concat('Alarm');
return new cloudWatch.Alarm(this, alarmName, {
alarmName: alarmName,
alarmDescription: 'CloudWatch Alarm for the '.concat(errorType).concat(' errors of ').concat(targetGroupName).concat(' target group'),
metric: metric,
threshold: 1,
evaluationPeriods: evaluationPeriods
});
}
That’s it. You have successfully completed a blue/green deployment and done automatic rollback of a failed deployment. Let’s review the configurations files now.