-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDBlueGreen.yaml
755 lines (698 loc) · 24.4 KB
/
CDBlueGreen.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License. A copy of the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# CDBlueGreen.yaml
#
# This is a CloudFormation template that creates the sample environment
# for the demonstration of the blue/green deployments for AWS CodeDeploy.
#
# The main components that are created include:
#
# BlueGreenLaunchConfig - This is a launch configuration that will
# bring up instances with version 1 of the code.
#
# BlueGreenWorkstation - This will be where a user logs into run
# the CodeDeploy tools to deploy version 2 of the code.
AWSTemplateFormatVersion: '2010-09-09'
Description: Create the VPC framework for the Ubuntu BlueGreen environment.
# Define the mappings for the Ubuntu 18.04 LTS in the regions that
# offer CodeDeploy.
Mappings:
RegionMap:
ap-northeast-1:
ubuntu: ami-07ad4b1c3af1ea214
ap-northeast-2:
ubuntu: ami-06e7b9c5e0c4dd014
ap-southeast-1:
ubuntu: ami-0c5199d385b432989
ap-southeast-2:
ubuntu: ami-07a3bd4944eb120a0
ca-central-1:
ubuntu: ami-0427e8367e3770df1
eu-central-1:
ubuntu: ami-0bdf93799014acdc4
eu-west-1:
ubuntu: ami-00035f41c82244dab
eu-west-2:
ubuntu: ami-0b0a60c0a2bd40612
us-east-1:
ubuntu: ami-0ac019f4fcb7cb7e6
us-east-2:
ubuntu: ami-0f65671a86f061fcd
us-west-1:
ubuntu: ami-063aa838bd7631e0b
us-west-2:
ubuntu: ami-0bbe6b35405ecebdb
# Offer the IP of the workstation host so the template user can log in.
Outputs:
LoadBalancer:
Description: DNS Name of AWS Elastic Load Balancer
Value: !GetAtt BlueGreenLB.DNSName
AutoScalingGroup:
Description: Initial auto scaling group
Value: !Ref BlueGreenASG
WorkStationIP:
Description: IP address of workStation host
Value: !GetAtt BlueGreenWorkStation.PublicIp
# Request a key pair, AZ, and a prefix that will be used to assign
# resource name tags.
Parameters:
AZName:
Description: Subnet availability zone
Type: AWS::EC2::AvailabilityZone::Name
BlueGreenKeyPairName:
Description: EC2 keypair for hosts
Type: AWS::EC2::KeyPair::KeyName
NamePrefix:
AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
Default: bluegreen
Description: Prefix for resource tags made by this template (2-15 chars).
MaxLength: 15
MinLength: 2
Type: String
Resources:
# VpcName - VPC for the entire demo
VpcName:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.200.0.0/16
EnableDnsHostnames: 'true'
EnableDnsSupport: 'true'
Tags:
- Key: Name
Value:
!Join ["", [ Ref: NamePrefix, "-vpc"] ]
# VpcInternetGateway: Internet gateway for the VPC
VpcInternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value:
!Join ["", [ Ref: NamePrefix, "-igw"] ]
# VpcInternetGatewayAttachment - attach the internet gateway to the VPC
# Note that several resources have a "DependsOn" key for this
# attachment (e.g. routes, EC2, etc.) to inform CloudFormation of the
# relationship.
VpcInternetGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref VpcInternetGateway
VpcId: !Ref VpcName
# The single subnet in the VPC. For simplicity, all hosts will receive
# a public IP.
PublicSubnet:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Ref AZName
CidrBlock: 10.200.11.0/24
MapPublicIpOnLaunch: 'true'
Tags:
- Key: Name
Value:
!Join ["", [ Ref: NamePrefix, "-public-sn"] ]
VpcId: !Ref VpcName
# VpcRouteTable - The routing table for the VPC
VpcRouteTable:
Type: AWS::EC2::RouteTable
Properties:
Tags:
- Key: Name
Value:
!Join ["", [ Ref: NamePrefix, "-rtb"] ]
VpcId: !Ref VpcName
# DefaultRoute - Add the default route to the Internet gateway. The
# gateway must be attached to the VPC before this can be done, hence
# the "DependsOn" key.
DefaultRoute:
Type: AWS::EC2::Route
DependsOn: VpcInternetGatewayAttachment
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref VpcInternetGateway
RouteTableId: !Ref VpcRouteTable
# PublicSubenetRtb: Add the public subnet to the route table
PublicSubnetRtb:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref VpcRouteTable
SubnetId: !Ref PublicSubnet
# BlueGrenApp - CodeDeploy application
BlueGreenApp:
Type: AWS::CodeDeploy::Application
Properties:
ApplicationName: !Join ["", [ Ref: NamePrefix, "-app"] ]
# BlueGreenDG - CodeDeploy deployment group
BlueGreenDG:
Type: AWS::CodeDeploy::DeploymentGroup
Properties:
ApplicationName: !Ref BlueGreenApp
DeploymentGroupName: !Join ["", [ Ref: NamePrefix, "-dg"] ]
ServiceRoleArn: !GetAtt BlueGreenDeployRole.Arn
# BlueGreenBucket - an S3 bucket that will be used for CodeDeploy revisions
BlueGreenBucket:
Type: AWS::S3::Bucket
Properties:
Tags:
- Key: Name
Value:
!Join ["", [ Ref: NamePrefix, "-deploy-bucket"] ]
# BlueGreenWorkStationRole - The instance role for the workstation host
#
# The workstation host will need to have all of the permissions needed for the
# user to run CodeDeploy. You can find this list at the link below.
#
# http://docs.aws.amazon.com/codedeploy/latest/userguide/
# instances-ec2-create-cloudformation-template.html
BlueGreenWorkStationRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Version: '2012-10-17'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforAWSCodeDeploy
Policies:
- PolicyName: "root"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- autoscaling:*
- codedeploy:*
- ec2:*
- elasticloadbalancing:*
- iam:AddRoleToInstanceProfile
- iam:CreateInstanceProfile
- iam:CreateRole
- iam:DeleteInstanceProfile
- iam:DeleteRole
- iam:DeleteRolePolicy
- iam:GetInstanceProfile
- iam:GetRole
- iam:GetRolePolicy
- iam:ListInstanceProfilesForRole
- iam:ListRolePolicies
- iam:ListRoles
- iam:PassRole
- iam:PutRolePolicy
- iam:RemoveRoleFromInstanceProfile
- s3:*
Resource: "*"
# BlueGreenWorkStationProfile - Instance profile for the workstation role
BlueGreenWorkStationProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- Ref: BlueGreenWorkStationRole
# BlueGreenWorkStationSecurity Group
#
# This is the security group for the workstation. It only needs SSH access.
BlueGreenWorkStationSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access via port 22
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: '22'
IpProtocol: tcp
ToPort: '22'
Tags:
- Key: Name
Value:
!Join ["", [ Ref: NamePrefix, "-workstation-sg"] ]
VpcId: !Ref VpcName
# BlueGreenWorkStation - Create the CodeDeploy workstation
BlueGreenWorkStation:
Type: "AWS::EC2::Instance"
DependsOn: VpcInternetGatewayAttachment
Metadata:
AWS::CloudFormation::Init:
configSets:
default:
- ubuntu1
ubuntu1:
commands:
ubuntu1cmd3:
command: chown -R ubuntu:ubuntu /home/ubuntu
files:
#
# cfn-hup.conf - for the CloudFormation helper scripts
#
/etc/cfn/cfn-hup.conf:
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
group: root
mode: '000400'
owner: root
#
# cfn-auto-reloader.conf - for the CloudFormation helper scripts
#
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.BlueGreenLaunchConfig.Metadata.AWS::CloudFormation::Init
action=/usr/local/bin/cfn-init -v -s ${AWS::StackId} -r BlueGreenWorkStation --region ${AWS::Region}
runas=root
group: root
mode: '000400'
owner: root
#
# updatedeploymentgroup.sh
#
# This script is run after cfn-init to change the settings of the
# deployment group to be BlueGreen. CloudFormation does not
# have currently support the creation of Blue/Green deployment
# groups. For this reason CloudFormation is used to create an "in
# place" deployment group and then we use updatedeploymentgroup.sh
# to update it to BlueGreen. This allows CloudFormation to delete
# the deployment group when the stack is deleted.
#
/home/ubuntu/scripts/updatedeploymentgroup.sh:
content: !Sub |
#!/bin/bash
DeployType="BLUE_GREEN,deploymentOption=WITH_TRAFFIC_CONTROL"
BlueGreenConf=\
"terminateBlueInstancesOnDeploymentSuccess={action=KEEP_ALIVE}"\
",deploymentReadyOption={actionOnTimeout=CONTINUE_DEPLOYMENT}"\
",greenFleetProvisioningOption={action=COPY_AUTO_SCALING_GROUP}"
aws deploy update-deployment-group \
--application-name "${BlueGreenApp}" \
--current-deployment-group-name "${BlueGreenDG}" \
--service-role-arn "${BlueGreenDeployRole.Arn}" \
--auto-scaling-groups "${BlueGreenASG}" \
--load-balancer-info elbInfoList=[{name="${BlueGreenLB}"}] \
--deployment-style "deploymentType=$DeployType" \
--blue-green-deployment-configuration "$BlueGreenConf" >/dev/null
group: ubuntu
mode: '000755'
owner: ubuntu
#
# appspec.yml
#
# This file is used to tell CodeDeploy how to deploy the new
# version
#
/home/ubuntu/newversion/appspec.yml:
content: !Sub |
version: 0.0
os: linux
files:
- source: content/index.php
destination: /var/www/html
permissions:
- object: /var/www/html/index.php
owner: ubuntu
group: ubuntu
mode: 644
hooks:
BeforeInstall:
- location: scripts/beforeinstall.sh
timeout: 180
group: ubuntu
mode: '000644'
owner: ubuntu
#
# index.php - Version 2 of the CodeDeploy application
#
# This is the new version of the app that we will deploy using
# code deploy. It's just a PHP app that displays "Version 2"
# along with the private IP and instance ID of the instance.
#
/home/ubuntu/newversion/content/index.php:
content: !Sub |
<?php
$i_id = file_get_contents("http://instance-data/latest/meta-data/instance-id");
$i_ipv4 = file_get_contents("http://instance-data/latest/meta-data/local-ipv4");
echo "<h1>application version: 2</h1>";
echo "<h1>instance-id: ", $i_id, "</h1>";
echo "<h1>local-ipv4: ", $i_ipv4, "</h1>";
?>
group: ubuntu
mode: '000755'
owner: ubuntu
#
# beforeinstall.sh - CodeDeploy BeforeInstall hook file
#
# This will be called by CodeDeploy during the BeforeInstall phase.
# It removes the existing version of the index.php file since
# CodeDeploy will not overwrite existing files.
#
/home/ubuntu/newversion/scripts/beforeinstall.sh:
content: !Sub |
#!/bin/bash
rm -f /var/www/html/index.php
group: ubuntu
mode: '000755'
owner: ubuntu
/home/ubuntu/scripts/pushnewversion.sh:
content: !Sub |
#!/bin/bash
aws deploy push \
--application-name ${BlueGreenApp} \
--description "new version" \
--ignore-hidden-files \
--s3-location s3://${BlueGreenBucket}/newversion.zip \
--source /home/ubuntu/newversion
group: ubuntu
mode: '000755'
owner: ubuntu
#
# cleanup.sh
#
# This is used to remove the code deployment bundle from S3
# as well as the autoscaling groups that were created. The
# reason this is being done is that CloudFormation can only
# clean up what it knows about. If these resources do not
# get manually deleted, CloudFormation cannot remove the
# entire stack.
#
/home/ubuntu/scripts/cleanup.sh:
content: !Sub |
#!/bin/bash
echo Removing deployment bundle from S3...
aws s3 rm s3://${BlueGreenBucket}/newversion.zip
echo Removing additional autoscaling group...
aws autoscaling describe-auto-scaling-groups \
--query \
'AutoScalingGroups[].[AutoScalingGroupName,VPCZoneIdentifier]' \
--output text | \
grep ${PublicSubnet} | \
cut -f1 | \
xargs -n1 aws autoscaling delete-auto-scaling-group \
--force-delete \
--auto-scaling-group-name
group: ubuntu
mode: '000755'
owner: ubuntu
Properties:
AvailabilityZone: !Ref AZName
IamInstanceProfile: !Ref BlueGreenWorkStationProfile
ImageId:
Fn::FindInMap:
- RegionMap
- Ref: AWS::Region
- ubuntu
InstanceType: t2.micro
KeyName: !Ref BlueGreenKeyPairName
SecurityGroupIds:
- Ref: BlueGreenWorkStationSecurityGroup
SubnetId: !Ref PublicSubnet
Tags:
- Key: Name
Value:
!Join ["", [ Ref: NamePrefix, "-workstation"] ]
UserData:
Fn::Base64: !Sub |
#!/bin/bash
function error_exit
{
cfn-signal -e 1 -r "$1" --stack ${AWS::StackId} --resource BlueGreenWorkStation --region ${AWS::Region}
exit 1
}
apt-get -y update
apt-get -y install python-setuptools python-pip software-properties-common
pip install boto3
pip install awscli
pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
ln -s /root/aws-cfn-bootstrap-latest/init/ubuntu/cfn-hup /etc/init.d/cfn-hup
cfn-init --region ${AWS::Region} -s ${AWS::StackId} -r BlueGreenWorkStation
sudo -u ubuntu aws configure set region ${AWS::Region}
sudo -u ubuntu /home/ubuntu/scripts/updatedeploymentgroup.sh
cfn-hup || error_exit ''Failed to start cfn-hup''
cfn-signal -e 0 -r 'BlueGreen configuration' --stack ${AWS::StackId} --resource BlueGreenWorkStation --region ${AWS::Region}
CreationPolicy:
ResourceSignal:
Count: '1'
Timeout: PT30M
# BlueGreenDeployRole: This is the role that will be used by the
# CodeDeploy service. Its sole purpose is to provide access to
# the CodeDeploy service. It is not used by the EC2 instances
# themselves.
BlueGreenDeployRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- codedeploy.amazonaws.com
Version: '2012-10-17'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole
BlueGreenInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- Ref: BlueGreenInstanceRole
BlueGreenInstanceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Version: '2012-10-17'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforAWSCodeDeploy
# BlueGreenLB
#
# This is the load balancer that will be used to support the BlueGreen
# environment.
BlueGreenLB:
Type: AWS::ElasticLoadBalancing::LoadBalancer
DependsOn: VpcInternetGatewayAttachment
Properties:
ConnectionSettings:
IdleTimeout: 1
HealthCheck:
HealthyThreshold: '2'
Interval: '15'
Target: TCP:80
Timeout: '5'
UnhealthyThreshold: '2'
Listeners:
- InstancePort: '80'
InstanceProtocol: TCP
LoadBalancerPort: '80'
Protocol: TCP
Scheme: internet-facing
SecurityGroups:
- Ref: BlueGreenWebSecurityGroup
Subnets:
- Ref: PublicSubnet
Tags:
- Key: Name
Value:
!Join ["", [ Ref: NamePrefix, "-elb"] ]
# BlueGreenASG
#
# This is the autoscaling group for the Blue/Green environment.
# This will be used for version 1 of the code. When version 2 is
# deployed, CodeDeploy will copy this autoscaling group and attach
# new instances to it.
BlueGreenASG:
DependsOn: VpcInternetGatewayAttachment
Properties:
Cooldown: '60'
LaunchConfigurationName:
Ref: BlueGreenLaunchConfig
LoadBalancerNames:
- Ref: BlueGreenLB
MaxSize: '2'
MinSize: '2'
Tags:
- Key: Name
PropagateAtLaunch: 'true'
Value:
!Join ["", [ Ref: NamePrefix, "-asg"] ]
VPCZoneIdentifier:
- Ref: PublicSubnet
CreationPolicy:
ResourceSignal:
Count: '2'
Timeout: PT30M
Type: AWS::AutoScaling::AutoScalingGroup
# BlueGreenWebSecurityGroup
#
# This is the security group for the instances created
# by the launch configuration. Only ports 22 and 80
# are needed.
BlueGreenWebSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable access to web servers
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: '22'
IpProtocol: tcp
ToPort: '22'
- CidrIp: 0.0.0.0/0
FromPort: '80'
IpProtocol: tcp
ToPort: '80'
Tags:
- Key: Name
Value:
!Join ["", [ Ref: NamePrefix, "-web-sg"] ]
VpcId: !Ref VpcName
# BlueGreenLaunchConfig
#
# This is the launch configuration for the autoscaling group.
# The metadata will be used to install version 1 of the
# application and to set up the CodeDeploy agent. It also
# removes the index.html file which has a "higher" priority
# in the Apache server.
BlueGreenLaunchConfig:
Type: AWS::AutoScaling::LaunchConfiguration
DependsOn: VpcInternetGatewayAttachment
Metadata:
AWS::CloudFormation::Init:
configSets:
default:
- ubuntu1
ubuntu1:
files:
/etc/cfn/cfn-hup.conf:
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
group: root
mode: '000400'
owner: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.BlueGreenLaunchConfig.Metadata.AWS::CloudFormation::Init
action=/usr/local/bin/cfn-init -v -s ${AWS::StackId} -r BlueGreenLaunchConfig --region ${AWS::Region}
runas=root
group: root
mode: '000400'
owner: root
#
# codedeployinstall
#
# This copies the code deploy installation script from the AWS S3 buckets.
#
/home/ubuntu/codedeployinstall:
group: ubuntu
mode: '000755'
owner: ubuntu
source:
!Join [ "", [ "http://aws-codedeploy-", !Ref "AWS::Region",".s3.amazonaws.com/latest/install" ] ]
#
# codedeployinstallpt2
#
# This actually installs the code deploy agent. A script is used
# because we need ingest a "yes" into the installation process.
# This will be run in the command section below.
#
/home/ubuntu/codedeployinstallpt2:
content: !Sub |
#!/bin/bash
yes|/home/ubuntu/codedeployinstall auto
group: ubuntu
mode: '000755'
owner: ubuntu
#
# index.php
#
# This is the first version of the application. It displays
# "version 1" along with the instance id and the private IP
# of the instance from the EC2 metadata.
#
/var/www/html/index.php:
content: !Sub |
<?php
$i_id = file_get_contents("http://instance-data/latest/meta-data/instance-id");
$i_ipv4 = file_get_contents("http://instance-data/latest/meta-data/local-ipv4");
echo "<h1>application version: 1</h1>";
echo "<h1>instance-id: ", $i_id, "</h1>";
echo "<h1>local-ipv4: ", $i_ipv4, "</h1>";
?>
group: ubuntu
mode: '000755'
owner: ubuntu
commands:
ubuntu1cmd1:
command: /home/ubuntu/codedeployinstallpt2
ubuntu1cmd2:
command: rm /var/www/html/index.html
packages:
apt:
apache2: []
libapache2-mod-php: []
php: []
services:
sysvinit:
httpd:
enabled: true
ensureRunning: true
Properties:
IamInstanceProfile: !Ref BlueGreenInstanceProfile
ImageId:
Fn::FindInMap:
- RegionMap
- Ref: AWS::Region
- ubuntu
InstanceType: t2.micro
KeyName: !Ref BlueGreenKeyPairName
SecurityGroups:
- Ref: BlueGreenWebSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash
function error_exit
{
cfn-signal -e 1 -r "$1" --stack ${AWS::StackId} --resource BlueGreenASG --region ${AWS::Region}
exit 1
}
apt-get -y update
apt-get -y install python-setuptools python-pip software-properties-common
pip install boto3
pip install awscli
pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
ln -s /root/aws-cfn-bootstrap-latest/init/ubuntu/cfn-hup /etc/init.d/cfn-hup
# Install the version of Ruby currently supported with CodeDeploy agent.
cat << EOF > /etc/apt/sources.list.d/xenial.list
deb http://archive.ubuntu.com/ubuntu/ xenial main
deb http://archive.ubuntu.com/ubuntu/ xenial-updates main
EOF
cat << EOF > /etc/apt/preferences.d/ruby-xenial
Package: ruby
Pin: release v=16.04, l=Ubuntu
Pin-Priority: 1024
Package: rake
Pin: release v=16.04, l=Ubuntu
Pin-Priority: 1024
EOF
apt-get -y update
apt-get -y install ruby gdebi-core
cfn-init --region ${AWS::Region} -s ${AWS::StackId} -r BlueGreenLaunchConfig
cfn-hup || error_exit ''Failed to start cfn-hup''
cfn-signal -e 0 -r 'BlueGreen configuration' --stack ${AWS::StackId} --resource BlueGreenASG --region ${AWS::Region}