-
Notifications
You must be signed in to change notification settings - Fork 0
/
infrastructure.yml
287 lines (261 loc) · 7.76 KB
/
infrastructure.yml
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
Parameters:
VpcId:
Type: AWS::EC2::VPC::Id
Default: vpc-0d5462fe36e408984
InternetGatewayId:
Type: String
Default: igw-02ca961c369600f90
Resources:
CodeRunnerRepository:
Type: AWS::ECR::Repository
Properties:
RepositoryName: coderunner
EmptyOnDelete: true
ImageScanningConfiguration:
ScanOnPush: false
RepositoryPolicyText:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action:
- 'ecr:BatchGetImage'
- 'ecr:DeleteRepositoryPolicy'
- 'ecr:DescribeImages'
- 'ecr:DescribeRepositories'
- 'ecr:GetDownloadUrlForLayer'
- 'ecr:GetRepositoryPolicy'
- 'ecr:ListImages'
- 'ecr:SetRepositoryPolicy'
- 'ecr:BatchCheckLayerAvailability'
- 'ecr:CompleteLayerUpload'
- 'ecr:InitiateLayerUpload'
- 'ecr:PutImage'
- 'ecr:UploadLayerPart'
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub 'coderunner-${AWS::AccountId}'
ClusterCredentials:
Type: AWS::SecretsManager::Secret
Properties:
GenerateSecretString:
SecretStringTemplate: '{"username": "app_user"}'
GenerateStringKey: password
PasswordLength: 30
ExcludeCharacters: '"@/\'
DbCluster:
Type: AWS::RDS::DBCluster
Properties:
DatabaseName: coderunner
DBClusterIdentifier: coderunner-dbcluster
Engine: aurora-postgresql
EngineMode: provisioned
MasterUsername: !Sub '{{resolve:secretsmanager:${ClusterCredentials}::username}}'
MasterUserPassword: !Sub '{{resolve:secretsmanager:${ClusterCredentials}::password}}'
Port: 5432
# PubliclyAccessible: true
VpcSecurityGroupIds:
- !GetAtt RdsSecurityGroup.GroupId
DbInstance:
Type: AWS::RDS::DBInstance
Properties:
DBClusterIdentifier: !Ref DbCluster
DBInstanceClass: db.t3.medium
Engine: aurora-postgresql
RdsSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: rds-sg
GroupDescription: Allow traffic from Lambda to RDS
VpcId: !Ref VpcId
# SecurityGroupIngress:
# - IpProtocol: tcp
# FromPort: 5432
# ToPort: 5432
# CidrIp: Your IP Address
SecurityGroupEgress:
- IpProtocol: -1
FromPort: -1
ToPort: -1
CidrIp: 0.0.0.0/0
RdsSecurityGroupIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !GetAtt RdsSecurityGroup.GroupId
IpProtocol: tcp
FromPort: 5432
ToPort: 5432
SourceSecurityGroupId: !GetAtt LambdaSecurityGroup.GroupId
LambdaSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: lambda-sg
GroupDescription: Allow traffic from Lambda to RDS
VpcId: !Ref VpcId
SecurityGroupEgress:
- IpProtocol: -1
FromPort: -1
ToPort: -1
DestinationSecurityGroupId: !GetAtt RdsSecurityGroup.GroupId
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
LambdaSecurityGroupEgress:
Type: AWS::EC2::SecurityGroupEgress
Properties:
GroupId: !GetAtt LambdaSecurityGroup.GroupId
IpProtocol: tcp
FromPort: 443
ToPort: 443
DestinationSecurityGroupId: !GetAtt SecretsManagerEndpointSecurityGroup.GroupId
LambdaPrivateSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VpcId
CidrBlock: 172.31.96.0/20
AvailabilityZone: us-east-1a
Tags:
- Key: Name
Value: LambdaPrivateSubnet1
LambdaPrivateSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VpcId
CidrBlock: 172.31.112.0/20
AvailabilityZone: us-east-1b
Tags:
- Key: Name
Value: LambdaPrivateSubnet2
LambdaPrivateSubnet3:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VpcId
CidrBlock: 172.31.128.0/20
AvailabilityZone: us-east-1c
Tags:
- Key: Name
Value: LambdaPrivateSubnet3
LambdaPrivateSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref LambdaPrivateSubnet1
RouteTableId: !Ref LambdaPrivateRouteTable
LambdaPrivateSubnet2RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref LambdaPrivateSubnet2
RouteTableId: !Ref LambdaPrivateRouteTable
LambdaPrivateSubnet3RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref LambdaPrivateSubnet3
RouteTableId: !Ref LambdaPrivateRouteTable
LambdaPrivateRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VpcId
Tags:
- Key: Name
Value: LambdaPrivateRouteTable
LambdaPrivateRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref LambdaPrivateRouteTable
NatGatewayId: !Ref NatGateway
DestinationCidrBlock: 0.0.0.0/0
NatGatewayEIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NatGateway:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt NatGatewayEIP.AllocationId
SubnetId: !Ref PublicInternetSubnet1
PublicInternetSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VpcId
CidrBlock: 172.31.144.0/20
AvailabilityZone: us-east-1a
Tags:
- Key: Name
Value: PublicInternetSubnet1
PublicInternetSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicInternetSubnet1
RouteTableId: !Ref PublicInternetRouteTable
PublicInternetRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VpcId
Tags:
- Key: Name
Value: PublicInternetRouteTable
PublicInternetRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PublicInternetRouteTable
GatewayId: !Ref InternetGatewayId
DestinationCidrBlock: 0.0.0.0/0
SecretsManagerEndpointSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: secretsmanager-sg
GroupDescription: Allow traffic from Lambda to SecretsManager
VpcId: !Ref VpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 443
ToPort: 443
SourceSecurityGroupId: !GetAtt LambdaSecurityGroup.GroupId
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 443
ToPort: 443
DestinationSecurityGroupId: !GetAtt LambdaSecurityGroup.GroupId
SecretsManagerEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
ServiceName: !Sub 'com.amazonaws.${AWS::Region}.secretsmanager'
VpcId: !Ref VpcId
PrivateDnsEnabled: true
VpcEndpointType: Interface
SecurityGroupIds:
- !GetAtt SecretsManagerEndpointSecurityGroup.GroupId
SubnetIds:
- !Ref LambdaPrivateSubnet1
- !Ref LambdaPrivateSubnet2
- !Ref LambdaPrivateSubnet3
Outputs:
ClusterCredentials:
Value: !Ref ClusterCredentials
Export:
Name: coderunner-ClusterCredentials
ClusterUrl:
Value: !Join [ ':', [ !GetAtt DbCluster.Endpoint.Address, !GetAtt DbCluster.Endpoint.Port ] ]
Export:
Name: coderunner-ClusterUrl
BucketName:
Value: !Ref Bucket
Export:
Name: coderunner-BucketName
LambdaSecurityGroupId:
Value: !GetAtt LambdaSecurityGroup.GroupId
Export:
Name: coderunner-LambdaSecurityGroupId
SubnetsId:
Value: !Join [
',',
[
!Ref LambdaPrivateSubnet1,
!Ref LambdaPrivateSubnet2,
!Ref LambdaPrivateSubnet3
]
]
Export:
Name: coderunner-SubnetsId