我对 AWS 很陌生。我学习了一门名为“AWS Cloud Practitioner Essentials”的课程,然后我最近一直在学习从 Udemy 获得的认证开发人员课程。
有段时间,我对 python、Django 和 Flask 很感兴趣。设置 EC2 实例有点有趣,但真正引起我注意的是 i) 设置自动缩放组/负载均衡器。ii) Elastic Beanstalk iii) CodePipeline 和 iv) CodeStar。
作为课程的一部分,使用带有 node.js 的 Elastic Beanstalk 设置了 CodePipeline,它创建了一个预生产环境、手动签核,然后是生产发布。
在 CodeStar 中设置 Django 项目非常容易,但是我对很多事情不是很有信心,正在寻找一些建议。特别是当在 CodeStar 中设置 Django 项目时,它不会直接在 CodePipeline 中创建 Elastic Beanstalk 项,而是创建两个名为 GenerateChange... 和 ExecuteChangeSet 的 CloudFormation 对象。
我必须承认我对 CloudFormation 没有信心。当我查看 Django 项目生成的 Elastic Beanstalk 时,它创建了一个带有一个 EC2 实例的系统。我真正想尝试的是负载平衡/自动缩放系统。您能帮忙解决以下问题吗:
i) 如果我直接进入创建的 Django CodeStar 项目的 Elastic Beanstalk 并将实例从单个更改为负载平衡,这是否可以接受,还是应该尝试调整 CodePipeline 中的 CloudFormation 设置?
ii) Elastic Beanstalk 如何知道如何在每个 EC2 实例上设置 Django 项目?当我进入 Elastic Beanstalk 项目并查看配置部分时,我看不到任何安装任何类型服务器的设置。
iii) 如果我要添加手动批准然后部署到生产环境,我可以设置一个新的 Elastic Beanstalk 实例还是需要使用 CloudFormation?
iv) 我还没有想到将数据库添加到 CodePipeline,但据我了解,在生产环境中,我需要创建一个单独的数据库,而不是直接将其添加到 Elastic Beanstalk 中。我想试着了解我要去的地方。我想知道的是,您通常是通过将迁移添加到 buildspec.yml 文件来自动化迁移,还是做其他事情?
v) 如果您对此特定领域的教程有任何建议,我们将不胜感激。
感谢您的建议。
标记
这是一个典型的 buildspec.yml 文件:
version: 0.2
phases:
install:
runtime-versions:
python: 3.7
commands:
# Install dependencies needed for running tests
- pip install -r requirements.txt
pre_build:
commands:
# Discover and run unit tests. For more information, see <https://docs.djangoproject.com/en/2.0/topics/testing/overview/>
- python manage.py test
- aws cloudformation package --template template.yml --s3-bucket $S3_BUCKET --output-template-file template-export.yml
post_build:
commands:
# Do not remove this statement. This command is required for AWS CodeStar projects.
# Update the AWS Partition, AWS Region, account ID and project ID in the project ARN on template-configuration.json file so AWS CloudFormation can tag project resources.
- sed -i.bak 's/\$PARTITION\$/'${PARTITION}'/g;s/\$AWS_REGION\$/'${AWS_REGION}'/g;s/\$ACCOUNT_ID\$/'${ACCOUNT_ID}'/g;s/\$PROJECT_ID\$/'${PROJECT_ID}'/g' template-configuration.json
artifacts:
type: zip
files:
- template-export.yml
- template-configuration.json
这是 cloudformation 模板:
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::CodeStar
Conditions:
UseSubnet:
Fn::Not:
- Fn::Equals:
- Ref: SubnetId
- subnet-none
Parameters:
ProjectId:
Type: String
Description: AWS CodeStar project ID used to name project resources and create
roles.
InstanceType:
Type: String
Description: The type of Amazon EC2 Linux instances that will be launched for
this project.
KeyPairName:
Type: String
Description: The name of an existing Amazon EC2 key pair in the region where the
project is created, which you can use to SSH into the new Amazon EC2 Linux instances.
VpcId:
Type: String
Description: The ID of the Amazon Virtual Private Cloud (VPC) used for the new
Amazon EC2 Linux instances.
SubnetId:
Type: String
Description: The name of the VPC subnet used for the new Amazon EC2 Linux instances
launched for this project.
SolutionStackName:
Type: String
Description: The software stack used to launch environments and configure instances
in AWS Elastic Beanstalk.
EBTrustRole:
Type: String
Description: The service role in IAM for AWS Elastic Beanstalk to be created for
this project.
EBInstanceProfile:
Type: String
Description: The IAM role that will be created for the Amazon EC2 Linux instances.
Stage:
Type: String
Description: The name for a project pipeline stage, such as Staging or Prod, for
which resources are provisioned and deployed.
Default: ''
Resources:
EBApplication:
Description: The AWS Elastic Beanstalk application, which is a container used
to deploy the correct application configuration.
Type: AWS::ElasticBeanstalk::Application
Properties:
ApplicationName:
Fn::Sub: ${ProjectId}app${Stage}
Description: The name of the AWS Elastic Beanstalk application to be created
for this project.
EBApplicationVersion:
Description: The version of the AWS Elastic Beanstalk application to be created
for this project.
Type: AWS::ElasticBeanstalk::ApplicationVersion
Properties:
ApplicationName:
Ref: EBApplication
Description: The application version number.
SourceBundle:
S3Bucket: aws-codestar-************-pipe
S3Key: *************************
EBConfigurationTemplate:
Description: The AWS Elastic Beanstalk configuration template to be created for
this project, which defines configuration settings used to deploy different
versions of an application.
Type: AWS::ElasticBeanstalk::ConfigurationTemplate
Properties:
ApplicationName:
Ref: EBApplication
Description: The name of the sample configuration template.
OptionSettings:
- Namespace: aws:elasticbeanstalk:environment
OptionName: EnvironmentType
Value: SingleInstance
- Namespace: aws:elasticbeanstalk:environment
OptionName: ServiceRole
Value:
Ref: EBTrustRole
- Namespace: aws:elasticbeanstalk:healthreporting:system
OptionName: SystemType
Value: enhanced
SolutionStackName:
Ref: SolutionStackName
EBEnvironment:
Description: The AWS Elastic Beanstalk deployment group where the application
is deployed, which is made up of the Amazon EC2 Linux instances launched for
this project.
Type: AWS::ElasticBeanstalk::Environment
Properties:
ApplicationName:
Ref: EBApplication
EnvironmentName:
Ref: EBApplication
Description: The application to be deployed to the environment.
TemplateName:
Ref: EBConfigurationTemplate
VersionLabel:
Ref: EBApplicationVersion
OptionSettings:
- Namespace: aws:autoscaling:launchconfiguration
OptionName: IamInstanceProfile
Value:
Ref: EBInstanceProfile
- Namespace: aws:autoscaling:launchconfiguration
OptionName: InstanceType
Value:
Ref: InstanceType
- Namespace: aws:autoscaling:launchconfiguration
OptionName: EC2KeyName
Value:
Ref: KeyPairName
- Namespace: aws:ec2:vpc
OptionName: VPCId
Value:
Ref: VpcId
- Fn::If:
- UseSubnet
- Namespace: aws:ec2:vpc
OptionName: Subnets
Value:
Ref: SubnetId
- Ref: AWS::NoValue
这些是 CloudFormation 的 CodePipeline 配置的快照: