AWS的CloudFormation和Elastic Beanstalk的簡單測試

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章