1
00:00:02,280 --> 00:00:05,640
So now the cluster and the service we started

2
00:00:05,640 --> 00:00:09,660
in ECS was deleted, now we can create a new cluster,

3
00:00:09,660 --> 00:00:14,660
new tasks, new services to run our backend container here.

4
00:00:15,240 --> 00:00:20,240
So for that I'll use this UI, click on Create Cluster

5
00:00:20,750 --> 00:00:24,513
and then choose a Networking only cluster here.

6
00:00:25,630 --> 00:00:30,110
Click Next step, give this cluster a name of your choice,

7
00:00:30,110 --> 00:00:33,423
I'll name it goals-app but this name is totally up to you.

8
00:00:34,770 --> 00:00:38,950
Check Create VPC and keep all the defaults here,

9
00:00:38,950 --> 00:00:43,440
this ensures that AWS sets up private cloud

10
00:00:43,440 --> 00:00:46,600
for this cluster, for all of the containers in the cluster

11
00:00:47,460 --> 00:00:49,320
and click Create.

12
00:00:49,320 --> 00:00:51,330
Now this will create this cluster

13
00:00:51,330 --> 00:00:54,370
and the cluster is just the surrounding network

14
00:00:54,370 --> 00:00:56,690
for your containers thereafter.

15
00:00:56,690 --> 00:01:01,410
So let's wait for this cluster creation to finish,

16
00:01:01,410 --> 00:01:03,783
and this can take a couple of minutes.

17
00:01:05,960 --> 00:01:09,550
Once this finished click on View Cluster

18
00:01:09,550 --> 00:01:13,410
and now we need to add tasks and services here.

19
00:01:13,410 --> 00:01:15,480
Now services are based on tasks

20
00:01:15,480 --> 00:01:17,920
so we need to create a task first.

21
00:01:17,920 --> 00:01:20,740
And we do this under Task Definitions.

22
00:01:20,740 --> 00:01:24,110
Now I already got two task definitions here,

23
00:01:24,110 --> 00:01:26,580
we are going to create a new one though.

24
00:01:26,580 --> 00:01:30,370
So create a new task definition and choose Fargate here

25
00:01:30,370 --> 00:01:33,970
to have this serverless container execution environment

26
00:01:33,970 --> 00:01:37,020
where your containers can scale infinitely

27
00:01:37,020 --> 00:01:39,343
and you only pay for what you need.

28
00:01:40,240 --> 00:01:43,690
Click on Next step and give this task definition name

29
00:01:43,690 --> 00:01:45,833
and I'll name it goals.

30
00:01:48,810 --> 00:01:50,820
On task role you should have

31
00:01:50,820 --> 00:01:54,230
a ECS task execution role available,

32
00:01:54,230 --> 00:01:56,043
make sure you choose that one.

33
00:01:58,230 --> 00:02:01,930
If you don't have that, you should delete the entire cluster

34
00:02:01,930 --> 00:02:06,120
again and launch at first run wizard again and go through

35
00:02:06,120 --> 00:02:09,573
that instead because this will then create this role.

36
00:02:10,430 --> 00:02:14,280
Scroll down and choose the smallest sizes here

37
00:02:14,280 --> 00:02:18,440
for the memory and CPU to keep costs down.

38
00:02:18,440 --> 00:02:21,810
Of course for real apps you might wanna choose

39
00:02:21,810 --> 00:02:25,610
more powerful settings here to speed up your code execution,

40
00:02:25,610 --> 00:02:28,800
it depends on the app you're building and it will take time

41
00:02:28,800 --> 00:02:31,610
and experience to find out what you need here.

42
00:02:31,610 --> 00:02:35,360
And then on container definitions click on Add container.

43
00:02:35,360 --> 00:02:37,250
Here give your a container a name

44
00:02:37,250 --> 00:02:40,950
and I'll name it goals-node because that will be the backend

45
00:02:40,950 --> 00:02:44,670
or maybe therefore name it goals-backend container.

46
00:02:44,670 --> 00:02:49,590
And then use this repository here,

47
00:02:49,590 --> 00:02:53,123
this image name academind/goals-node in my case.

48
00:02:54,200 --> 00:02:58,470
On Port mappings make sure you map port 80

49
00:02:58,470 --> 00:03:02,660
because that is the port exposed by this application,

50
00:03:02,660 --> 00:03:07,660
we're listening on port 80 here in our node JS code.

51
00:03:11,410 --> 00:03:15,140
Ignore the health check but here under environment,

52
00:03:15,140 --> 00:03:17,263
I now do wanna change something.

53
00:03:18,130 --> 00:03:20,150
It is worth pointing out that

54
00:03:20,150 --> 00:03:22,900
in hour docker file for this backend image,

55
00:03:22,900 --> 00:03:27,900
we are actually executing npm start here as a final command.

56
00:03:28,160 --> 00:03:32,660
And that in turn will then utilize nodemon

57
00:03:33,530 --> 00:03:35,650
to start app js.

58
00:03:35,650 --> 00:03:40,650
Now I used nodemon locally to have this life reloading

59
00:03:40,890 --> 00:03:44,090
when we change something in our code.

60
00:03:44,090 --> 00:03:46,720
Now in production we won't have that,

61
00:03:46,720 --> 00:03:50,690
we also won't have to bind mounts to feed any live code

62
00:03:50,690 --> 00:03:51,970
into the running app,

63
00:03:51,970 --> 00:03:56,120
I talked about this early already, why we don't do that.

64
00:03:56,120 --> 00:03:59,400
So therefore we of course don't wanna use nodemon

65
00:03:59,400 --> 00:04:02,420
to execute our code here, we wanna use node.

66
00:04:02,420 --> 00:04:07,240
So therefore here on command I'll enter node app js.

67
00:04:07,240 --> 00:04:10,260
There should be a comma separated list here,

68
00:04:10,260 --> 00:04:13,200
cd hint here if nothing was entered

69
00:04:13,200 --> 00:04:17,930
and they offer node, app js will execute the app js file

70
00:04:17,930 --> 00:04:21,820
inside of the container with the node runtime.

71
00:04:21,820 --> 00:04:23,790
And that is a change we should make here

72
00:04:23,790 --> 00:04:28,790
and that also shows us how we again can leverage the exact

73
00:04:28,870 --> 00:04:32,640
same image and container during development and production

74
00:04:32,640 --> 00:04:36,350
and still find tune individual aspects

75
00:04:36,350 --> 00:04:39,810
to the different environments where we run the container,

76
00:04:39,810 --> 00:04:43,860
which is not a problem but actually a feature of docker.

77
00:04:43,860 --> 00:04:48,860
We still have the locked in code and delocked in runtime.

78
00:04:49,980 --> 00:04:53,120
We use node here, that's locked into the image.

79
00:04:53,120 --> 00:04:57,310
So we have that guarantee of having the same application

80
00:04:57,310 --> 00:05:01,100
and application environment in development and production,

81
00:05:01,100 --> 00:05:04,390
that main asset docker is all about.

82
00:05:04,390 --> 00:05:08,060
And still we can find tune individual aspects

83
00:05:08,060 --> 00:05:10,900
that need fine tuning like here

84
00:05:10,900 --> 00:05:13,380
where we use npm start locally

85
00:05:13,380 --> 00:05:16,663
and use node app js in production.

86
00:05:18,250 --> 00:05:21,160
Now we should specify the environment variables.

87
00:05:21,160 --> 00:05:23,250
And here I just noticed something,

88
00:05:23,250 --> 00:05:25,370
in the docker file we should of course

89
00:05:25,370 --> 00:05:28,380
also add this new environment variable,

90
00:05:28,380 --> 00:05:32,450
MONGODB URL environment variable which we're using

91
00:05:32,450 --> 00:05:34,770
in the MONGODB connection string now.

92
00:05:34,770 --> 00:05:38,330
So make sure you add this ENV definition

93
00:05:38,330 --> 00:05:42,760
in your docker file and then actually rebuild

94
00:05:42,760 --> 00:05:46,283
that docker file by running the docker build command again.

95
00:05:47,120 --> 00:05:49,480
And then of course also tag it again

96
00:05:49,480 --> 00:05:53,330
to the academind/goals-node image name in my case.

97
00:05:53,330 --> 00:05:56,100
So to that docker hub repository image,

98
00:05:56,100 --> 00:05:59,720
and then push updated image to ensure

99
00:05:59,720 --> 00:06:03,110
that your docker file lists all the environment variables

100
00:06:03,110 --> 00:06:06,083
which are getting used inside of the container of code.

101
00:06:07,070 --> 00:06:11,510
Now with that, back on AMS ECS we now can specify

102
00:06:11,510 --> 00:06:13,110
the environment variables here.

103
00:06:13,110 --> 00:06:16,730
And that's another important thing, locally

104
00:06:16,730 --> 00:06:20,590
when we run our container locally we use docker compose

105
00:06:20,590 --> 00:06:23,210
to run these multiple containers,

106
00:06:23,210 --> 00:06:28,110
and there we then of course used the env file configuration

107
00:06:28,110 --> 00:06:31,010
to point at an env file.

108
00:06:31,010 --> 00:06:34,420
Alternatively if we use to docker run command,

109
00:06:34,420 --> 00:06:39,280
there we also add an option for adding environment variables

110
00:06:39,280 --> 00:06:42,180
or pointing at an environment variable file.

111
00:06:42,180 --> 00:06:46,493
Here it was the env, the --env file option.

112
00:06:47,450 --> 00:06:49,720
Now we don't have an environment file here

113
00:06:49,720 --> 00:06:53,860
when we use AWS ECS but we can add our environment variables

114
00:06:53,860 --> 00:06:56,280
as key value pairs here.

115
00:06:56,280 --> 00:07:00,630
So therefore I now will go into the backend env file

116
00:07:00,630 --> 00:07:03,590
and grab the environment variables here

117
00:07:03,590 --> 00:07:05,940
and add them as keys.

118
00:07:05,940 --> 00:07:10,940
So MONGODB username, MONGODB password and MONGODB URL

119
00:07:13,180 --> 00:07:16,090
and then add values here.

120
00:07:16,090 --> 00:07:19,730
And I will use the same values as I did during development

121
00:07:19,730 --> 00:07:22,780
though in reality you might wanna consider choosing

122
00:07:22,780 --> 00:07:25,400
more complex usernames and passwords

123
00:07:25,400 --> 00:07:27,053
but I will go with these ones.

124
00:07:28,750 --> 00:07:31,367
But I will change my URL,

125
00:07:32,341 --> 00:07:37,300
I used MONGODB here in development because there thanks

126
00:07:37,300 --> 00:07:41,160
to docker compose and this network being created

127
00:07:41,160 --> 00:07:45,020
by docker which was a very useful feature here locally.

128
00:07:45,020 --> 00:07:48,770
We could use the container name as an address

129
00:07:48,770 --> 00:07:51,370
for requests being made from one container

130
00:07:51,370 --> 00:07:52,653
to another container.

131
00:07:53,879 --> 00:07:57,950
AWS ECS doesn't support this feature like this,

132
00:07:57,950 --> 00:08:00,570
but it does have another neat feature

133
00:08:00,570 --> 00:08:02,623
which is easy to set up and use.

134
00:08:03,530 --> 00:08:07,180
When we add multiple containers to the same task

135
00:08:07,180 --> 00:08:08,720
which we are going to do,

136
00:08:08,720 --> 00:08:11,250
we are going to add the MONGODB container

137
00:08:11,250 --> 00:08:15,110
to this same task as this container is part of,

138
00:08:15,110 --> 00:08:18,210
then these containers can communicate

139
00:08:18,210 --> 00:08:21,870
with each other under the local host key.

140
00:08:21,870 --> 00:08:25,250
So it basically emulates a local system

141
00:08:25,250 --> 00:08:28,973
where all containers are on the same host you could say.

142
00:08:29,870 --> 00:08:34,870
So here in production, in AWS ECS we can use local host

143
00:08:35,419 --> 00:08:38,320
to send the request to some other container

144
00:08:38,320 --> 00:08:41,250
which will be part of the same task.

145
00:08:41,250 --> 00:08:46,000
And therefore I use local host as a value for MONGODB URL

146
00:08:46,000 --> 00:08:51,000
in production, we use MONGODB as a value in development

147
00:08:52,200 --> 00:08:55,520
because in development local hosts wouldn't work.

148
00:08:55,520 --> 00:08:57,210
So that's again another difference

149
00:08:57,210 --> 00:09:00,710
but again we are implementing this such

150
00:09:00,710 --> 00:09:04,210
that our application code is not effected.

151
00:09:04,210 --> 00:09:06,850
There we just use an environment variable

152
00:09:06,850 --> 00:09:10,750
for getting the real address and then the value

153
00:09:10,750 --> 00:09:12,970
for that address depends simply

154
00:09:12,970 --> 00:09:17,150
on where we run this container, and that again ensures

155
00:09:17,150 --> 00:09:19,630
that what's in the container is locked in

156
00:09:19,630 --> 00:09:22,960
and works equally in all environments.

157
00:09:22,960 --> 00:09:26,980
And then we may just choose to run things differently

158
00:09:26,980 --> 00:09:28,823
in different environments.

159
00:09:30,170 --> 00:09:34,300
So with that, I added these free environment variables.

160
00:09:34,300 --> 00:09:38,090
We don't need to set up anything else here

161
00:09:38,090 --> 00:09:40,373
in the other parts.

162
00:09:42,150 --> 00:09:45,120
We also don't need to set up anything for storage

163
00:09:45,120 --> 00:09:48,800
and log in because the two volumes I did set up

164
00:09:48,800 --> 00:09:52,090
for my backend here, I removed the logs which we used

165
00:09:52,090 --> 00:09:55,070
in docker compose and therefore the two remaining volumes

166
00:09:55,070 --> 00:09:57,860
we have here are really just a bind mount

167
00:09:57,860 --> 00:10:00,350
and then an anonymous volume to ensure

168
00:10:00,350 --> 00:10:03,860
that the bind mount doesn't overwrite node modules.

169
00:10:03,860 --> 00:10:06,230
Since we're not using bind mounts in production,

170
00:10:06,230 --> 00:10:09,143
we therefore don't need to add any volume at all here.

171
00:10:10,220 --> 00:10:12,860
So therefore we can now click Add

172
00:10:12,860 --> 00:10:16,053
and with that we added our first container here.

173
00:10:16,960 --> 00:10:19,840
But of course that's not the only container we want here,

174
00:10:19,840 --> 00:10:22,270
after all we have an application

175
00:10:22,270 --> 00:10:27,120
that uses two containers here, the backend and MONGODB.

176
00:10:27,120 --> 00:10:29,813
So let's now add MONGODB as well.

