AWS的CloudFormation和Elastic Beanstalk的简单测试

文章目录

  • CloudFormation
  • Elastic Beanstalk
  • 通过EB CLI操作Elastic Beanstalk
    • EB CLI操作的简单测试
    • EB CLI指令eb init的问题
    • 使用EB CLI发布项目
  • Reference

CloudFormation

CloudFormation归类在Management & Governance下,它有很多模板可以直接使用,模板实际上是包含配置的文本文件,使用模板可以用很简单的操作可以创建,重复创建和设置很复杂的应用。

这里做一个简单的测试,点Create stack,取名: XiongStack,再使用Use a sample template,使用已有的模板,创建一个Wordpress blog,并进行各种设置。在这里可以看到模板的细节,也可以对模板进行编辑。

创建后可以在Events下看到进度。创建完成后,在Resource下查看网址:http://ec2-13-229-131-47.ap-southeast-1.compute.amazonaws.com/wordpress/,输入后有以下提示:

Your server is running PHP version 5.3.29 but WordPress 5.3 requires at least 5.6.20.

这个问题以前碰到过,需要升级PHP版本,参见使用AWS云安装WordPress + RDS MySQL方法进行解决,解决后以下网址可以正常访问:

http://ec2-13-229-131-47.ap-southeast-1.compute.amazonaws.com/wordpress/

测试完成后,删除这个Stack,那么这个Stack下所有的资源全部都被删除。

Elastic Beanstalk

Elastic Beanstalk是在EC2界面里,它主要实现了容器的功能,使你不用担心底层的架构,例如负载均衡,扩展,运行健康检查等,使用者只需关注于应用层的创建和设置。

创建一个Elastic Beanstalk,进入Create a web app,取名为XiongElasticBeanstalk,使用PHP,并使用Sample Application,也就是样例应用,这里是简单起见。当然也可以上传代码(上传ZIP或者WAR),发布成功后,查看以下生成的 DNS网址,成功访问。

http://xiongelasticbeanstalk-env-1.pysmrc6iwc.ap-southeast-1.elasticbeanstalk.com/

可以对样例程序进行下载,修改,再上传,进行发布,出现以下问题:

Forbidden
You don’t have permission to access this resource.

查找原因,参见403 Forbidden error,原因如下:

When zipping up the files Winzip has created a additional folder to contain all files. Therefore the root folder did no have an index file but only the additional folder with the entire website.

也就是在ZIP文件时,自动生成了根目录,因为只有根目录,所以找不到index.html文件,而是在下一级目录。这里要在目录里面去生成ZIP文件,再重新生成php-v3.zip文件。

再重新上传,进行发布,最后通过生成的DNS网址来检查更新的情况。问题解决,网页可以正常访问!

通过EB CLI操作Elastic Beanstalk

我的Windows环境的Python和PIP如下:、

C:\Users\xionghuilin>python --version
Python 2.7.9

C:\Users\xionghuilin>pip --version
pip 1.5.6 from C:\Python27\lib\site-packages (python 2.7)

C:\Users\xionghuilin>

参见:Github aws/aws-elastic-beanstalk-cli-setup,在Github克隆EB CLI的安装文件,

D:\AWS> git clone https://github.com/aws/aws-elastic-beanstalk-cli-setup.git
Cloning into 'aws-elastic-beanstalk-cli-setup'...
remote: Enumerating objects: 49, done.
remote: Counting objects: 100% (49/49), done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 286 (delta 23), reused 31 (delta 14), pack-reused 237
Receiving objects: 100% (286/286), 519.17 KiB | 549.00 KiB/s, done.
Resolving deltas: 100% (153/153), done.

出现以下错误,

D:\AWS> python .\aws-elastic-beanstalk-cli-setup\scripts\ebcli_installer.py

***********************************
1. Locating virtualenv installation
***********************************
ERROR: Could not find and "virtualenv" installed. Ensurevirtualenv is installed and that it is in PATH before executingthis script.

******************************************
2. Creating exclusive virtualenv for EBCLI
******************************************
'virtualenv' is not recognized as an internal or external command,
operable program or batch file.

解决办法是重新安装virtualenv

D:\AWS> pip uninstall -y virtualenv
Cannot uninstall requirement virtualenv, not installed
Storing debug log for failure in C:\Users\xionghuilin\pip\pip.log
D:\AWS> pip install virtualenv
Downloading/unpacking virtualenv
Installing collected packages: virtualenv
Successfully installed virtualenv
Cleaning up...

正确安装 结果如下:

D:\Mywork\blog\2016\AWS\AWS2019> python .\aws-elastic-beanstalk-cli-setup\scripts\ebcli_installer.py

***********************************
1. Locating virtualenv installation
***********************************

******************************************
2. Creating exclusive virtualenv for EBCLI
******************************************
Already using interpreter C:\Python27\python.exe
New python executable in C:\Users\xionghuilin\.ebcli-virtual-env\Scripts\python.exe
Installing setuptools, pip, wheel...
done.

************************
3. Activating virtualenv
************************

*******************
4. Installing EBCLI
*******************
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting awsebcli
  Downloading https://files.pythonhosted.org/packages/a6/26/e723d628db031a434466597f2310779a46d41e7548a83e04188c55aaa051/awsebcli-3.16.0.tar.gz (242kB)
  ...
  ...
Successfully built awsebcli cement future pathspec semantic-version termcolor
Installing collected packages: jmespath, urllib3, six, python-dateutil, docutils, botocore, cement, colorama, future, pathspec, PyYAML, idna, certifi, chardet, requests, semantic-version, termcolor, wcwidth, pypiwin32, awsebcli
Successfully installed PyYAML-3.13 awsebcli-3.16.0 botocore-1.13.28 cement-2.8.2 certifi-2019.9.11 chardet-3.0.4 colorama-0.3.9 docutils-0.15.2 future-0.16.0 idna-2.7 jmespath-0.9.4 pathspec-0.5.9 pypiwin32-219 python-dateutil-2.8.0 requests-2.20.1 semantic-version-2.5.0 six-1.11.0 termcolor-1.1.0 urllib3-1.24.3 wcwidth-0.1.7

***********************
5. Creating EB wrappers
***********************

***************
6. Finishing up
***************
Success!


To complete installation, ensure `eb` is in PATH. You can ensure this by executing:


    1. CMD Prompt:

        cmd.exe /c "C:\Users\xionghuilin\.ebcli-virtual-env\executables\path_exporter.bat"

    2. PowerShell:

        & "C:\Users\xionghuilin\.ebcli-virtual-env\executables\path_exporter.vbs"


NOTE: Additionally, you would need to **restart this shell**


D:\AWS>
D:\AWS> eb --version
EB CLI 3.16.0 (Python 2.7.9)

EB CLI操作的简单测试

根据以下指导进行测试:

Get Started
$ mkdir HelloWorld
$ cd HelloWorld
$ eb init -p PHP
$ echo "Hello World" > index.html
$ eb create dev-env
$ eb open
To deploy updates to your applications, use ‘eb deploy’.

测试结果如下:

D:\AWS\Beanstalk> mkdir HelloWorld


    Directory: D:\AWS\Beanstalk


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       27/11/2019  12:06 PM                HelloWorld


D:\AWS\Beanstalk> cd .\HelloWorld
D:\AWS\Beanstalk\HelloWorld> eb init -p PHP
Application HelloWorld has been created.
D:\AWS\Beanstalk\HelloWorld> echo "Hello World" > index.html
D:\AWS\Beanstalk\HelloWorld> eb create dev-env
Creating application version archive "app-191127_120824".
Uploading HelloWorld/app-191127_120824.zip to S3. This may take a while.
Upload Complete.
Environment details for: dev-env
  Application name: HelloWorld
  Region: us-west-2
  Deployed Version: app-191127_120824
  Environment ID: e-xtuncgmjxq
  Platform: arn:aws:elasticbeanstalk:us-west-2::platform/PHP 7.3 running on 64bit Amazon Linux/2.9.1
  Tier: WebServer-Standard-1.0
  CNAME: UNKNOWN
  Updated: 2019-11-27 04:08:38.632000+00:00
Printing Status:
2019-11-27 04:08:37    INFO    createEnvironment is starting.
2019-11-27 04:08:38    INFO    Using elasticbeanstalk-us-west-2-606255748358 as Amazon S3 storage bucket for environment data.
2019-11-27 04:09:03    INFO    Created security group named: sg-04858e5693e0726c4
2019-11-27 04:09:03    INFO    Created load balancer named: awseb-e-x-AWSEBLoa-142K4TE22ELRA
2019-11-27 04:09:20    INFO    Created security group named: awseb-e-xtuncgmjxq-stack-AWSEBSecurityGroup-1OX45879I5282
2019-11-27 04:09:20    INFO    Created Auto Scaling launch configuration named: awseb-e-xtuncgmjxq-stack-AWSEBAutoScalingLaunchConfiguration-17CVE406BL5Q1
2019-11-27 04:10:22    INFO    Created Auto Scaling group named: awseb-e-xtuncgmjxq-stack-AWSEBAutoScalingGroup-1VAKBZQ2RS6O2
2019-11-27 04:10:22    INFO    Waiting for EC2 instances to launch. This may take a few minutes.
2019-11-27 04:10:22    INFO    Created Auto Scaling group policy named: arn:aws:autoscaling:us-west-2:606255748358:scalingPolicy:03298489-18c9-43c0-87b3-5f7931fefa33:autoScalingGroupName/awseb-e-xtuncgmjxq-stack-AWSEBAutoScalingGroup-1VAKBZQ2RS6O2:policyName/awseb-e-xtuncgmjxq-stack-AWSEBAutoScalingScaleDownPolicy-GN2EGUXEO27C
2019-11-27 04:10:22    INFO    Created Auto Scaling group policy named: arn:aws:autoscaling:us-west-2:606255748358:scalingPolicy:e6216fa7-c732-49e1-bcc4-3c1d085596c0:autoScalingGroupName/awseb-e-xtuncgmjxq-stack-AWSEBAutoScalingGroup-1VAKBZQ2RS6O2:policyName/awseb-e-xtuncgmjxq-stack-AWSEBAutoScalingScaleUpPolicy-19O2WY5AGMT1K
2019-11-27 04:10:22    INFO    Created CloudWatch alarm named: awseb-e-xtuncgmjxq-stack-AWSEBCloudwatchAlarmLow-1M3O1KQ22U7BP
2019-11-27 04:10:22    INFO    Created CloudWatch alarm named: awseb-e-xtuncgmjxq-stack-AWSEBCloudwatchAlarmHigh-Z4T27E63I8IY
2019-11-27 04:10:45    INFO    Application available at dev-env.xhifdm4xhe.us-west-2.elasticbeanstalk.com.
2019-11-27 04:10:45    INFO    Successfully launched environment: dev-env

D:\AWS\Beanstalk\HelloWorld>eb deploy

以上默认选择了US West (Oregon)us-west-2创建了应用。yml在目录HelloWorld\.elasticbeanstalk\config.yml,文件内容如下:

branch-defaults:
  default:
    environment: dev-env
    group_suffix: null
global:
  application_name: HelloWorld
  branch: null
  default_ec2_keyname: null
  default_platform: PHP
  default_region: us-west-2
  include_git_submodules: true
  instance_profile: null
  platform_name: null
  platform_version: null
  profile: null
  repository: null
  sc: null
  workspace_type: Application

测试生成的网址:http://dev-env.xhifdm4xhe.us-west-2.elasticbeanstalk.com/,成功显示 Hello World

根据Configure the EB CLI, 认证的信息是放在以下目录的:C:\Users\xionghuilin\.aws\credentials,所以在EB CLI操作过程中是不用进行认证操作的。

测试完成后,删除应用使用指令eb terminate

D:\AWS\Beanstalk\HelloWorld> eb terminate
The environment "dev-env" and all associated instances will be terminated.
To confirm, type the environment name: dev-env
2019-11-27 06:22:41    INFO    terminateEnvironment is starting.
2019-11-27 06:22:58    INFO    Deleted CloudWatch alarm named: awseb-e-xtuncgmjxq-stack-AWSEBCloudwatchAlarmHigh-Z4T27E63I8IY
2019-11-27 06:22:58    INFO    Deleted CloudWatch alarm named: awseb-e-xtuncgmjxq-stack-AWSEBCloudwatchAlarmLow-1M3O1KQ22U7BP
2019-11-27 06:22:58    INFO    Deleted Auto Scaling group policy named: arn:aws:autoscaling:us-west-2:606255748358:scalingPolicy:e6216fa7-c732-49e1-bcc4-3c1d085596c0:autoScalingGroupName/awseb-e-xtuncgmjxq-stack-AWSEBAutoScalingGroup-1VAKBZQ2RS6O2:policyName/awseb-e-xtuncgmjxq-stack-AWSEBAutoScalingScaleUpPolicy-19O2WY5AGMT1K
2019-11-27 06:22:58    INFO    Deleted Auto Scaling group policy named: arn:aws:autoscaling:us-west-2:606255748358:scalingPolicy:03298489-18c9-43c0-87b3-5f7931fefa33:autoScalingGroupName/awseb-e-xtuncgmjxq-stack-AWSEBAutoScalingGroup-1VAKBZQ2RS6O2:policyName/awseb-e-xtuncgmjxq-stack-AWSEBAutoScalingScaleDownPolicy-GN2EGUXEO27C
2019-11-27 06:22:58    INFO    Waiting for EC2 instances to terminate. This may take a few minutes.
2019-11-27 06:25:00    INFO    Deleted Auto Scaling group named: awseb-e-xtuncgmjxq-stack-AWSEBAutoScalingGroup-1VAKBZQ2RS6O2
2019-11-27 06:25:00    INFO    Deleted load balancer named: awseb-e-x-AWSEBLoa-142K4TE22ELRA
2019-11-27 06:25:00    INFO    Deleted Auto Scaling launch configuration named: awseb-e-xtuncgmjxq-stack-AWSEBAutoScalingLaunchConfiguration-17CVE406BL5Q1
2019-11-27 06:25:00    INFO    Deleted security group named: awseb-e-xtuncgmjxq-stack-AWSEBSecurityGroup-1OX45879I5282
2019-11-27 06:25:31    INFO    Deleted security group named: sg-04858e5693e0726c4
2019-11-27 06:25:34    INFO    Deleting SNS topic for environment dev-env.
2019-11-27 06:25:35    INFO    terminateEnvironment completed successfully.

EB CLI指令eb init的问题

碰到问题:
Cannot setup CodeCommit because there is no Source Control setup, continuing with initialization

D:\AWS\Beanstalk> eb init
Cannot setup CodeCommit because there is no Source Control setup, continuing with initialization

需要使用git来做CodeCommit,如下:

D:\AWS\Beanstalk> git init
Initialized empty Git repository in D:\AWS\Beanstalk/.git/
D:\AWS\Beanstalk [master +2 ~0 -0 !]> git add .
D:\AWS\Beanstalk [master +3 ~0 -0 ~]> eb init --interactive
WARNING: Git is in a detached head state. Using branch "default".
WARNING: Git is in a detached head state. Using branch "default".
WARNING: Git is in a detached head state. Using branch "default".

Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-south-1 : Asia Pacific (Mumbai)
7) ap-southeast-1 : Asia Pacific (Singapore)
8) ap-southeast-2 : Asia Pacific (Sydney)
9) ap-northeast-1 : Asia Pacific (Tokyo)
10) ap-northeast-2 : Asia Pacific (Seoul)
11) sa-east-1 : South America (Sao Paulo)
12) cn-north-1 : China (Beijing)
13) cn-northwest-1 : China (Ningxia)
14) us-east-2 : US East (Ohio)
15) ca-central-1 : Canada (Central)
16) eu-west-2 : EU (London)
17) eu-west-3 : EU (Paris)
18) eu-north-1 : EU (Stockholm)
19) ap-east-1 : Asia Pacific (Hong Kong)
20) me-south-1 : Middle East (Bahrain)
(default is 3): 7

Select an application to use
1) XiongElasticBeanstalk
2) [ Create new Application ]
(default is 2): 1
WARNING: Git is in a detached head state. Using branch "default".
WARNING: Git is in a detached head state. Using branch "default".

Select a platform.
1) Node.js
2) PHP
3) Python
4) Ruby
5) Tomcat
6) IIS
7) Docker
8) Multi-container Docker
9) GlassFish
10) Go
11) Java
12) Corretto (BETA)
13) Packer
(default is 1): 2

Select a platform version.
1) PHP 7.3
2) PHP 7.2
3) PHP 7.1
4) PHP 7.0
5) PHP 5.6
6) PHP 5.5
7) PHP 5.4
(default is 1):
WARNING: Git is in a detached head state. Using branch "default".
Do you wish to continue with CodeCommit? (y/N) (default is n):
WARNING: Git is in a detached head state. Using branch "default".
WARNING: Git is in a detached head state. Using branch "default".
Do you want to set up SSH for your instances?
(Y/n): n
D:\AWS\Beanstalk [master +3 ~0 -0 ~]>

使用EB CLI发布项目

测试了使用EB CLI发布项目,对index.php做改动后,再进行发布。

首先碰到以下问题,需要做commit

D:\AWS\Beanstalk [master +3 ~0 -0 | +6 ~0 -2 !]> git add .
warning: LF will be replaced by CRLF in .ebextensions/logging.config.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in cron.yaml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in scheduled.php.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in styles.css.
The file will have its original line endings in your working directory.
D:\AWS\Beanstalk [master +7 ~0 -0 ~]> eb deploy
WARNING: Git is in a detached head state. Using branch "default".
WARNING: Git is in a detached head state. Using branch "default".
WARNING: Git is in a detached head state. Using branch "default".
WARNING: Git is in a detached head state. Using branch "default".
ERROR: CommandError - git could not find the HEAD; most likely because there are no commits present

需要提交,提交后有以下问题,Changes not staged for commitERROR: This branch does not have a default environment

D:\AWS\Beanstalk [master +7 ~0 -0 ~]> git commit
Aborting commit due to empty commit message.
D:\AWS\Beanstalk [master +7 ~0 -0 ~]> git commit -m "latest update"
[master (root-commit) a137324] latest update
 7 files changed, 223 insertions(+)
 create mode 100644 .ebextensions/logging.config
 create mode 100644 .gitignore
 create mode 100644 cron.yaml
 create mode 100644 index.php
 create mode 100644 logo_aws_reduced.gif
 create mode 100644 scheduled.php
 create mode 100644 styles.css
D:\AWS\Beanstalk [master]> eb deploy
ERROR: This branch does not have a default environment. You must either specify an environment by typing "eb deploy my-env-name" or set a default environment by typing "eb use my-env-name".
D:\AWS\Beanstalk [master]> git commit -m "latest update 2"
On branch master
Changes not staged for commit:
        modified:   index.php

no changes added to commit

针对以上两个问题,Changes not staged for commitERROR: This branch does not have a default environment,进行以下操作,可以解决:

D:\AWS\Beanstalk [master +0 ~1 -0 !]> git add index.php
D:\AWS\Beanstalk [master +0 ~1 -0 ~]> git commit -a -m "update after add index.php"
[master 5ec2d37] update after add index.php
 1 file changed, 1 insertion(+), 1 deletion(-)
D:\AWS\Beanstalk [master +0 ~1 -0 !]> eb use Xiongelasticbeanstalk-env-1
D:\AWS\Beanstalk [master]> eb deploy
Creating application version archive "app-5ec2-191127_152548".
Uploading XiongElasticBeanstalk/app-5ec2-191127_152548.zip to S3. This may take a while.
Upload Complete.
2019-11-27 07:26:04    INFO    Environment update is starting.
2019-11-27 07:26:07    INFO    Deploying new version to instance(s).
2019-11-27 07:26:21    INFO    New application version was deployed to running EC2 instances.
2019-11-27 07:26:21    INFO    Environment update completed successfully.

D:\AWS\Beanstalk [master]>

发布完后,再使用DNS网址进行测试:http://xiongelasticbeanstalk-env-1.pysmrc6iwc.ap-southeast-1.elasticbeanstalk.com/,发现最新做的改动已经发布到网页中了。测试成功!

使用eb terminate删除所有的应用,完成本次测试。

Reference

What is AWS CloudFormation?
Getting Started with AWS CloudFormation
AWS Elastic Beanstalk Concepts
What Is AWS Elastic Beanstalk?
Getting Started Using Elastic Beanstalk
使用AWS云安装WordPress + RDS MySQL
403 Forbidden error
Configure the EB CLI
Github aws/aws-elastic-beanstalk-cli-setup

你可能感兴趣的:(AWS,elastic,beanstalk,aws,CloudFormation,EB,CLI,git)