-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfn-exemplo-rnp-app.yml
More file actions
executable file
·164 lines (158 loc) · 5.04 KB
/
cfn-exemplo-rnp-app.yml
File metadata and controls
executable file
·164 lines (158 loc) · 5.04 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
---
AWSTemplateFormatVersion: 2010-09-09
Description: Exemplo simples de infraestrutura como codigo.
Metadata:
Authors:
Description: Carlos Correa (carlos@xtbsolutions.com)
License: MIT
Parameters:
KeyName:
Description: Nome do par de chaves
Type: String
ConstraintDescription: O par de chaves precisa ter sido previamente criado
Default: "carlos-key"
SourceCidr:
Default: 0.0.0.0/0
Description: Range default para acesso SSH
Type: String
Resources:
## Os recursos que vamos criar
## Uma senha para nosso banco de dados
SenhaWordpress:
Type: AWS::SecretsManager::Secret
Properties:
Name: 'SenhaWordpress'
Description: 'Senha para o banco de dados do WordPress.'
GenerateSecretString:
RequireEachIncludedType: True
SecretStringTemplate: '{"username": "wordpress"}'
GenerateStringKey: 'password'
PasswordLength: 30
ExcludeCharacters: '"@/\'
GrupoSubnetRDS:
Type: AWS::RDS::DBSubnetGroup
Properties:
DBSubnetGroupDescription: Grupo de seguranca para o banco
SubnetIds:
- Fn::ImportValue:
!Sub "cfn-exemplo-rnp-vpc-PrivateSubnet1"
- Fn::ImportValue:
!Sub "cfn-exemplo-rnp-vpc-PrivateSubnet2"
RDSSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId:
Fn::ImportValue:
!Sub "cfn-exemplo-rnp-vpc-Vpc"
GroupDescription: Security group para o banco RDS
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '3306'
ToPort: '3306'
CidrIp: 172.17.0.0/16
WordpressRDS:
Type: AWS::RDS::DBInstance
Properties:
AllocatedStorage: 20
BackupRetentionPeriod: 0
DBName: wordpress
DBInstanceClass: db.t2.micro
Engine: mysql
MasterUsername: !Join ['', ['{{resolve:secretsmanager:', !Ref SenhaWordpress, ':SecretString:username}}' ]]
MasterUserPassword: !Join ['', ['{{resolve:secretsmanager:', !Ref SenhaWordpress, ':SecretString:password}}' ]]
DBInstanceIdentifier: 'wordpress'
DBSubnetGroupName: !Ref GrupoSubnetRDS
VPCSecurityGroups:
- !Ref RDSSecurityGroup
SenhaRDSAttachment:
Type: AWS::SecretsManager::SecretTargetAttachment
Properties:
SecretId: !Ref SenhaWordpress
TargetId: !Ref WordpressRDS
TargetType: AWS::RDS::DBInstance
## Cria um security group para nosso servidor de app
AppSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId:
Fn::ImportValue:
!Sub "cfn-exemplo-rnp-vpc-Vpc"
GroupDescription: Gerencia acessos ao servidor de aplicacao
SecurityGroupIngress:
- CidrIp: 172.17.0.0/16
FromPort: 22
IpProtocol: tcp
ToPort: 22
- CidrIp: 0.0.0.0/0
FromPort: 80
IpProtocol: tcp
ToPort: 80
## Cria uma interface de rede para nosso servidor de aplicacao
AppXface:
Type: AWS::EC2::NetworkInterface
Properties:
SubnetId:
Fn::ImportValue:
!Sub "cfn-exemplo-rnp-vpc-PrivateSubnet1"
Description: Interface para o servidor de aplicacao
GroupSet:
- !Ref AppSecurityGroup
SourceDestCheck: false
Tags:
-
Key: Name
Value: AppXface
## Cria nosso servidor de app
Ec2App:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
config:
files:
/etc/cfn/cfn-hup.conf:
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
interval=1
verbose=true
mode: '000400'
owner: root
group: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.Ec2App.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init --stack cfn-exemplo-rnp-app --resource Ec2App --region us-east-1
runas=root
Properties:
ImageId: ami-0947d2ba12ee1ff75
InstanceType: t3a.micro
KeyName: !Ref KeyName
NetworkInterfaces:
-
NetworkInterfaceId: !Ref AppXface
DeviceIndex: 0
Tags:
-
Key: Name
Value: Servidor App
UserData:
Fn::Base64: !Sub |
#!/bin/bash
yum -y update
yum install aws-cfn-bootstrap
/opt/aws/bin/cfn-init --stack cfn-exemplo-rnp-app --resource Ec2App --region us-east-1
systemctl start cfn-hup
systemctl enable cfn-hup
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar xvf latest.tar.gz
yum install -y httpd jq
amazon-linux-extras install -y php7.3
yum install -y php-pecl-mcrypt php-pecl-imagick php-mbstring
systemctl enable httpd
systemctl start httpd
rsync -r /tmp/wordpress/. /var/www/html
chown -R apache:apache /var/www/