Introduction to AWS Elastic Beanstalk
What's going on guys? In this video we're going to take a look at Elastic Beanstalk and then we're going to deploy our Gin application on Amazon Web Services using that platform. Elastic Beanstalk is a developer-friendly platform to deploy and scale web applications. These applications could be developed in Golang, Node, Java, .NET, Python, Ruby, PHP or we can even deploy a single docker container or multiple docker containers. Elastic Beanstalk is able to handle deployments, capacity provisioning, load balancing, auto scaling and so forth.
It's a platform as a service layer on top of multiple amazon web services such as EC2. Elastic Beanstalk is going to manage EC2 instances for us, is going to create new instances, is going to terminate existing instances, is going to set up auto scaling groups for us, load balancers, Elastic IPs, Security Groups and so forth.
Elastic Beanstalk is going to interact with Amazon S3 to store the different versions of the application, environment configurations and application logs. Elastic Beanstalk introduces a level of abstraction to CloudFormation. In a nutshell, CloudFormation allow us to describe the infrastructure in JSON format and we can use that definition to create the actual infrastructure.
Elastic Beanstalk uses CloudFormation behind the scenes creating and applying infrastructure definitions for us, also interacts with other services such as CloudWatch, RDS, DynamoDB, SQS, SNS and so forth. AWS Elastic Beanstalk supports two application or architectural types: web servers and workers.
A web server application may include a single instance or a load balancer and multiple instances "behind it" and in case of using a worker application we're gonna have an SQS queue with just one consumer or a single instance or multiple instances or consumers. In order to set up Elastic beanstalk we can use the web console, the sdk or the CLI and some similar tools are Heroku, Azure App Service and Google App Engine.
And if you want to learn data science with Python check out DataCamp, there is a link in the description below this video. And you can also download the Golang cheat sheet, there is a link in description below this video as well. Remember to subscribe to the channel and let's get started.
Setting Up IAM Permissions for Deployment
We are going to use the Elastic beanstalk CLI so we're going to need to assign the required permissions to our user in order to create the infrastructure we need to deploy our application. Elastic Beanstalk integrates multiple AWS services so we need to create a policy such as our user is able to do multiple actions on multiple AWS resources.
And if we go to IAM, there is a policy that includes permissions to use all the services that are integrated with Elastic Beanstalk. So let's go to IAM, now let's go to Policies, and here we can look for ElasticBeanstalkFullAccess. This one. And if we see the details of the policy we're gonna see that allows actions on multiple resources: Elastic Beanstalk, EC2, ECS, ECR, S3, SQS, SNS, load-balancing, auto-scaling, IAM... multiple services.
This is a default policy we can use this one but a good practice from the security perspective is to set up a policy on demand. What I mean is you have to give permissions only to the resources that you know you are going to use. So in our case we don't need RDS, we don't need SQS, we don't need DynamoDB so I'm going to create a policy. I'm going to copy this policy, I'm going to create a new one but removing the resources that I don't need to use.
Okay so I'm going to create a new policy and I'm going to paste the default policy and I'm going to remove RDS, Dynamo and SQS. I'm going to review the policy and I'm going to name it "eb-deploy".
I'm going to create a new group. I'm going to call it "eb-deploy" and I'm going to attach the policy that I created, "eb-deploy". And now I'm going to add my user to that group. Add user to groups... and this is "eb-deploy". Now this user is able to access Elastic Beanstalk and deploy applications.
Preparing the Go Application Code
On a previous video we created two groups for our endpoints. The first group is the API group that includes two endpoints. The first one will allow us to get the existing videos, this is actually a (HTTP) GET endpoint, and a (HTTP) POST endpoint that is going to allow us to create new videos. And the second group is the "View" group where we render the existing videos using HTML and the templates that we created here on this folder.
Before deploying our application on AWS using Elastic Beanstalk, we are going to apply a basic Authorization middleware just to the API group here. The view group, this one, is going to be public so there is no Authorization required in this case.
And the last change I made to the code is setting up the port here by default as 5000 and this is the default port number that Elastic Beanstalk, using Nginx, is going to forward the requests to. And here I'm also reading the port from an environment variable so we can change it from the Elastic Beanstalk console. There is a place within the Elastic Beanstalk console where we can easily edit all the environment variables that are being used by our application.
With Elastic Beanstalk we can deploy the application using two approaches: so we can either be platform specific in this case with Golang or we can take a platform neutral approach and deploy our application using Docker containers.
In this video we are going to deploy using a platform specific approach and in a future video I'm going to deploy the same application but using Docker containers. Okay, so to deploy our Golang application without using Docker we need to provide a few files: the first file is going to be a shell script to build the application so in our case is going to download a couple of packages and dependencies here for the Gin HTTP framework and the validator and it's going to build the application running this command and it's going to generate a binary file named application within the bin folder here.
Another file that we need to provide in our deployment is the Buildfile that basically indicates what is the shell script file that Elastic Beanstalk needs to run in order to build the application and in our case is this one. And finally we have to include a Procfile that indicates to Elastic Beanstalk what is the command required to run the web application and that is the binary, this one, bin slash application that is a binary that we generated using the build script here.
Initializing the Environment with the EB CLI
In "Beanstalk" we have to create an application and all the environments that we want to use for example: Dev, QA, UAT, Production, etc. So... the first command that we need to run is "eb init" where we're going to select first the region that we want to use to deploy our application and the environments of the application. A good way to know what is the region with the minimum latency for us is going here to Cloudping.info and in my case Sao Paulo is a region with optimal latency for me so I'm going to use that one.
So I'm going to select in this case is the option 11. I already created an application but I'm going to create a new one, let's call it "golang-api"... that is not a valid choice, oh yeah my bad. And here: "golang-api"... okay. And I'm going to select the environment, here we can select sorry, the platform, here we can select different platforms. I'm going to select "Go" and I'm going to select the first option here.
I'm not going to use CodeCommit, I'm not going to set up SSH and here as we can see it's going to generate this config file that is going to include the application name, the platform, the region and some other information.
Creating and Deploying the Application
Ok, now that we have our application ready let's create a new environment so we need to run eb create and I'm going to create an environment with a single instance. Here we need to assign a name to the environment so I'm going to use "gin-dev" and this name is going to be included as part of the DNS that Elastic Beanstalk is going to assign to our application. I'm not going to use this, and now it's going to create the application is going to create a zip file including all the contents that we have here.
And here are some details: the application name, the name of the environment, the region, the version of the application, that is the name of a zip file, this one... is going to upload that zip to a S3 bucket and this is the platform, this is the ID of the environment, we're using a web server application type, this is the DNS that we can use to access the application and here are all the events that are part of the creation of the environment. So here is the name of the bucket that is going to use to store environment data and different versions of the application and this is the security group that is going to create for us and this is the elastic IP that is going to create for this environment.
Verifying the Deployment and Testing the API
Ok, so Elastic Beanstalk has finished the creation of the new environment and now we can access the application using this domain name. So if we go to the Elastic Beanstalk console we can access the environment there and we can go to the URL and we can use this URL to access the list of videos and here we can see that we are able to access the HTML of our application. It's empty because we don't have any videos in the server so let's go to Postman and let's create a few videos and we're gonna see that we're gonna have all the videos listed here. Okay.
Let's create a couple of videos. Remember that we enabled a middleware, an authorization middleware, so we need to pass this auth header or we can use this option here. We can select basic auth and select pragmatic and reviews, that's the password. okay so let's create another video, another video here, and let's create another one and now if we go back and we refresh here we're gonna see that we have all the videos created.
Exploring the Elastic Beanstalk Console Dashboard
So if we go to the Elastic Beanstalk dashboard, I'm not going to do a deep dive here but the most important options are here. The configuration section where we can set all the environment variables using this UI. We can also set the autoscaling policy here, we can select if we are going to use a single instance or a load balanced environment. And here we can say how many instances we are going to use, we can set up the autoscaling triggers. And here we have different options if we can scale depending on CPU utilization, Network In/out and latency, requests count and some other options.
And here we have some other configurations that we can do. Here is the log section where we can access the logs for our environment. Let's say that I want to get the last hundred lines of the log, I can access that from here. Here is the health section where I get an overview of the instance. Here is the monitoring section I can get different information. I can also edit this dashboard and add some other statistics so let's say this environment... so I can select different options. And that's pretty much it, thank you guys for watching and I see you in the next video, take care, Bye!