The Amazon Web Services (AWS) Elastic Beanstalk is their answer to cloud web services. Elastic Beanstalk allows you to create a web application on the AWS platform and deploy your web packages to it. You could set this up via the AWS Management Console, but that's a lot of clicking! Instead, let's explore not only how to automate creating the Elastic Beanstalk application and environment but also how to deploy a package to it, all with PowerShell!

First, you'll need to have the AWSPowerShell module available from the PowerShell Gallery (Install-Module AWSPowerShell). You should also have created either an Identity and Access Management (IAM) user or know your root key to authenticate. I'll assume you've already authenticated.

Once authenticated, we need to map out what we're going to be doing, since there are a few steps to this process. We will be:

  • creating the application
  • creating the environment
  • uploading the package to make it available to the application
  • creating a new version of the application
  • deploying the latest version to the environment

Let's dive in and see how to do this with PowerShell!

Creating the Elastic Beanstalk application ^

The first step is to create the Elastic Beanstalk application. We can easily do this using the New-EBApplication command. I'm calling my application TechSnips, but you can name your application anything.

$ebApp = New-EBApplication -ApplicationName 'TechSnips'

Inspecting our new Elastic Beanstalk application shows us that the New-EBApplication returns some interesting information.

New Elastic Beanstalk application

New Elastic Beanstalk application

Creating the Elastic Beanstalk environment ^

Next, we'll need to create the environment or the EC2 instance to host this application on. You can choose various Windows or Linux flavors. To get a full list of all available options, you can use the Get-EBAvailableSolutionStackList command and inspect the SolutionStackDetails property as seen below.

(Get-EBAvailableSolutionStackList).SolutionStackDetails
Elastic Beanstalk environment options

Elastic Beanstalk environment options

Once you've found the solution stack name to use, you can apply it along with all other required parameters to create the Elastic Beanstalk environment using the New-EBEnvironment command as shown below. I've chosen to name my environment Testing. If creating a simple web app, you'll also use the Tier_Type and Tier_Name parameters as Standard and WElastic Beanstalkserver, respectively.

## Create a new environment
$parameters = @{
      ApplicationName = 'TechSnips'
      EnvironmentName = 'Testing'
      SolutionStackName = '64bit Windows Server Core 2012 R2 running IIS 8.5'
      Tier_Type = 'Standard'
      Tier_Name = 'WElastic Beanstalkerver'
}
New-EBEnvironment @parameters

Uploading the web package ^

Now that we've created the environment, we need to upload our web package. I'm assuming you already have a web project in a folder called C:\MyPackageFolder. You need to upload a .zip file containing all files that comprise your web app. We can zip everything up using the Compress-Archive command.

Get-ChildItem -Path 'C:\MyPackageFolder'
Compress-Archive -Path 'C:\MyPackageFolder\*' -DestinationPath 'C:\package.zip'

Once we have the .zip file, we'll need to upload it to a Simple Storage Service (S3) bucket. You are free to use an existing one, but since I don't have one yet, I'll create one using the New-S3Bucket command.

New-S3Bucket -BucketName 'techsnipsapp'

Once I've created the S3 bucket, I can then upload the .zip file to it using the Write-S3Object command.

Write-S3Object -BucketName 'techsnipsapp' -File 'C:\package.zip'

Creating a new Elastic Beanstalk application version ^

We're now ready to create a new version of the application. Every time you make a change to the application, you must create a new version using the New-EBApplicationVersion command. This command requires specifying the name, the S3 bucket, and the S3 key. It's best to define a unique version label. One way to do this is to create a string representing the date in the number of ticks. This is a great way to define the version label.

$verLabel = [System.DateTime]::Now.Ticks.ToString()
$newVerParams = @{
      ApplicationName       = 'TechSnips'
      VersionLabel          = $verLabel
      SourceBundle_S3Bucket = 'techsnipsapp'
      SourceBundle_S3Key    = 'package.zip'
}
New-EBApplicationVersion @newVerParams

Deploying the web package ^

After creating the new version, we now need to deploy the package to the application environment. To do this, we can use the Update-EBEnvironment command specifying some of the same parameters we used earlier.

Update-EBEnvironment -ApplicationName 'TechSnips' -EnvironmentName 'Testing' -VersionLabel $verLabel -Force

Monitoring web package deployment ^

At this point, we've created the application and environment, and we've initiated the package deployment. We can now check on the status of the deployment by using the Get-EBEnvironment command.

Get-EBEnvironment -ApplicationName 'TechSnips' -EnvironmentName 'Testing'

Or, if you'd like to use the AWS Management Console, you can navigate to Elastic Beanstalk Service and monitor it from there as shown in the screenshot below.

Subscribe to 4sysops newsletter!

Monitoring Elastic Beanstalk package deployment

Monitoring Elastic Beanstalk package deployment

Wrap-up ^

You've now gone from nothing all the way up to a deployed web package for a new AWS Elastic Beanstalk application! You should give yourself a pat on the back. As mentioned before, you can perform all of these steps via the AWS Management Console. But if you want to replicate this process many times, it's always a great idea to create a PowerShell script to automate the process.

1 Comment
  1. Chris Tout 2 years ago

    'WElastic Beanstalkerver' should be 'WebServer'

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account