Gallery
Search…
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.
The New Template page with AWS enabled

Terraform Setup

Now, we'll set up a basic terraform template to use for your infrastructure.
  1. 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.
1
$ git clone https://github.com/<your_username>/flask-terraform-getting-started.git
Copied!
2. Add the following to the vars.tffile.
1
variable "gallery_info" {
2
type = object({
3
env_name = string
4
})
5
}
Copied!
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.
1
resource "aws_elastic_beanstalk_application" "gallery" {
2
name = "gallery-test-app-${var.gallery_info.env_name}"
3
description = "Gallery test application"
4
}
5
6
resource "aws_elastic_beanstalk_environment" "tfenvtest" {
7
name = "tf-test-name-${var.gallery_info.env_name}"
8
application = aws_elastic_beanstalk_application.gallery.name
9
solution_stack_name = "64bit Amazon Linux 2 v3.3.7 running Python 3.8"
10
11
setting {
12
namespace = "aws:autoscaling:launchconfiguration"
13
name = "IamInstanceProfile"
14
value = "aws-elasticbeanstalk-ec2-role"
15
}
16
17
setting {
18
namespace = "aws:autoscaling:launchconfiguration"
19
name = "InstanceType"
20
value = "t2.micro"
21
}
22
23
setting {
24
namespace = "aws:elasticbeanstalk:environment"
25
name = "EnvironmentType"
26
value = "LoadBalanced"
27
}
28
29
setting {
30
namespace = "aws:elasticbeanstalk:environment"
31
name = "LoadBalancerType"
32
value = "application"
33
}
34
35
setting {
36
namespace = "aws:autoscaling:asg"
37
name = "MinSize"
38
value = 1
39
}
40
41
setting {
42
namespace = "aws:autoscaling:asg"
43
name = "MaxSize"
44
value = 1
45
}
46
}
Copied!
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.
1
output "base_url" {
2
description = "The URL to the elastic beanstalk instance"
3
value = aws_elastic_beanstalk_environment.tfenvtest.cname
4
}
Copied!
5. Push the changes to your repository fork.
1
$ git push origin master
Copied!

Set up the Application Repository

Now, we'll set up the flask application repository to deploy on your newly created infrastructure.
  1. 1.
    First, fork the sample repository into your Github account/organization, and clone the repository.
1
$ git clone https://github.com/gallery-devops/flask-app-getting-started.git
Copied!
2. Add the following into the .gallery-deploy.yml file.
1
image: coxauto/aws-ebcli
2
infra_stages:
3
- deploy
4
jobs:
5
deploy:
6
stage: deploy
7
script:
8
- eb init gallery-test-app-$ENV_ID --platform "Python 3.8" --region "us-east-2"
9
- eb deploy tf-test-name-$ENV_ID
Copied!
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. 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. 1.
    Go to the "Environments" tab, and click "Add Environment."
  2. 2.
    Pick an environment name, select the template you created in the previous step, and click "Save"
  3. 3.
    Wait for the environment status to change to active and refresh the page -- you should see a link to your new environment!
Last modified 25d ago