Steps in CDK#
- Initialize construct
- Zip flask code
- Set up options for application, env vars, and app version
- Set environment
- Make sure environment is built after CDK application
## Deploy to ElasticBeanstalkanstalk-application-packaging-through-cdk-in-typescript-e91b7ffe4928
app_name = "MyApp"
elb_flask_app = elb.CfnApplication(
self, f"flask-appserver", application_name=app_name
)
## Move app code into S3 for ELB
elb_archive = s3_assets.Asset(
self,
f"elb-flask-app-{stage}",
path=f"{str(Path(__file__).absolute().parents[2])}/app.zip",
)
## Set OptionSettingProperties
option_setting_properties = [
elb.CfnEnvironment.OptionSettingProperty(
namespace="aws:autoscaling:launchconfiguration",
option_name="InstanceType",
value="t3.small",
),
elb.CfnEnvironment.OptionSettingProperty(
namespace="aws:autoscaling:launchconfiguration",
option_name="IamInstanceProfile",
value="aws-elasticbeanstalk-ec2-role",
),
elb.CfnEnvironment.OptionSettingProperty(
namespace="aws:autoscaling:launchconfiguration",
option_name="IamInstanceProfile",
value="aws-elasticbeanstalk-servicerole",
),
# This is an example of setting up an environment variable
elb.CfnEnvironment.OptionSettingProperty(
namespace="aws:elasticbeanstalk:application:environment",
option_name="ES_DOMAIN",
value=es_domain.domain_endpoint,
),
]
## Set app version properties
app_version_props = elb.CfnApplicationVersion(
self,
f"AppVersion-{stage}",
application_name=app_name,
source_bundle=elb.CfnApplicationVersion.SourceBundleProperty(
s3_bucket=elb_archive.s3_bucket_name,
s3_key=elb_archive.s3_object_key,
),
)
## Set environment
elb_env = elb.CfnEnvironment(
self,
f"elb-env",
environment_name=f"elb-env",
application_name=app_name,
solution_stack_name="64bit Amazon Linux 2 v3.2.1 running Python 3.8",
option_settings=option_setting_properties,
version_label=app_version_props.ref,
)
## Ensure the env isn't built before the app by placing this at the end of your CDK stack file
app_version_props.add_depends_on(elb_flask_app)
elb_env.add_depends_on(elb_flask_app)
Steps in AWS Console#
- Launch EC2 instance
- SSH into it,
- Set up Git keys
- See here
- Add deploy key to repo
- Pull code from Github into instance
- Run Flask server
- Activate virutal environment
- Install dependencies with
pip install -r requirements.txt - Set environment variables:
FLASK_ENV=developmentorFLASK_ENV=prod
- Set up uWSGI
- Install
gcc - Install uWSGI with
pip install uwsgi - Install NGinx source 1. Only do steps 1 and 2 from article:
sudo apt update sudo apt install nginx # Test firewall sudo ufw app list sudo ufw allow 'Nginx HTTP' sudo ufw status- Set up uWSGI configuration with
app.ini:
[uwsgi] module = wsgi:app master = true processes = 5 socket = myproject.sock chmod-socket = 660 vacuum = true die-on-term = true- Set up system profile file
/etc/systemd/system/myproject.service, taken from linked article[Unit] Description=uWSGI instance to serve myproject After=network.target [Service] User=sammy Group=www-data WorkingDirectory=/home/sammy/myproject Environment="PATH=/home/sammy/myproject/myprojectenv/bin" ExecStart=/home/sammy/myproject/myprojectenv/bin/uwsgi --ini myproject.ini [Install] WantedBy=multi-user.target - Start with
systemctl:sudo systemctl start myprojectsudo systemctl enable myproject
- Set up nginx
- Edit
/etc/nginx/sites-available/myapp
- Edit
server { listen 80; server_name <EC2 domain>; location / { include uwsgi_params; uwsgi_pass unix:/home/ubuntu/myapp/flask-app/myapp.sock; } }- Then link:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled- Restart nginx service:
- Update firewall rules
$ sudo ufw delete allow 5000 $ sudo ufw allow 'Nginx Full' - Restart nginx service:
- Install
Sources#
AWS - Deploying a Flask Application to Elastic Beanstalk Packaging a NodeJS application with CDK on Elastic Beanstalk