Elastic Beanstalk
Elastic Beanstalk is a fully managed service that makes it easy to deploy and run applications in multiple languages. Here is an example of how to deploy a Python Flask application to Elastic Beanstalk using the AWS SDK for Python (boto3):
python
import boto3
from zipfile import ZipFile
elasticbeanstalk = boto3.client('elasticbeanstalk')
application_name = 'my-application'
environment_name = 'my-environment'
version_label = 'v1'
# Create a ZIP file of the Flask application
with ZipFile('my-application.zip', 'w') as zip:
zip.write('application.py')
zip.write('requirements.txt')
# Upload the ZIP file to S3
s3 = boto3.client('s3')
s3.upload_file('my-application.zip', 'my-bucket', 'my-application.zip')
# Create a new application version
elasticbeanstalk.create_application_version(
ApplicationName=application_name,
VersionLabel=version_label,
SourceBundle={
'S3Bucket': 'my-bucket',
'S3Key': 'my-application.zip'
}
)
# Create a new environment
elasticbeanstalk.create_environment(
ApplicationName=application_name,
EnvironmentName=environment_name,
VersionLabel=version_label,
SolutionStackName='64bit Amazon Linux 2 v5.4.7 running Python 3.8',
OptionSettings=[
{
'Namespace': 'aws:autoscaling:launchconfiguration',
'OptionName': 'InstanceType',
'Value': 't2.micro'
}
]
)
print(f"Flask application deployed to Elastic Beanstalk environment {environment_name}")
This code creates a ZIP file of a Python Flask application, uploads the ZIP file to S3, creates a new application version in Elastic Beanstalk, and creates a new environment for the application. The create_environment method specifies an Amazon Linux 2 environment running Python 3.8 and a t2.micro instance type.
No comments:
Post a Comment