1
00:00:02,190 --> 00:00:04,150
So to show you how to work with that.

2
00:00:04,150 --> 00:00:06,570
I will now switch to MongoDB Atlas

3
00:00:06,570 --> 00:00:10,190
and use that instead of my own Mongo container.

4
00:00:10,190 --> 00:00:11,760
And we can switch easily

5
00:00:11,760 --> 00:00:14,720
because getting started with it is free.

6
00:00:14,720 --> 00:00:16,470
You don't need to pay initially,

7
00:00:16,470 --> 00:00:20,060
but of course, as always and as before with AWS,

8
00:00:20,060 --> 00:00:22,710
you always want to check out the pricing pages

9
00:00:22,710 --> 00:00:26,070
to learn more about pricing as you grow.

10
00:00:26,070 --> 00:00:29,730
And as you want to work with that in production,

11
00:00:29,730 --> 00:00:32,060
but getting started is possible for free.

12
00:00:32,060 --> 00:00:34,130
We can simply click on start free,

13
00:00:34,130 --> 00:00:37,293
fill in the details here and create our account.

14
00:00:39,030 --> 00:00:41,250
Now, if you just created your account

15
00:00:41,250 --> 00:00:43,830
you might see some getting started screen.

16
00:00:43,830 --> 00:00:45,580
I already had an account

17
00:00:45,580 --> 00:00:49,000
and either way you should find a button to create

18
00:00:49,000 --> 00:00:50,500
or build a new cluster

19
00:00:50,500 --> 00:00:52,540
because that's what we're going to do.

20
00:00:52,540 --> 00:00:55,150
And the clusters here have nothing to do

21
00:00:55,150 --> 00:00:58,030
with the AWS, ECS clusters.

22
00:00:58,030 --> 00:00:59,710
It's just a similar term

23
00:00:59,710 --> 00:01:01,730
because again, we can have a cluster

24
00:01:01,730 --> 00:01:05,319
with multiple MongoDB databases inside of it.

25
00:01:05,319 --> 00:01:07,380
So we are going to create a cluster here

26
00:01:07,380 --> 00:01:09,620
and we are going to use the shared cluster

27
00:01:09,620 --> 00:01:11,493
because that's available for free.

28
00:01:12,440 --> 00:01:14,820
Now here, you still choose a cloud provider

29
00:01:14,820 --> 00:01:16,580
because under the hood

30
00:01:16,580 --> 00:01:19,600
MongoDB Atlas also uses a cloud provider

31
00:01:19,600 --> 00:01:21,960
but you don't have to use AWS here

32
00:01:21,960 --> 00:01:23,880
just because we used it before.

33
00:01:23,880 --> 00:01:26,280
You don't need to log into these providers.

34
00:01:26,280 --> 00:01:28,640
You don't even need an account with them.

35
00:01:28,640 --> 00:01:31,890
MongoDB will work with them under the hood.

36
00:01:31,890 --> 00:01:33,390
I will stick to AWS,

37
00:01:33,390 --> 00:01:35,660
but you could choose any cloud provider.

38
00:01:35,660 --> 00:01:37,870
You just want to make sure that ultimately

39
00:01:37,870 --> 00:01:39,660
you end up with a combination,

40
00:01:39,660 --> 00:01:42,080
which you can launch for free.

41
00:01:42,080 --> 00:01:44,430
And I will go with AWS

42
00:01:44,430 --> 00:01:48,930
stay with the default region here on the cluster tier.

43
00:01:48,930 --> 00:01:52,350
I will use the M0 sandbox, which is free.

44
00:01:52,350 --> 00:01:54,743
So which we can get started with for free.

45
00:01:55,700 --> 00:01:59,220
On additional settings, I will leave the defaults here

46
00:01:59,220 --> 00:02:01,980
and on cluster name I will also keep the default

47
00:02:01,980 --> 00:02:04,400
though of course you can rename this

48
00:02:04,400 --> 00:02:07,400
and then we can simply create this cluster.

49
00:02:07,400 --> 00:02:09,479
And this will now take a short while

50
00:02:09,479 --> 00:02:11,950
and it will set up all the cloud resources

51
00:02:11,950 --> 00:02:13,870
under the hood behind the scenes.

52
00:02:13,870 --> 00:02:17,090
And it will give us a cloud MongoDB database

53
00:02:17,090 --> 00:02:18,653
to which we can connect.

54
00:02:20,270 --> 00:02:24,280
Now, once the cluster was created and is up and running,

55
00:02:24,280 --> 00:02:26,120
you can click on connect here

56
00:02:26,120 --> 00:02:28,380
to learn how to connect to this cluster.

57
00:02:28,380 --> 00:02:30,340
And we want to connect an application

58
00:02:31,980 --> 00:02:33,950
because we got an application here.

59
00:02:33,950 --> 00:02:36,480
We got our backend node rest API,

60
00:02:36,480 --> 00:02:38,530
which is talking to MongoDB,

61
00:02:38,530 --> 00:02:41,650
and that's the application we want to connect.

62
00:02:41,650 --> 00:02:43,570
Currently, we're manually connecting

63
00:02:43,570 --> 00:02:45,840
to our MongoDB container

64
00:02:45,840 --> 00:02:49,853
but now we want to connect to this cloud MongoDB service.

65
00:02:50,770 --> 00:02:51,960
Now the cool thing is,

66
00:02:51,960 --> 00:02:54,590
here you see that connection string,

67
00:02:54,590 --> 00:02:55,930
which you should use now.

68
00:02:55,930 --> 00:02:59,660
And it's very similar to the string we used before

69
00:02:59,660 --> 00:03:01,870
there we used MongoDB

70
00:03:01,870 --> 00:03:05,880
then double slashes here, then our username our password

71
00:03:05,880 --> 00:03:10,880
the URL or domain name, the port and then the database name

72
00:03:11,440 --> 00:03:13,380
and this query parameter.

73
00:03:13,380 --> 00:03:16,417
Now it's MongoDB plus SRV

74
00:03:16,417 --> 00:03:17,760
at the beginning,

75
00:03:17,760 --> 00:03:19,630
then still username and password

76
00:03:19,630 --> 00:03:22,500
still a domain name in the end here

77
00:03:22,500 --> 00:03:27,200
then the database name and then this query parameter

78
00:03:27,200 --> 00:03:30,030
which is a different one than before

79
00:03:30,030 --> 00:03:33,600
but overall, we can simply copy this connection string

80
00:03:33,600 --> 00:03:36,930
and use that instead of the existing one.

81
00:03:36,930 --> 00:03:40,140
And now we have a general decision to make.

82
00:03:40,140 --> 00:03:42,670
Do we want to use MongoDB Atlas

83
00:03:42,670 --> 00:03:46,430
in production only, or also during development?

84
00:03:46,430 --> 00:03:49,880
Now you can find arguments for both approaches.

85
00:03:49,880 --> 00:03:53,310
We could use a MongoDB container during development

86
00:03:53,310 --> 00:03:55,900
and MongoDB Atlas during production

87
00:03:55,900 --> 00:03:59,950
so that we don't hit our cloud MongoDB database

88
00:03:59,950 --> 00:04:02,680
whilst we're working on the application

89
00:04:02,680 --> 00:04:05,040
whilst we're testing it.

90
00:04:05,040 --> 00:04:07,270
Though we could also solve that problem

91
00:04:07,270 --> 00:04:10,130
by simply connecting to a different database here

92
00:04:10,130 --> 00:04:12,890
in development than we do in production

93
00:04:12,890 --> 00:04:15,100
so that we don't work with the production data

94
00:04:15,100 --> 00:04:16,390
in development.

95
00:04:16,390 --> 00:04:19,579
So we don't necessarily need a container in development

96
00:04:19,579 --> 00:04:22,830
just to avoid working with the production data

97
00:04:22,830 --> 00:04:23,810
in the database.

98
00:04:23,810 --> 00:04:25,390
Using a different database

99
00:04:25,390 --> 00:04:29,330
on one at the same MongoDB cluster or instance

100
00:04:29,330 --> 00:04:31,100
would be a solution for that.

101
00:04:31,100 --> 00:04:32,470
Nonetheless, we could argue

102
00:04:32,470 --> 00:04:34,850
that we want to use a container during development

103
00:04:34,850 --> 00:04:38,580
for this fully isolated development only environment

104
00:04:38,580 --> 00:04:42,163
and then use the cloud database for production.

105
00:04:43,080 --> 00:04:44,220
But that of course would mean

106
00:04:44,220 --> 00:04:46,930
that we don't have exactly the same environment

107
00:04:46,930 --> 00:04:49,970
during development and during production.

108
00:04:49,970 --> 00:04:51,550
Because it is worth pointing out

109
00:04:51,550 --> 00:04:53,700
that our MongoDB cluster here

110
00:04:53,700 --> 00:04:56,190
uses a certain version of MongoDB.

111
00:04:56,190 --> 00:04:58,603
In my case, 4.2.9.

112
00:04:59,750 --> 00:05:02,650
This means that when we want to use a container

113
00:05:02,650 --> 00:05:03,770
during development.

114
00:05:03,770 --> 00:05:08,010
We have to ensure that we use the exact same version here.

115
00:05:08,010 --> 00:05:12,560
So we must fetch the exact same version during development.

116
00:05:12,560 --> 00:05:13,810
And we should avoid,

117
00:05:13,810 --> 00:05:16,470
that we, for example use the Mongo container

118
00:05:16,470 --> 00:05:19,150
based on Mongo 4.4,

119
00:05:19,150 --> 00:05:20,900
in my specific case here

120
00:05:20,900 --> 00:05:24,000
because then we would be using a later version of Mongo

121
00:05:24,000 --> 00:05:25,150
during development.

122
00:05:25,150 --> 00:05:27,080
Than we do in production.

123
00:05:27,080 --> 00:05:29,450
And this might cause us to accidentally

124
00:05:29,450 --> 00:05:32,620
use some NextGen MongoDB feature

125
00:05:32,620 --> 00:05:34,590
which works fine during development

126
00:05:34,590 --> 00:05:38,100
but won't work once we use this MongoDB cloud service

127
00:05:38,100 --> 00:05:39,310
in production.

128
00:05:39,310 --> 00:05:42,850
And that's defeats the idea behind using containers

129
00:05:42,850 --> 00:05:45,860
that we have to same environment in development

130
00:05:45,860 --> 00:05:48,050
as we have it in production.

131
00:05:48,050 --> 00:05:50,990
So that is something we would have to watch out for.

132
00:05:50,990 --> 00:05:55,440
We always would need to check our production MongoDB version

133
00:05:55,440 --> 00:05:57,880
and ensure that we use the same version

134
00:05:57,880 --> 00:06:00,160
for our development container.

135
00:06:00,160 --> 00:06:02,430
If we do that though, it would be fine

136
00:06:02,430 --> 00:06:06,370
and we could absolutely use a container during development

137
00:06:06,370 --> 00:06:09,790
and the cloud database during production.

138
00:06:09,790 --> 00:06:12,290
We then just would need to find a different way

139
00:06:12,290 --> 00:06:16,040
of constructing this string to stay like this.

140
00:06:16,040 --> 00:06:17,710
When we talk to a container

141
00:06:17,710 --> 00:06:19,810
and to look like

142
00:06:19,810 --> 00:06:22,620
this, when we talk to the production database

143
00:06:22,620 --> 00:06:24,840
but with help of environment variables

144
00:06:24,840 --> 00:06:26,863
that shouldn't be a great challenge.

145
00:06:27,890 --> 00:06:31,470
So long story short, you can find arguments for both.

146
00:06:31,470 --> 00:06:33,090
But I think it's better

147
00:06:33,090 --> 00:06:36,090
if we really use the same

148
00:06:36,090 --> 00:06:38,330
the exact same environment

149
00:06:38,330 --> 00:06:40,610
in development and production

150
00:06:40,610 --> 00:06:45,610
and therefore, I always want to use my cloud service here.

151
00:06:45,660 --> 00:06:48,560
So that means that this will be my connection string

152
00:06:48,560 --> 00:06:50,780
and I will there for now use that.

153
00:06:50,780 --> 00:06:54,900
I will copy this query parameter here at the end

154
00:06:54,900 --> 00:06:57,510
and replace my current query parameter

155
00:06:57,510 --> 00:06:59,163
in the connection string with it,

156
00:07:01,080 --> 00:07:05,740
I will copy this protocol here at the beginning

157
00:07:05,740 --> 00:07:08,943
and use that instead of just Mongo DB.

158
00:07:09,797 --> 00:07:13,150
And now we don't need a port anymore.

159
00:07:13,150 --> 00:07:14,410
So we can remove that

160
00:07:15,500 --> 00:07:17,610
because this is not part of the connection string

161
00:07:17,610 --> 00:07:18,493
we see here.

162
00:07:20,010 --> 00:07:23,400
I now want to make that database name flexible though

163
00:07:23,400 --> 00:07:25,440
so that we can use a different database

164
00:07:25,440 --> 00:07:27,530
for development and for production

165
00:07:27,530 --> 00:07:30,720
so that we don't accidentally override production data.

166
00:07:30,720 --> 00:07:32,140
When we are playing around

167
00:07:32,140 --> 00:07:34,300
with our application in development.

168
00:07:34,300 --> 00:07:37,160
So here we can introduce a new environment variable

169
00:07:37,160 --> 00:07:40,480
and maybe name this MongoDB

170
00:07:42,120 --> 00:07:43,590
database

171
00:07:43,590 --> 00:07:47,563
or simply MongoDB name, whatever you want.

172
00:07:48,890 --> 00:07:51,640
Now, this environment variable will also be registered

173
00:07:51,640 --> 00:07:53,590
in the Docker file

174
00:07:53,590 --> 00:07:57,170
and I'll set the default value here of goals-dev

175
00:07:58,240 --> 00:08:01,460
so that my default database name is goals-dev.

176
00:08:01,460 --> 00:08:04,660
And in production we could override this with just goals

177
00:08:04,660 --> 00:08:08,470
to ensure that we use the right database name.

178
00:08:08,470 --> 00:08:09,980
I will also already set it

179
00:08:09,980 --> 00:08:12,800
in my backend.env file here.

180
00:08:12,800 --> 00:08:15,190
There I will also add this

181
00:08:15,190 --> 00:08:17,570
so that the default in my Docker file

182
00:08:17,570 --> 00:08:19,400
is overwritten with this value,

183
00:08:19,400 --> 00:08:21,410
which of course holds the same value

184
00:08:21,410 --> 00:08:24,963
but simply to show you how you can use this env file.

185
00:08:25,830 --> 00:08:29,260
So now we got this development set up

186
00:08:29,260 --> 00:08:31,500
and our code is now flexible enough

187
00:08:31,500 --> 00:08:34,130
to use the same cloud provider

188
00:08:34,130 --> 00:08:37,330
the same cloud MongoDB database provider

189
00:08:37,330 --> 00:08:40,280
during development and production.

190
00:08:40,280 --> 00:08:41,590
Now, what does this mean

191
00:08:41,590 --> 00:08:44,120
for our deployed containers now though?

192
00:08:44,120 --> 00:08:47,300
And what does it mean for our docker-compose file?

193
00:08:47,300 --> 00:08:49,350
Well, let's maybe start with docker-compose

194
00:08:49,350 --> 00:08:51,740
and with our local setup.

195
00:08:51,740 --> 00:08:55,000
Since we now have our database in the cloud

196
00:08:55,000 --> 00:08:58,930
we no longer launch our own MongoDB container.

197
00:08:58,930 --> 00:08:59,800
So that means,

198
00:08:59,800 --> 00:09:03,500
we can get rid of this MongoDB service here entirely.

199
00:09:03,500 --> 00:09:05,790
We don't need it anymore.

200
00:09:05,790 --> 00:09:07,540
This also means of course,

201
00:09:07,540 --> 00:09:11,410
that in backend.env for the MongoDB URL

202
00:09:11,410 --> 00:09:14,320
we no longer use the MongoDB container name

203
00:09:14,320 --> 00:09:17,420
but instead we'll use this URL here

204
00:09:17,420 --> 00:09:19,490
which we find in this connection string.

205
00:09:19,490 --> 00:09:21,930
So let's add this here,

206
00:09:21,930 --> 00:09:24,200
so that this is the URL we're using.

207
00:09:24,200 --> 00:09:26,713
When we try to connect to MongoDB.

208
00:09:28,680 --> 00:09:31,530
It also means we can remove that volumes part here

209
00:09:31,530 --> 00:09:34,830
in the docker-compose file because that data volume,

210
00:09:34,830 --> 00:09:36,950
which was used by the MongoDB service

211
00:09:36,950 --> 00:09:39,100
is not needed anymore.

212
00:09:39,100 --> 00:09:41,350
And of course we could also delete this code

213
00:09:41,350 --> 00:09:44,520
and we could also delete the Mongo.env file now

214
00:09:44,520 --> 00:09:48,140
because we won't have a MongoDB container anymore.

215
00:09:48,140 --> 00:09:49,790
We're not going to use it anymore

216
00:09:49,790 --> 00:09:53,350
for the reasons I outlined over the last minutes

217
00:09:53,350 --> 00:09:56,800
I will keep it here for reference though, but save it now.

218
00:09:56,800 --> 00:10:00,380
And now we can give this a quick try.

219
00:10:00,380 --> 00:10:04,470
We can simply run docker-compose up here

220
00:10:04,470 --> 00:10:06,700
and see whether that works.

221
00:10:06,700 --> 00:10:11,700
And I get an error because I have this depends on key here.

222
00:10:11,700 --> 00:10:13,540
And of course, I'm trying to depend

223
00:10:13,540 --> 00:10:15,730
on a container which doesn't exist anymore.

224
00:10:15,730 --> 00:10:17,530
So let's remove this too.

225
00:10:17,530 --> 00:10:19,610
And now we can try this again.

226
00:10:19,610 --> 00:10:23,130
So now this builds our updated backend image

227
00:10:23,130 --> 00:10:25,410
and then once it is done building that,

228
00:10:25,410 --> 00:10:27,480
it should bring up that backend service

229
00:10:27,480 --> 00:10:32,210
which then hopefully is still able to connect to MongoDB.

230
00:10:32,210 --> 00:10:35,393
Now, however, with MongoDB in the cloud.

231
00:10:36,770 --> 00:10:39,370
However, this fails

232
00:10:39,370 --> 00:10:43,060
and it fails if we expand this

233
00:10:43,060 --> 00:10:44,630
with an error

234
00:10:44,630 --> 00:10:49,010
that in the end tells us that it could not connect

235
00:10:49,010 --> 00:10:51,110
and it even tells us what the problem could be

236
00:10:51,110 --> 00:10:53,520
because of course, MongoDB Atlas

237
00:10:53,520 --> 00:10:57,090
also has certain security mechanisms.

238
00:10:57,090 --> 00:11:01,200
We can control which IP addresses might connect here.

239
00:11:01,200 --> 00:11:03,420
And we can, of course also add users

240
00:11:03,420 --> 00:11:07,660
and use user credentials to control who's able to connect.

241
00:11:07,660 --> 00:11:10,580
So therefore we of course want to use that

242
00:11:10,580 --> 00:11:15,060
and configure our cluster to control who's able to connect.

243
00:11:15,060 --> 00:11:17,450
And that starts with the network access.

244
00:11:17,450 --> 00:11:20,630
Here I already got a bunch of IP addresses added.

245
00:11:20,630 --> 00:11:23,010
You want to add an IP address here

246
00:11:23,010 --> 00:11:26,410
and either add your current IP address only

247
00:11:26,410 --> 00:11:28,463
or allow access from anywhere,

248
00:11:29,580 --> 00:11:30,670
all due to radar.

249
00:11:30,670 --> 00:11:33,830
So that later also my deployed application

250
00:11:33,830 --> 00:11:37,140
on the AWS servers is able to connect

251
00:11:37,140 --> 00:11:40,743
but you need to white list some IP address here.

252
00:11:41,770 --> 00:11:45,520
So I will allow access from anywhere and confirm this.

253
00:11:45,520 --> 00:11:48,630
But besides that under database access

254
00:11:48,630 --> 00:11:52,990
you can add and configure users for this database.

255
00:11:52,990 --> 00:11:55,670
Now I already got a couple of users here.

256
00:11:55,670 --> 00:11:59,610
You should at least add one new user with a password

257
00:11:59,610 --> 00:12:03,603
and give that user read and write access to the database.

258
00:12:04,650 --> 00:12:08,300
Now, I already did that here for Max,

259
00:12:08,300 --> 00:12:12,510
but I will add a new auto-generated password here.

260
00:12:12,510 --> 00:12:15,630
Which I'll of course delete after this video recording.

261
00:12:15,630 --> 00:12:17,140
So you don't have to try it .

262
00:12:17,140 --> 00:12:19,890
I'll lock down this database in general

263
00:12:19,890 --> 00:12:21,860
and copy that

264
00:12:21,860 --> 00:12:24,140
and click update user.

265
00:12:24,140 --> 00:12:26,700
This user is even an admin.

266
00:12:26,700 --> 00:12:30,570
So therefore let me actually grab another user here

267
00:12:30,570 --> 00:12:34,260
generate a password, grab that password.

268
00:12:34,260 --> 00:12:38,480
This user has read and write access and update this user.

269
00:12:38,480 --> 00:12:43,470
And now with that, we can go back to our backend.env file

270
00:12:43,470 --> 00:12:46,760
and there, I want to use this new username

271
00:12:46,760 --> 00:12:48,480
in my case Maximilian

272
00:12:48,480 --> 00:12:52,370
and this generated password for connecting.

273
00:12:52,370 --> 00:12:55,080
And now once all these changes have been deployed

274
00:12:55,080 --> 00:12:57,910
on MongoDB Atlas once that is done.

275
00:12:57,910 --> 00:13:00,780
We should be able to connect successfully.

276
00:13:00,780 --> 00:13:04,583
So let's wait for these changes to have an effect there.

277
00:13:06,460 --> 00:13:09,150
Quit this running docker-compose process

278
00:13:10,180 --> 00:13:13,020
and restarted with docker-compose up

279
00:13:13,020 --> 00:13:16,480
and now disconnected to MongoDB.

280
00:13:16,480 --> 00:13:17,313
And therefore

281
00:13:17,313 --> 00:13:20,900
if we quickly test this by accessing local host slash goals.

282
00:13:20,900 --> 00:13:23,740
We get back this empty goals response.

283
00:13:23,740 --> 00:13:26,950
Which proves that we're successfully querying the database.

284
00:13:26,950 --> 00:13:28,820
Of course there is no data in it yet,

285
00:13:28,820 --> 00:13:31,350
but that can change of course.

286
00:13:31,350 --> 00:13:34,770
So that's how we can use MongoDB Atlas in development

287
00:13:34,770 --> 00:13:37,260
and why we might want to consider using it.

288
00:13:37,260 --> 00:13:39,480
Now, the main reason for switching to it

289
00:13:39,480 --> 00:13:41,820
of course, was for production.

290
00:13:41,820 --> 00:13:43,603
So how can we use it there.

