In this POC, we'll automatize all the infrastructure of creating AWS ECS Resources using CloudFormation.
We already did some steps in the previous video:
- Create a dotnet project
- Create a dockerfile
- Create an image and container in Docker to test.
In the last video we also deployed the app in AWS but we had to do this manually using the AWS dashboard.
Now we'll automatize all the resources that we need to create in AWS Dashboard in the previous video using CloudFormation. What we'll do then: 4. CloudFormation File for us to create an ECR 5. Run the docker command line to: 4.1. Get our credentials from AWS 4.2. Send our Image to ECR 6. Create a CloudFormation file with instructions to: 6.1. Create a Task Definition 6.2. Create a Cluster on ECS 6.3. Attach our Task to the Cluster 6.4. Open the port on EC2 - Security Group 6.5. Add the Image URI to the Task Definition 7. Voilà
The first steps we already did in our first video.
Add an ecs.yml where we will define every resource that we need to create to deploy our container.
To create or update the stack in CloudFormation run:
#to create a new stack
aws cloudformation create-stack --stack-name simple-cf --template-body ecs.yml --parameters 'ParameterKey=SubnetID,ParameterValue=subnet-12345678' --capabilities CAPABILITY_NAMED_IAM
TIP: But let's do something a little bit more fancy by adding this code in a .sh file and storing the parameters in a different json file.
To pass the parameters via file, we use --parameters file://my.params.json
:
#to create a new stack
aws cloudformation create-stack --stack-name simple-cf --template-body file://mycloudformation.yaml --parameters file://my.params.json --capabilities CAPABILITY_NAMED_IAM
After your .sh is created, just run it in your terminal
#to create a new stack
cd ecr/
sh cf-ecr.deploy.sh
We should be able to see your Image repository created in ECR AWS dashboard.
We already have our Docker image created locally from our last video. You can find how we do that reading this README here
So, now we will just have to push our docker image to ECR Repository:
#tag
docker tag devotts/simpleapi:latest [AccountId].dkr.ecr.us-east-1.amazonaws.com/simple-cf-repo:latest
#push
docker push [AccountId].dkr.ecr.us-east-1.amazonaws.com/simple-cf-repo:latest
In the previous video, you saw how time-consuming is to set all the infrastructure to set up ECS. Imagine if you have to do this for different environments like dev, uat, prod.
Even worth it if you need to delete what you did. How do we make sure that we deleted everything that we had to create?
CloudFormation is here to help us. Take a look at the cf-ecs.yml
file and compare it with the step-by-step in the previous video where we did it all manually. You'll notice that we are doing exactly the same, but now through coding.
We are also using a separate file to store the parms and again using a .sh file to run the aws cloudformation command line.
So, let's run it:
cd ecs/
sh cf-ecs.deploy.sh
This will create the resources above:
- Cluster
- TaskDefinition
- Service
- LogGroup
- SecurityGroup
- IAM Role
You can follow the creation of all these resources in CloudFormation > Stack.
Notice something important in this yaml file.
-
To make our code clear and easy to change, we create a PrefixName parameter that let us normalize names for all the resources that we create. We use
!Join
to concatenate the string. Take a look at the documentation -
We weren't able to use the same port as we did in the previous video, so I kept the port as 5000 without mapping it. You can understand more about this bug that CloudFunction has here in this issue. Also the StackOverflow thread.
Everything is ready! Now go to your Cluster > Tasks and in the details of this task, get the Public IP number and just open it in your browser:
http://the-ip:5000/WeatherForecast
- One of the best parts of using CloudFormation is how easy it is to delete all resources. Basically, you just have to delete the Stack in CloudFormation.
Just make sure that you delete the image in ECR before you do that.
- Did you notice that we didn't have to add an Input Rule in Security Group? Another beauty of using this code!
After finishing this I'm still not happy because. Some of the questions that I ask myself.
-
How can I automatize the push of our Image to ECR process?
-
CloudFunction is nice, but to be honest, kind of intimidating to have to know all these properties and possible configurations for each resource. Can it be easier?
-
I don't want to have to run the bash commands every time I make a change in my code. How can I automatize that?
The questions for that are Codebuild, CDK, and CodePipeline in this same order, and that will be our improvements in the next videos.