Flask Elastic Beanstalk Quickstart

Overview

This tutorial will help you deploy a simple flask app that displays a "Welcome to Gallery" splash screen, hosted on Elastic Beanstalk on your own infrastructure. You'll need to have a Gallery account linked with AWS (take a look at the AWS page to do that) and Github to get started.

Create the Template

Go to the templates page on your Gallery dashboard, and click create a template. Name it "Flask Quickstart," and enable the AWS provider.

Terraform Setup

Now, we'll set up a basic terraform template to use for your infrastructure.

  1. First, fork and clone the starter terraform project. It's important you fork the project to your own Github namespace in order to be able to add it to the template later in the tutorial.

$ git clone https://github.com/<your_username>/flask-terraform-getting-started.git

2. Add the following to the vars.tffile.

variable "gallery_info" {
    type = object({
        env_name = string
    })
}

This defines the Gallery variables, which allows our build system to inject variables to your infrastructure-as-code projects at build time. In this case, we're going to use the var.gallery_info.env_name variable, which is a unique identifier for each environment.

3. Now, add the following code to the eb.tf file.

resource "aws_elastic_beanstalk_application" "gallery" {
  name        = "gallery-test-app-${var.gallery_info.env_name}"
  description = "Gallery test application"
}

resource "aws_elastic_beanstalk_environment" "tfenvtest" {
  name                = "tf-test-name-${var.gallery_info.env_name}"
  application         = aws_elastic_beanstalk_application.gallery.name
  solution_stack_name = "64bit Amazon Linux 2 v3.3.7 running Python 3.8"

  setting {
      namespace = "aws:autoscaling:launchconfiguration"
      name = "IamInstanceProfile"
      value = "aws-elasticbeanstalk-ec2-role"
  }

  setting {
    namespace = "aws:autoscaling:launchconfiguration"
    name = "InstanceType"
    value = "t2.micro"
  }

  setting {
    namespace = "aws:elasticbeanstalk:environment"
    name = "EnvironmentType"
    value = "LoadBalanced"
  }

  setting {
    namespace = "aws:elasticbeanstalk:environment"
    name = "LoadBalancerType"
    value = "application"
  }

  setting {
    namespace = "aws:autoscaling:asg"
    name = "MinSize"
    value = 1
  }

  setting {
    namespace = "aws:autoscaling:asg"
    name = "MaxSize"
    value = 1
  }
}

This snippet defines a basic elastic beanstalk environment and application, with the name of the environment and application including the gallery environment name (in order to have uniqueness in those fields).

4. Add the following to outputs.tf. Gallery can use outputs to give you access to data in the application build step.

output "base_url" {
     description = "The URL to the elastic beanstalk instance"
     value = aws_elastic_beanstalk_environment.tfenvtest.cname
}

5. Push the changes to your repository fork.

$ git push origin master

Set up the Application Repository

Now, we'll set up the flask application repository to deploy on your newly created infrastructure.

  1. First, fork the sample repository into your Github account/organization, and clone the repository.

$ git clone https://github.com/gallery-devops/flask-app-getting-started.git

2. Add the following into the .gallery-deploy.yml file.

image: coxauto/aws-ebcli
infra_stages:
  - deploy
jobs:
  deploy:
    stage: deploy
    script:
      - eb init gallery-test-app-$ENV_ID --platform "Python 3.8" --region "us-east-2"
      - eb deploy tf-test-name-$ENV_ID

This deploy file has a single job and stage that deploys the application to the elastic beanstalk environment we created in the previous step.

Add the Services and Deploy Commands

  1. Now, head back to the Gallery template creation flow, and click "Add Service" and "From Terraform Snippet.

2. Select the "Repository" terraform type, link your forked terraform repository, and click "Save."

3. Once the new template is created, go to the URL Mappings tab, select the base_url terraform output, leaving the subdomain blank and hit "Save." This will allow Gallery to serve traffic to the newly created environment.

4. Click next, and click "Add Repo" on the deployment commands page. Then, select the forked application repository, and the "Defined in Repo" build type, and click save.

5. Click next and finish the template creation process.

Create the Environment

  1. Go to the "Environments" tab, and click "Add Environment."

  2. Pick an environment name, select the template you created in the previous step, and click "Save"

  3. Wait for the environment status to change to active and refresh the page -- you should see a link to your new environment!

Last updated