AWS

AWS Deploying ASP .NET Core app to Elastic Beanstalk

What are we talking about this time?

This time we are going to talk about AWS Elastic Beanstalk and how we can use it to deploy a scalable load balanced web site

Initial setup

If you did not read the very first part of this series of posts, I urge you to go and read that one now as it shows you how to get started with AWS, and create an IAM user : https://sachabarbs.wordpress.com/2018/08/30/aws-initial-setup/

Where is the code

The code for this post can be found here in GitHub : https://github.com/sachabarber/AWS/tree/master/Compute/ElasticBeanstalk/WebApiForElasticBeanstalk

What is Elastic Beanstalk?

Elastic Beanstalk is one of the AWS compute services. It comes with several platform languages supported. In other languages such as Java there is a web kind of role and a worker kind of role. However for .NET there is ONLY a IIS web kind of role. However don’t let that put you off, The whole planet like web sites of late it would seem, and it just so happens Elastic Beanstalk is a perfect fit for these type of apps.

Elastic Beanstalk has the following architecture

Image result for elastic beanstalk

It can be seen that EC2 instance are part of an Auto Scale Group (scalability) and we also get a load balancer out of the box with a single URI endpoint for our Elastic Beanstalk app, which will load balance amongst the running web apps hosted on the EC2 instances which are all running the web app. This is quite a lot of stuff that is good to have for free. This sort of stuff is quite hard to configure by yourself, so this is quite cool.

Deploying from Visual Studio

So when you create a new .NET Core Web App (The demo uses a standard .NET Core WebApi project) you can publish it straight to Elastic Beanstalk straight from Visual Studio.

image

This is obviously thanks to the AWS Toolkit (which I have installed and talk about in the 1st article in this series). This will launch a wizard, which looks like this

image

You can choose to create a new application environment or use one that you previously created

image

We can then pick a name for the application and its URI that will be publicly available

image

You then pick your hardware requirements (EC2 instance types)

image

You then pick your application permissions / Service permissions

image

We then pick our application options

When you Finish the wizard you should see something like this screen

image

The entire deployment takes about 10 minutes to do, so be patient. One thing to note is that the Visual Studio deployment also creates the aws-beanstalk-tools-defaults.json file to aid in the final application deployment to AWS. This is its contents for this demo app

{
    "comment" : "This file is used to help set default values when using the dotnet CLI extension Amazon.ElasticBeanstalk.Tools. For more information run \"dotnet eb --help\" from the project root.",
    "profile" : "default",
    "region"  : "eu-west-2",
    "application" : "WebApiForElasticBeanstalk",
    "environment" : "WebApiForElasticBeanstalk-dev",
    "cname"       : "webapiforelasticbeanstalk-dev",
    "solution-stack" : "64bit Windows Server 2016 v1.2.0 running IIS 10.0",
    "environment-type" : "SingleInstance",
    "instance-profile" : "aws-elasticbeanstalk-ec2-role",
    "service-role"     : "aws-elasticbeanstalk-service-role",
    "health-check-url" : "/",
    "instance-type"    : "t2.micro",
    "key-pair"         : "",
    "iis-website"      : "Default Web Site",
    "app-path"         : "/",
    "enable-xray"      : false
}

This is what is happening behind the scenes

Launching an environment creates the following resources:

  • EC2 instance – An Amazon Elastic Compute Cloud (Amazon EC2) virtual machine configured to run web apps on the platform that you choose.

    Each platform runs a specific set of software, configuration files, and scripts to support a specific language version, framework, web container, or combination thereof. Most platforms use either Apache or nginx as a reverse proxy that sits in front of your web app, forwards requests to it, serves static assets, and generates access and error logs.

  • Instance security group – An Amazon EC2 security group configured to allow ingress on port 80. This resource lets HTTP traffic from the load balancer reach the EC2 instance running your web app. By default, traffic isn’t allowed on other ports.
  • Load balancer – An Elastic Load Balancing load balancer configured to distribute requests to the instances running your application. A load balancer also eliminates the need to expose your instances directly to the internet.
  • Load balancer security group – An Amazon EC2 security group configured to allow ingress on port 80. This resource lets HTTP traffic from the internet reach the load balancer. By default, traffic isn’t allowed on other ports.
  • Auto Scaling group – An Auto Scaling group configured to replace an instance if it is terminated or becomes unavailable.
  • Amazon S3 bucket – A storage location for your source code, logs, and other artifacts that are created when you use Elastic Beanstalk.
  • Amazon CloudWatch alarms – Two CloudWatch alarms that monitor the load on the instances in your environment and are triggered if the load is too high or too low. When an alarm is triggered, your Auto Scaling group scales up or down in response.
  • AWS CloudFormation stack – Elastic Beanstalk uses AWS CloudFormation to launch the resources in your environment and propagate configuration changes. The resources are defined in a template that you can view in the AWS CloudFormation console.
  • Domain name – A domain name that routes to your web app in the formsubdomain.region.elasticbeanstalk.com.

All of these resources are managed by Elastic Beanstalk. When you terminate your environment, Elastic Beanstalk terminates all the resources that it contains.

 

Taken from https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/dotnet-core-tutorial.html#dotnet-core-tutorial-deploy up on date 19/09/18

Deploying from the command line

Ok so we can deploy from Visual Studio, this is cool, but it certainly won’t cut the mustard for CI purposes. Luckily AWS exposes some new .NET Core commands that we can use to deploy to Elastic Beanstalk. We need to grab the CLI tools to do this

Get the AWS Dot Net Core CLI tools

You will need .NET Core SDk 2.1.300 and later (dotnet tool is only available in 2.1.3 or later), once you have that installed you should be able to run this command dotnet tool install -g Amazon.ElasticBeanstalk.Tools to install the AWS Elastic Beanstalk dotnet commands. You can read more about this here : https://github.com/aws/aws-extensions-for-dotnet-cli/blob/master/README.md

So once you have these you should be able to change to the directory that contains your app to deploy and use this command line dotnet eb deploy-environment and follow the command line prompts. You may see an error message about S3 bucket not existing, which is easy enough to fix, just look at what bucket it was trying to create and go create it, it should be a Non-Public / Private bucket

Checking the deployment

So as we saw above a deployment to Elastic Beanstalk did quite a few things, so we should be able to check out the following

That the binaries are in S3 bucket

When we deploy an app to Elastic Beanstalk the binaries are placed into a S3 bucket, so we should see that has been created for us

image

Where we can drill into the bucket and see these files

image

The Elastic Beanstalk Env

We can use the Elastic Beankstalk console https://console.aws.amazon.com/elasticbeanstalk to see if our environment is deployed ok. We should see something like this

image

One thing that is interesting is if we go into the environment (green box)

image

Then use the Actions button at the top right, we can save this environment, so we can use this as a blueprint for a new environment.

Other cool stuff is that we can look at the Configuration, request logs, look at monitoring, setup alarms etc etc

Checking the deployed application works

Using the URL for our app, we should be able to test out our standard WebApi project. Lets give it a go

image

All looking good. So there you go. That’s all pretty cool I think

See ya later, not goodbye

Ok that’s it for now until the next post

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s