-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws_ec2_codedeploy.yml
More file actions
144 lines (138 loc) · 4.37 KB
/
aws_ec2_codedeploy.yml
File metadata and controls
144 lines (138 loc) · 4.37 KB
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
AWSTemplateFormatVersion: 2010-09-09
Description: This CF Template will create Attach IAM Role to an EC2
Parameters:
KeyName:
Description: EC2 Instance SSH Key
Type: 'AWS::EC2::KeyPair::KeyName'
InstanceType:
Description: EC2 instance specs configuration
Type: String
Default: t2.micro
AllowedValues:
- t2.small
- t2.micro
LatestLinuxAmiId:
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
EnvironmentName:
Description: SDLC Environment Value e.g dev/qa/prod.
Type: String
Resources:
EC2Instance1:
Type: 'AWS::EC2::Instance'
Properties:
InstanceType: !Ref InstanceType
ImageId: !Ref LatestLinuxAmiId
KeyName: !Ref KeyName
UserData:
Fn::Base64:
!Sub |
#!/bin/bash -xe
yum update -y
yum install httpd -y
systemctl enable httpd
systemctl start httpd
yum update -y
yum install -y ruby wget
wget https://aws-codedeploy-eu-west-1.s3.eu-west-1.amazonaws.com/latest/install
chmod +x ./install
./install auto
systemctl enable codedeploy-agent
systemctl start codedeploy-agent
IamInstanceProfile: !Ref CodedeployEC2InstanceProfile
SecurityGroupIds:
- !Ref SSHAccessSG
Tags:
- Key: Name
Value: !Join [ "-", [ !Ref EnvironmentName, 'ec2-instance' ] ]
- Key: SDLC_ENVIRONMENT
Value: !Join [ "-", [ !Ref EnvironmentName, 'application' ] ]
EC2Instance2:
Type: 'AWS::EC2::Instance'
Properties:
InstanceType: !Ref InstanceType
ImageId: !Ref LatestLinuxAmiId
KeyName: !Ref KeyName
UserData:
Fn::Base64:
!Sub |
#!/bin/bash -xe
yum update -y
yum install httpd -y
systemctl enable httpd
systemctl start httpd
yum update -y
yum install -y ruby wget
wget https://aws-codedeploy-eu-west-1.s3.eu-west-1.amazonaws.com/latest/install
chmod +x ./install
./install auto
systemctl enable codedeploy-agent
systemctl start codedeploy-agent
IamInstanceProfile: !Ref CodedeployEC2InstanceProfile
SecurityGroupIds:
- !Ref SSHAccessSG
Tags:
- Key: Name
Value: !Join [ "-", [ !Ref EnvironmentName, 'ec2-instance' ] ]
- Key: SDLC_ENVIRONMENT
Value: !Join [ "-", [ !Ref EnvironmentName, 'application' ] ]
SSHAccessSG:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupName: !Join [ "-", [ !Ref EnvironmentName, 'ec2-sg' ] ]
GroupDescription: Allow SSH access from anywhere
SecurityGroupIngress:
- FromPort: '22'
ToPort: '22'
IpProtocol: tcp
CidrIp: 0.0.0.0/0
- FromPort: '80'
ToPort: '80'
IpProtocol: tcp
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: !Join [ "-", [ !Ref EnvironmentName, 'SSHAccess' ] ]
CodedeployEC2InstanceProfile:
Type: 'AWS::IAM::InstanceProfile'
Properties:
Path: /
Roles:
- !Ref CodedeployEc2InstanceRole
CodeDeployEC2Policy:
Type: 'AWS::IAM::Policy'
Properties:
PolicyName: !Join [ "-", [ !Ref EnvironmentName, CodedeployEc2InstancePolicy ] ]
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: Allow
Action:
- 's3:Get*'
- 's3:List*'
- 'ec2:*'
- 'codedeploy:*'
Resource: "*"
Roles:
- !Ref CodedeployEc2InstanceRole
CodedeployEc2InstanceRole:
Type: 'AWS::IAM::Role'
Properties:
Path: /
RoleName: !Join [ "-", [ !Ref EnvironmentName, CodedeployEc2InstanceRole ] ]
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
Outputs:
EC21:
Description: Command to SSH the EC2 Instance
Value: !Join [ "", [ "ssh -i ",!Ref KeyName,".pem ec2-user@", !GetAtt EC2Instance1.PublicIp] ]
EC22:
Description: Command to SSH the EC2 Instance
Value: !Join [ "", [ "ssh -i ",!Ref KeyName,".pem ec2-user@", !GetAtt EC2Instance2.PublicIp] ]